import unit from "$utils/unit/unit";
import popup from "$utils/tool/popup";
import {PopupExportComponent} from "$popup/popup-export/const";
import user from "$config/user";
import typeData from '../data/type';
import {GiftType} from "$popup/popup-detail/const/giftType";

export default <LibMixins>{

    data(){
        return {
            vValue:false,
            vNumber:1,
            vItem:{},
            width: unit.unitPx(120),
            height:unit.unitPx(110),
            vX:0,
            vType:0,
            vY:0,
            diff:10
        }
    },

    watch:{
        value:{
            handler:function (){
                return this.setValue(this.value);
            },
            immediate:true
        },
        vNumber:function (){
          return this.triggerUpdateNumber(this.vNumber);
        },
        x:{
            handler:function () {
                this.setTargetValue(this.x,'vX');
            },
            immediate:true
        },
        y:{
            handler:function () {
                this.setTargetValue(this.y,'vY');
            },
            immediate:true
        },
        item:{
            handler:function () {
                this.setItem(this.item,'vItem');
            },
            immediate:true
        },
        type:{
            handler:function () {
                this.setItem(this.type,'vType');
            },
            immediate:true
        },
    },

    computed:{
       config(){
           console.log(typeData[this.vType]);
           return typeData[this.vType] || typeData[GiftType.gift];
       }
    },

    methods:{

        setTargetValue(number,key){
            if (this.value === false) return null;
            let diff = Math.abs(this[key] - number);
            if (diff >= this.diff) {
                this.setItem(number,key);
            }
        },

        setItem(item,key){

            if (this.value === false) return null;
            if (this[key] !== item ) {
                this[key]= item;
            }

        },

        setValue(value){
            if (this.vValue !== value) {
                this.vValue = value;

                if (value) {
                    this.stroageAttrs = Object.assign({},this.$attrs);
                    this.setItem(this.number,'vNumber');
                }

                if (this.value !== value) {
                    return this.$emit('update:value',value);
                }
            }
        },

        openRecharge(){
            if (!user.verificationLogin()) return;
            return popup.$popup.open(PopupExportComponent.recharge);
        },

        triggerUpdateNumber(number:number){
          if (this.stroageAttrs.updateNumber && this.number !== number) {
              this.stroageAttrs.updateNumber({
                  item:this.vItem,
                  number
              });
          }
        },

        // 触发打赏
        triggerReward(){

            if (!user.verificationLogin()) return;

            if (this.stroageAttrs.reward) {
                this.stroageAttrs.reward({
                    item:this.vItem,
                    value: this.vNumber
                });
            }
        }

    },

}
