import SoundStatus from '../const/soundStatus';

import SoundRecording, {TriggerListener} from '$utils/tool/sound-recording';

import popup from '$utils/tool/popup';

import file from '$utils/tool/file';
import AudioStatus from "$components/sound-recording/const/audio";
import Status from "$mixins/status/const/status";

export default <LibMixins>{

    data(){
        return {
            SoundStatus,
            soundStatus:SoundStatus.none,
            soundStatusMessage:{
                [SoundStatus.none]:'长按开始录音',
                [SoundStatus.sound]:'正在录音',
                [SoundStatus.jurisdiction]:'浏览器授权后，请点击重试',
                [SoundStatus.support]:'暂不支持'
            },
            countdown:0,
            support:true,
            assets:false
        }
    },

    methods:{

        // 设置当前录音状态
        setSoundStatus(status:SoundStatus){
            if (this.soundStatus !== status) {

                // 如果状态为 none 清楚资源
                if (status === SoundStatus.none) {
                    this.clearAssets();
                }

                this.soundStatus = status;
            }
        },

        reRecording(){
            if (this.status === Status.loading) return;

            if (!this.soundRecording) this.installUseSoundRecording();

            return this.setSoundStatus(SoundStatus.none);
        },

        // 设置授权状态
        setJurisdictionStatus(config:Record<string, any>){

            if (config.support === false) {
                return this.setSoundStatus(SoundStatus.support);
            }

            if (config.jurisdiction === false ) {
                return this.setSoundStatus(SoundStatus.jurisdiction);
            }
        },

        start(){
            if (this.soundStatus === SoundStatus.none ) {
                return this.soundRecording.start();
            }
        },

        stop(){
            if (this.soundStatus === SoundStatus.sound ) {
                return this.soundRecording.stop();
            }
        },

        // 安装
        installSoundRecording(){

            if (this.soundRecording.initializationStatus) return;

            // 安装
            this.soundRecording.initialization().then(()=>{

                this.setJurisdictionStatus({
                    jurisdiction:true,
                    support:true
                });

                this.soundRecording
                  .addListener(TriggerListener.fail,(message)=>{

                    // 设置状态从头开始
                    this.setSoundStatus(SoundStatus.none);

                    message && popup.$toast(message);
                  })
                  .addListener(TriggerListener.speed,(duration)=>{
                        if (this.countdown !== duration) {
                            this.countdown = duration;
                        }
                  })
                  .addListener(TriggerListener.soundStatus,(status:boolean)=>{

                      this.setSoundStatus(status ? SoundStatus.sound : SoundStatus.end);

                  })
                  .addListener(TriggerListener.success,(data)=>{

                      if (this.assets) return;

                      // 设置资源
                      this.setAssets(data.blob);
                      this.setAudioStatus(AudioStatus.loading);

                      // 设置路径
                      file.createdPath(data.blob).then((data)=>{
                          return this.switchSrc(data);
                      }).catch((error)=>{
                          return this.setAudioStatus(AudioStatus.noURL);
                      });
                  })


            }).catch((fail)=>{


                if (typeof fail === 'object') {
                    this.setJurisdictionStatus(fail);
                }

            });

        },

        installUseSoundRecording(){
            // 创建录音对象
            this.soundRecording = new SoundRecording({
                min: this.min,
                max: this.max
            });

            this.installSoundRecording();
        },

        // 设置资源
        setAssets(blob){
            if (blob) {
                this.stroageBlob = blob;
                this.assets = true;
                this.stroageUpload = null;
                if (!this.upload) {
                    this.$emit('update:value',blob);
                }
            }
        },

        // 清楚资源
        clearAssets(){
            if (this.assets) {
                this.assets = false;
                this.stroageBlob = null;
                this.stroageUpload = null;
                if (!this.upload) {
                    this.$emit('update:value',undefined);
                }
            }
        }

    },

    created(){
        !this.src && this.installUseSoundRecording();
    },

    beforeUnmount(){
        this.soundRecording && this.soundRecording.destroy();
        this.soundRecording = null;
    }

}