

const ADSdk = require("ADSdk");

cc.Class({
    extends: cc.Component,

    properties: {
        closeBtn_kaiUI: cc.Node,
        openBtn_kaiUI: cc.Node,
        closeBtn: cc.Node,
        tixianBtn: cc.Node,
        sureBtn: cc.Node,

        uiNode: [cc.Node], // 0开红包界面 1红包已到账界面 2get UI 3left UI 4提现界面

        curMoneyLabel: cc.Label,
        getMoneyLabel: cc.Label,
        moneyTip: cc.Node,

        // 提现界面
        returnBtn: cc.Node,
        tiXianBtn_1: cc.Node,
        itemParent: cc.Node,
        moneyLabelTiXian: cc.Label,

        iconSpriteFrame: [cc.SpriteFrame],

        videoTip:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {

        this.closeBtn_kaiUI.on("click", this._closeKaiUI, this);
        this.openBtn_kaiUI.on("click", this._open, this);
        this.closeBtn.on("click", this._close, this);
        this.tixianBtn.on("click", this.showMoneyTip, this);
        this.sureBtn.on("click", this.closeMoneyTip, this);

        this.returnBtn.on("click", this._return, this);
        this.tiXianBtn_1.on("click", this._tixian, this);

        this.selectedId = 0;
        this.tipLabel = this.moneyTip.getChildByName("tipLabel").getComponent(cc.Label);
        this.moneyNumArr = ["0.03", "20.00", "30.00", "50.00", "100.00", "200.00"];
    },

    init(id, c) {

        if (c) this.controller = c;
        this.node.active = true;
        this.closeMoneyTip();
        this.showUI(id);
    },

    _close() {

        // 恢复初始
        // this.uiNode[0].active = true;
        // this.uiNode[1].active = true;
        // this.uiNode[2].active = true;
        // this.uiNode[3].y = 0;
        this.node.active = false;

        // TODO 广告 关闭原生广告/插屏广告
    },

    _open() {


        if (AppConst.SHOWADUI) {
            let videoAD = AppConst.probabilityConfig.videoAD;
            if (AppConst.CHANNEL == AppConst.channel.ANDROID) {


                if (this.playVidepNum < videoAD.redBagUI.probability) {
                    // this.videoTip.active = true;
                    this.openBtn_kaiUI.getComponent(cc.Button).interactable = false;
                    ADSdk.pay(10, this);
                } else {
                    // this.videoTip.active = false;
                    this.openRedBag();
                }
            } else {


                if (this.playVidepNum < videoAD.redBagUI.probability) {
                    // this.videoTip.active = true;
                    this.openBtn_kaiUI.getComponent(cc.Button).interactable = false;
                    ADSdk.showVideo(null, (result) => {
                        if (result == 1) {
                            this.openRedBag();
                        }
                    });
                } else {
                    // this.videoTip.active = false;
                    this.openRedBag();
                }
            }
        } else {
            this.openRedBag();
        }


    },

    openRedBag() {

        
        let moneyNum = Utils.getRedPacketNum();
        // 0.00 -- 20.00   0 -- 2000
        this.addNum = 0;
        let min = 1, max = 50;
        if (moneyNum >= 19) {
            this.addNum = 0.01;
        } else {
            this.addNum = Utils.randomNum(min, max) / 100;
        }

        if (moneyNum + this.addNum < 20) {
            Utils.addRedPacketNum(this.addNum);
            Utils.clearRedPacket();
            this.init(1);
        }
        // 更新游戏界面主界面UI
        this.controller.startPage.showRedBtn();
        this.controller.game.showRedBtn();
    },

    _closeKaiUI() {
        // 动画保存红包 更新 UI
        this.controller.startPage.showRedBtn();
        this.controller.game.showRedBtn();
        this._close();
        this.controller.game.closeRedUIAni();
    },

    // 原生UI
    showNativeAdUI(id) {

    },

    // 0开红包界面 1红包已到账界面 2余额界面 3提现界面
    showUI(id) {

        let ui = this.node.children[1];
        this.uiNode[4].active = false;
        ui.scaleX = 0.1;
        ui.scaleY = 0.1;
        let moneyNum = Utils.getRedPacketNum();
        if (moneyNum == 0) moneyNum = "0.00";
        switch (id) {
            case 0:

                
                this.openBtn_kaiUI.getComponent(cc.Button).interactable = true;
                this.uiNode[0].active = true;
                this.uiNode[1].active = false;
                

                // TODO 广告 原生广告/插屏广告
                let num = Utils.randomNum(0, 100);
                let redBagUI = AppConst.probabilityConfig.nativeAD.redBagUI;

                let videoAD = AppConst.probabilityConfig.videoAD;
                this.playVidepNum = Utils.randomNum(0, 100);

                if (this.playVidepNum < videoAD.redBagUI.probability) {
                    this.videoTip.active = true;
                } else {
                    this.videoTip.active = false;
                }


                if (num < redBagUI.probability) {

                    // console.log("==============显示提示");
                    
                    if (AppConst.SHOWADUI) {
                        if (AppConst.CHANNEL == AppConst.channel.ANDROID) {
                            let time = redBagUI.delayTime;
                            this.scheduleOnce(() => {
                                ADSdk.pay(8, this);
                            }, time);
                        } else {
                            ADSdk.showInsertAd();
                        }
                    }  
                }

                break;
            case 1:
                this.uiNode[0].active = false;
                this.uiNode[1].active = true;
                this.uiNode[2].active = true;
                this.uiNode[3].y = 0;

                this.curMoneyLabel.string = moneyNum + "元";
                this.getMoneyLabel.string = this.addNum + "元";
                break;
            case 2:
                this.uiNode[0].active = false;
                this.uiNode[1].active = true;
                this.uiNode[2].active = false;
                this.uiNode[3].y = -80;
                this.curMoneyLabel.string = moneyNum + "元";
                break;
            case 3:
                this.uiNode[4].active = true;
                this.moneyLabelTiXian.string = moneyNum;
                this.showItem();
                break;
        }
        ui.runAction(
            cc.sequence(
                cc.scaleTo(0.3, 1.1),
                cc.scaleTo(0.2, 1)
            ));
    },

    showMoneyTip() {
        this.init(3);
    },

    closeMoneyTip() {
        this.moneyTip.active = false;
    },

    showItem() {
        for (let i = 0; i < this.itemParent.childrenCount; i++) {
            this.itemParent.children[i].getComponent("itemMoney").init(this, i);
        }
    },

    showTip(str,newStr) {
        let des = "红包金额达到" + str + "元方可提现"
        if(newStr) des = str;

        this.tipLabel.string = des;
        this.moneyTip.active = true;
    },

    _return() {
        this._close();
    },

    _tixian() {

        if (this.selectedId != 0) {
            this.showTip(this.moneyNumArr[this.selectedId]);
            return;
        }

        if (window.newPlayer) {
            let moneyNum = Utils.getRedPacketNum();
            if (moneyNum >= 0.03) {
                // 提现
                // QQApi.createRed((result) => {
                //     if (result == 1) {
                //         window.newPlayer = false;
                //         Utils.reduceRedPacketNum(0.03);
                //         this.itemParent.children[0].active = false;
                //         let src = this.itemParent.children[1].getComponent("itemMoney");
                //         src.iconSprite.spriteFrame = this.iconSpriteFrame[1];
                //         moneyNum = Utils.getRedPacketNum();
                //         this.moneyLabelTiXian.string = moneyNum;
                //         this.showTip("恭喜您，提现成功",true);
                //     }else{
                //         this.showTip("系统繁忙，请稍后再试！",true);
                //     }
                // });
            }else{
                this.showTip(0.03);
            }

        } else {
            // this.moneyTip.active = true;
            this.showTip(this.moneyNumArr[this.selectedId]);
        }
    }
    // update (dt) {},
});
