import audio, {CustomAudio, PlayStatus, TriggerAudioListener} from "$utils/tool/audio";
import SoundStatus from '../const/soundStatus';
import AudioStatus from '../const/audio';

import request,{ InstructionsMessageType } from '$utils/request';
import Status from "$mixins/status/const/status";


export default <LibMixins>{

    data(){
        return {
            vSrc:'',
            AudioStatus,
            audioStatus: CustomAudio.supper ? AudioStatus.loading : AudioStatus.supper,
            audioDuration:0,
            audioCurrentDuration:0,
            audioFailMessage:'',
            status_fetch_key:'upload-next'
        }
    },

    methods:{

        installAudioSrc(src,clear:boolean=false){
            if (this.vSrc !== src) {
                this.vSrc = src;
                // 设置暂时不需要录音
                this.setSoundStatus(SoundStatus.end);
                // 清楚资源
                clear && this.clearAssets();
                // 设置播放路径
                this.switchSrc(src);
            }
        },

        triggerAudio(){
            if (this.audioStatus === AudioStatus.supper || this.audioStatus === AudioStatus.loading) return;

            if (this.audioStatus === AudioStatus.none || this.audioStatus === AudioStatus.pause) {
                return audio.play();
            } else if (this.audioStatus === AudioStatus.play) {
                return audio.paused();
            }

        },

        switchSrc(src){

            if (this.audioStatus === AudioStatus.supper) return;

            this.setAudioStatus(AudioStatus.loading);
            audio
                .setSrc(src)
                .addListener(TriggerAudioListener.status,(status)=>{
                    if (status === PlayStatus.play) {
                        this.setAudioStatus(AudioStatus.play);
                    } else if (status === PlayStatus.pause) {
                        this.setAudioStatus(AudioStatus.pause);
                    }
                })
                .addListener(TriggerAudioListener.canplay, ({duration})=>{
                    this.audioDuration = duration;
                    this.audioCurrentDuration = duration;
                    this.setAudioStatus(AudioStatus.none);
                })
                .addListener(TriggerAudioListener.fail, ({message})=>{
                    this.audioFailMessage = message;
                    this.setAudioStatus(AudioStatus.fail);
                })
                .addListener(TriggerAudioListener.speed, ({currDuration,duration})=>{
                    let useDuration = duration - currDuration;

                    this.audioCurrentDuration = useDuration < 0 ? 0 : useDuration;
                })

        },

        setAudioStatus(status){
            if (this.audioStatus === AudioStatus.supper) return;

            if (this.audioStatus !== status) {
                this.audioStatus = status;
            }
        },

        // 提交资源
        submitAssets(){

            if (!this.assets) return;

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

            if (this.$attrs.onSubmitVerification) {
                return this.$emit('submit-verification',{
                    submit: ()=> this.submit()
                });
            } else {
                return this.submit();
            }

        },

        // 提交
        submit(){
            if (!this.assets) return;

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

            this.setStatus(Status.loading);

            if (this.stroageUpload) {
                return  this.triggerSuccess(this.stroageUpload);
            } else {
                return request.uploadFile({
                    url:'hxupload/img_upload',
                    data:{
                        file: this.stroageBlob
                    },
                    token:true,
                    message: InstructionsMessageType.other,
                    failMessage:true
                }).then((data)=>{

                    if (data.isSuccess) {
                        return this.triggerSuccess(data);
                    } else {
                        return this.setStatus(Status.fail);
                    }
                }).catch(()=>{
                    return this.setStatus(Status.fail);
                });
            }

        },

        triggerSuccess(data){

            this.stroageUpload = data.data;

            if (this.$attrs.onUploadNext) {
                return this.statusFetch(true,data.data);
            } else {
                // 设置
                this.$emit('upload-success',data.data);
                // 设置加载状态成功
                return this.setStatus(Status.success);
            }
        },

        triggerChangeStatus(status) {
            if (status === Status.success) {
                // 清除资源
                this.clearAssets();
            }
        }

    },

    created(){
        if (this.src) {
            return this.installAudioSrc(this.src,true);
        }
    },

    beforeUnmount(){
        audio.destroy();
    }

}