import userIntegral from '$utils/control/integral';
import popup from "$utils/tool/popup";
import $request, {InstructionsMessageType} from "$utils/request";
import socket from "$utils/socket";
import MessageType from "$utils/socket/const/messageType";
import {NoticeType} from "$utils/socket/const";

import globalUser from '$config/user';

export default {

    content:undefined,

    giveGift(item,number,user){

        return new Promise((pResolve, pReject)=>{

            if (this.giveGiftStatus) return pReject();

            if (item.gid) {

                // 计算余额是否足够
                let integral = item.gift_price * number;

                if (userIntegral.checkBalance(integral,true)) {
                    popup.$confirm({
                        title:'是否赠送'+number+'个'+(item.gift_name)+'?',
                        successAsyncText:'赠送成功',
                        confirm:()=>{
                            return new Promise((resolve, reject)=>{
                                $request({
                                    url:'playergift/send_gift',
                                    token:true,
                                    data:{
                                        num: number,
                                        gid:item.gid,
                                        to_uid_list:user.uid
                                    },
                                    next:({status})=> this.giveGiftStatus = status,
                                    failMessage:true,
                                    message:InstructionsMessageType.other
                                }).then((data)=>{
                                    if (data.isSuccess) {

                                        userIntegral.reduceBalance(integral);
                                        let conversation = socket.getConversation(user.uid);
                                        conversation && conversation.send({
                                            messageType:MessageType.gift,
                                            content:socket.addUser({
                                                gift:item,
                                                number:number,
                                                message:'打赏'+user.nick_name+number+'个'+(item.gift_name)
                                            },user)
                                        });

                                        if (this.content) {
                                            this.content.sendGift && this.content.sendGift(item,number,user);
                                        }

                                        resolve(true);
                                        pResolve(data);

                                    } else {
                                        resolve(false);
                                        pReject();
                                    }
                                }).catch(()=>{
                                    reject();
                                    pReject();
                                });
                            });
                        }
                    });
                }


            } else {
                pReject();
            }
        });

    },

    giveNaming(item,user){
        return new Promise((pResolve, pReject)=>{

            if (this.giveGiftStatus) return pReject();

            if (item.gid) {

                // 计算余额是否足够
                let integral = parseFloat(item.gift_price);

                if (userIntegral.checkBalance(integral,true)) {
                    popup.$confirm({
                        title:'是否为'+user.nick_name+'冠名'+(item.days)+'天?',
                        successAsyncText:'赠送成功',
                        confirm:()=>{
                            return new Promise((resolve, reject)=>{
                                $request({
                                    url:'player/do_guanming_user',
                                    token:true,
                                    data:{
                                        gid:item.gid,
                                        to_uid:user.uid
                                    },
                                    next:({status})=> this.giveGiftStatus = status,
                                    failMessage:true,
                                    message:InstructionsMessageType.other
                                }).then((data)=>{
                                    if (data.isSuccess) {

                                        // 通知更新冠名
                                        socket.triggerNotice(NoticeType.naming, {
                                            uid: user.uid,
                                            naming:{
                                                nick_name: (globalUser.user.nick_name || '').substr(0,2),
                                                image: item.base_image
                                            }
                                        });

                                        userIntegral.reduceBalance(integral);
                                        // let conversation = socket.getConversation(user.uid);
                                        // conversation && conversation.send({
                                        //     messageType:MessageType.gift,
                                        //     content:socket.addUser({
                                        //         gift:item,
                                        //         number:number,
                                        //         message:'打赏'+user.nick_name+number+'个'+(item.gift_name)
                                        //     },user)
                                        // });
                                        // if (this.content) {
                                        //     this.content.sendGift && this.content.sendGift(item,number,user);
                                        // }

                                        resolve(true);
                                        pResolve(data);

                                    } else {
                                        resolve(false);
                                        pReject();
                                    }
                                }).catch(()=>{
                                    reject();
                                    pReject();
                                });
                            });
                        }
                    });
                }


            } else {
                pReject();
            }
        });
    },

}
