
const AlertManager = require('../utils/AlertManager');
const DrawApi  = require("../net/DrawApi");
const DWTool = require("../utils/DWTool");
const GameModule = require("../utils/GameModule");

cc.Class({
    extends: cc.Component,

    properties: {
        moreRichText: cc.RichText,
        bestRichText: cc.RichText,   
        normalRichText: cc.RichText,     
        normalDrawButton: cc.Button,
        bestChanceButton: cc.Button,
        moreChanceButton: cc.Button,

        normalDrawButtonSprite: cc.Sprite,
        bestChanceButtonSprite: cc.Sprite,
        moreChanceButtonSprite: cc.Sprite,
        content: cc.Node,
    },

     // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.setUpUIIsShow(false);
        // 	 cdTime	已经用了的CD时间 如果 如果为0或者大于和等于总时间 说明可以免费抽奖
        // 	 cdTotalTime	cd总时间 ，目前是60分钟
        this.getDrawState().then((respondData) => {
            this.setUpUIIsShow(true);
            this.respondData = respondData;
            this.setUpUI();
            console.log(respondData);
        }).catch(({code, msg}) => {
            console.log(code, msg);
        });
    },

    start () {

    },

    setUpUIIsShow(isShow) {
        this.normalDrawButton.node.active = isShow;
        this.bestChanceButton.node.active = isShow;
        this.moreChanceButton.node.active = isShow;
    },

    setUpUI() {
        /// 抽奖冷却结束
        if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
            this.setUpNormalButton();
        } else { 
            this.setUpUseBtnBg(false, 1);
            let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
            this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
            this.schedule(this.updateCD, 1);
        }
        this.moreRichText.string =  `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond2}</c></b>`;
        this.bestRichText.string = `<img src='skill_diamond'/><b><color=#ffffff> ${this.respondData.diamond3}</c></b>`;
        
        let hasDiamod = GameModule.userInfo.diamond;
        if (hasDiamod <= this.respondData.diamond2) {
            this.setUpUseBtnBg(false, 2);
        } else if (hasDiamod <= this.respondData.diamond3) {
            this.setUpUseBtnBg(false, 3);
        }
    },

    setUpNormalButton() {
        this.normalDrawButton.interactable = true;
        this.normalRichText.string = '<b><color=#ffffff>看广告\n免费获得</c></b>';
    },
    /// 网络请求
    getDrawState() {
        return new Promise((resolve, reject) => {
            DrawApi.getLotteryInfo((respondData) => {
                resolve(respondData);
            }, ({code, msg}) => {
                reject({code, msg});
            });
        })
    },

    startDrawRequest(typeId, diamond = 0) {
        return new Promise((resolve, reject) => {
            ///开始抽奖
            DrawApi.startLottery(typeId, diamond, (respondData) => {
                // resolve(respondData);
                let code = -3;
                let msg = "test";
                reject({code, msg});
            }, (code, msg) => {
                reject({code, msg});
            });
        })
    },

    startDraw(typeId, completion) {
        this.startDrawRequest(typeId, 0).then((respondData) => {
            /// 把抽奖状态 以及抽奖成功之后的数据加上去
            this.respondData.propId = respondData.propId;
            this.respondData.propName = respondData.propName;
            this.closeNodeAction();
            completion();
            AlertManager.showDrawScrollAlert(this.respondData, typeId);
        }).catch(({code, msg}) => {
            console.log(code, msg);
            completion();
        });
    },

    updateCD() {
        this.respondData.cdTime += 1000;
        if (this.respondData.cdTime >= this.respondData.cdTotalTime) {
            this.setUpNormalButton();
            this.setUpUseBtnBg(true, 1);
            this.unschedule(this.updateCD, this);
        }
        let text = DWTool.calculateTime((this.respondData.cdTotalTime - this.respondData.cdTime) / 1000);
        this.normalRichText.string = '<b><color=#ffffff>' + text + '</c></b>';
    },

    /// 看广告抽奖
    adNodeAction() {
        this.normalDrawButton.interactable = false;
        this.startDraw(1, () => {
            this.normalDrawButton.interactable = true
        });
    },

    ///更多砖石抽奖
    moreChanceNodeAction() {
        this.moreChanceButton.interactable = false;
        this.startDraw(2, () => {
            this.moreChanceButton.interactable = true;
            GameModule.userInfo.diamond -= this.respondData.diamond2;
        });
    },
    
    ///最大概率抽奖
    bestChanceNodeAction () {
        this.bestChanceButton.interactable = false;
        this.startDraw(3, () => {
            this.bestChanceButton.interactable = true;
            GameModule.userInfo.diamond -= this.respondData.diamond3;
        });
    },

    closeNodeAction() {
        let finish = cc.callFunc(() => {
            this.node.destroy();
        }, this);
        let sequence = cc.sequence(cc.scaleTo(0.2, 0, 0).easing(cc.easeBackIn()), finish)
        this.content.runAction(sequence);
    },


    /// 设置button是否能点击 type 123
    setUpUseBtnBg(isActive, type) {
        let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
        DWTool.loadResSpriteFrame(path)
            .then((spriteFrame) => {
            if (type == 1) {
                this.normalDrawButtonSprite.spriteFrame = spriteFrame;
                this.normalDrawButton.interactable = isActive;
            } else if (type == 2) {
                this.moreChanceButtonSprite.spriteFrame = spriteFrame;
                this.moreChanceButton.interactable = isActive;
            } else {
                this.bestChanceButtonSprite.spriteFrame = spriteFrame;
                this.bestChanceButton.interactable = isActive;
            }
        });
    },

    // update (dt) {},
});
