import optionData from '../data/option';

import buttonData from '../data/button';

import transactionData from '../data/transaction';

import describeData from "../data/describe";

import tabData from '../data/tab';

import audio, {PlayStatus, TriggerAudioListener} from "$utils/tool/audio";
import popup from "$utils/tool/popup";
import {nextTick} from "vue";
import unit from "$utils/unit/unit";

export default <LibMixins>{

    data(){
      return {
          item:{},
          number:1,
          optionData,
          buttonData,
          transactionData,
          describeData,
          PlayStatus,
          tabData,
          playStatus: PlayStatus.none,
          duration:0,
          height:0
      }
    },

    computed:{
        evaluate_number(){
            if (this.item && this.item.evaluate_info && this.item.evaluate_info.evaluate_number) {
                let number = this.item.evaluate_info.evaluate_number;
                number = number > 99 ?'99+':number;
                return '('+number+')';
            } else {
                return '';
            }
        }
    },

    provide(){
      return {
          updateAutoHeight:()=> {
              return this.$refs.tab && this.$refs.tab.updateAutoHeight();
          }
      }
    },

    methods:{

        getDetail(obj){

            if (this.detail) {
                this.item = this.detail;
                obj.success([1]);
                return nextTick(()=>{
                    return this.installHeight();
                })
            }

            return this.$request({
                url:'player/get_player_info',
                data:{
                    pid: this.pid
                },
                token:true
            }).then((data)=>{
                data = data.data || {};

                data.game_info.skill_level_list.map((item)=>{
                    if (item.lid === data.lid) {
                        data.game_level_name = item.game_level_name;
                    }
                });

                this.item = data;

                obj.success([1]);

                return nextTick(()=>{
                    return this.installHeight();
                })

            }).catch(obj.fail);
        },

        setPlayStatus(status){
            if (this._playStatus !== status) {
                this._playStatus = status;
                clearTimeout(this._playStatusTime);

                if (status === PlayStatus.playBefore) {

                    this._playStatusTime = setTimeout(()=>{
                       this.playStatus = this._playStatus;
                    },300);

                } else {
                    this.playStatus = status;
                }

            }
        },

        triggerPlay(){

            if (this.playStatus !== PlayStatus.play && this.playStatus !== PlayStatus.none && this.playStatus !== PlayStatus.pause) {
                audio.paused();
            } else {

                if (this.item.sound) {
                    audio.setSrc(this.item.sound).addListener(TriggerAudioListener.fail, (data)=> {
                        this.setPlayStatus(PlayStatus.none);
                        return data.message && popup.$toast(data.message);
                    })
                        .addListener(TriggerAudioListener.clear, (data)=> {
                            this.setPlayStatus(PlayStatus.none);
                        })
                        .addListener(TriggerAudioListener.status, (status)=> {
                            this.setPlayStatus(status);
                        }).addListener(TriggerAudioListener.speed, ({currDuration,duration})=> {
                        let useDuration = duration - currDuration;

                        this.duration = useDuration < 0 ? 0 :useDuration;
                    }).replay()
                }
            }



        },

        installHeight(){
            this.height = this.$refs.content.offsetHeight - unit.unitPx(50);
        }

    },

    mounted() {
      if (this.detail) {
          this.triggerPlay();
      }
    },

    created(){
        this._playStatus = PlayStatus.none;

    },

    beforeUnmount() {
        clearTimeout(this._playStatusTime);
        // 如果处于播放状态触发
        if (this.playStatus === PlayStatus.play && this.playStatus !== PlayStatus.none && this.playStatus !== PlayStatus.pause){
            return audio.destroy();
        }
    }


}
