import unit from "$utils/unit/unit";
import popup,{PopupExportComponent} from "$utils/tool/popup";
import {mapState} from "vuex";

import userIntegral from '$utils/control/integral';
import {InstructionsMessageType} from "$utils/request";
import giveGift from "$utils/control/giveGift";

import typeData from '../data/type';
import {GiftType} from "$popup/popup-detail/const/giftType";

export default <LibMixins>{

    data(){
        return {
            swiperOption:{
                slidesPerView:5,
                slidesPerGroup:5,
                spaceBetween:unit.unitPx(10),
                slidesPerColumn:2,
                slidesPerColumnFill:'row'
            },
            offsetTop: unit.unitPx(10),
            giftUserData:[],
        }
    },

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

    methods:{
        mouseover(event,item){
            if (event.target) {
                if (this.useOffsetWidth === undefined) {
                    this.useOffsetWidth =event.target.parentNode.offsetWidth /2;
                }

                popup.$popup && popup.$popup.open(PopupExportComponent.giftBubble,{
                    value:true,
                    x: event.clientX - event.offsetX + this.useOffsetWidth,
                    y: event.clientY - event.offsetY + this.offsetTop,
                    item,
                    type: this.type,
                    number: this.numberObject[item.gid] || 1,
                    updateNumber:(data)=> this.updateNumber(data),
                    reward:(data)=> this.reward(data)
                });
            }

        },
        mouseout(event){
            // 设置关闭
            popup.$popup && popup.$popup.open(PopupExportComponent.giftBubble,{value:false});
        },

        updateNumber({item,number}){
            this.numberObject[item.gid] = number;
        },

        giveGift({item,value}){
            return giveGift.giveGift(item,value,this.useOption);
        },

        giveNaming({item}){
            return giveGift.giveNaming(item,this.useOption);
        },

        reward(data){

            if (this.rewardStatus) return;

            this[this.config.give] && this[this.config.give](data);

            // if (item.gid) {
            //
            //     // 计算余额是否足够
            //     let integral = item.gift_price * value;
            //
            //     if (userIntegral.checkBalance(integral,true)) {
            //         popup.$confirm({
            //             title:'是否赠送'+value+'个'+(item.gift_name)+'?',
            //             successAsyncText:'赠送成功',
            //             confirm:()=>{
            //                 return new Promise((resolve, reject)=>{
            //                     this.$request({
            //                         url:'playergift/send_gift',
            //                         token:true,
            //                         data:{
            //                             num: value,
            //                             gid:item.gid,
            //                             to_uid_list:this.useOption.uid
            //                         },
            //                         next:({status})=> this.rewardStatus = status,
            //                         failMessage:true,
            //                         message:InstructionsMessageType.other
            //                     }).then((data)=>{
            //                         if (data.isSuccess) {
            //                             userIntegral.reduceBalance(integral);
            //                             resolve(true);
            //                         } else {
            //                             resolve(false);
            //                         }
            //                     }).catch(reject);
            //                 });
            //             }
            //         });
            //     }
            //
            //
            // }

        },

        fetch(obj){
            return this[this.config.fetch](obj);
        },

        changeData(data){
            this.giftUserData = data;
        },

        fetchGift(obj){
            return this.$store.dispatch('initializationUserGiftPromise').then(obj.success).catch(obj.fail);
        },

        fetchNaming(obj){
            return this.$request({
                url:'player/get_guanming_list',
                token:true,
                cache:true
            }).then((data)=>{
               return obj.success((data.data || []).map((item)=>{
                   return {
                       gift_price:item.price,
                       gift_name:item.name,
                       base_image: item.image,
                       gid:item.id,
                       days: item.days
                   }
               }));
            }).catch(obj.fail);
        }

    },

    // computed:mapState({
    //     // @ts-ignore
    //     giftUserData:(state)=> state.gift.giftUserData
    // }),

    created() {
        this.numberObject = {};
    }

}
