import JsbSystem from "../../../mk/system/JsbSystem";

/**
 * @description 兑换码界面
 * @author kaka
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class RedeemNode extends cc.Component {

    @property(cc.Label)
    txtGiftName: cc.Label = null;

    @property(cc.Prefab)
    videoPre: cc.Prefab = null;

    @property(cc.Node)
    rds: cc.Node[] = [];
    @property(cc.Sprite)
    rd_imgs: cc.Sprite[] = [];
    @property(cc.Label)
    rd_txts: cc.Label[] = [];

    @property(cc.EditBox)
    inputBox: cc.EditBox = null;

    @property(cc.Node)
    suc_node: cc.Node = null;
    @property(cc.Node)
    suc_node_panel: cc.Node = null;

    @property(cc.Node)
    suc_rds: cc.Node[] = [];
    @property(cc.Sprite)
    suc_rd_imgs: cc.Sprite[] = [];
    @property(cc.Label)
    suc_rd_txts: cc.Label[] = [];

    videoNode = null;

    rewardData: any = null;

    onEnable() {
        gData.redeem.init_redeemData = true;
    }

    update() {
        if (gData.redeem.init_redeemData) {
            this.InitData();
        }
    }

    lateUpdate() {
        gData.redeem.init_redeemData = false;
    }

    InitData() {
        let data = gData.redeem.redeemData.redemption;
        if (!data.content) {
            mk.ui.closePanel('module/redeem/RedeemNode');
            return
        }
        this.txtGiftName.string = data.title;
        this.rewardData = data.content;
        for (let n of this.rds) {
            n.active = false;
        }
        for (let m of this.suc_rds) {
            m.active = false;
        }
        for (let i = 0; i < this.rewardData.length; i++) {
            this.rds[i].active = true;
            this.suc_rds[i].active = true;
            cc.loader.loadRes("game/texture/coin/" + this.rewardData[i].type, cc.SpriteFrame, (err, res) => {
                if (err) { console.log("img load err:" + JSON.stringify(err)); }
                this.rd_imgs[i].spriteFrame = res;
                this.suc_rd_imgs[i].spriteFrame = res;
            });
            this.rd_txts[i].string = mk.math.format(this.rewardData[i].count);
            this.suc_rd_txts[i].string = mk.math.format(this.rewardData[i].count);
        }
    }

    async Click_RedeemBtn() {
        console.log("---RedeemCode: " + this.inputBox.string);
        if (this.inputBox.string != "") {
            let response = await gData.redeem.redeemExchange(this.inputBox.string);
            if (response && response.data.code == 1) {
                console.log("兑换成功");

                this.suc_node_panel.scale = 0;
                this.suc_node.active = true;
                cc.tween(this.suc_node_panel).to(0.2, { scale: 1 }, { easing: "backOut" }).start();
            }
            else {
                mk.tip.pop(response.data.msg);
            }
        } else {
            mk.tip.pop('请输入兑换码');
        }
    }

    coolTime = false
    /** 点击教程 */
    async Click_TeachBtn() {
        mk.audio.playEffect('button');
        if (this.coolTime) {
            return
        }

        this.coolTime = true

        console.log('clickTeach')
        gData.redCodeData.teachVideoType = 2;
        this.videoNode = cc.instantiate(this.videoPre)
        this.videoNode.getChildByName('video').on("completed", this.onCompleted, this)
        this.node.addChild(this.videoNode)
    }

    onCompleted() {
        this.videoNode.getChildByName('video').off("completed", this.onCompleted, this)
        this.videoNode.destroy()
        // GameM.audioM.setResumeMusic()
        this.coolTime = false
    }

    /** 点击复制公众号 */
    clickCopyOfficial() {
        mk.audio.playEffect('button');
        JsbSystem.setClipboard("白羊游戏社")
    }

    // /** 点击复制礼包名 */
    // clickCopyInfo() {
    //     mk.audio.playEffect('button');
    //     JsbSystem.setClipboard(this.txtTitle.string)
    // }

    async Click_GetBtn() {
        this.suc_node.active = false;

        for (let i = 0; i < this.rewardData.length; i++) {
            mk.audio.playEffect('rewardClose');
            mk.fly.PlayCoinAnim(this.rewardData[i].type, 5, cc.v2(0, -300), cc.v2(0, 0))
        }


        let response = await gData.redeem.redeemGetRedemption();
        // if (response && response.data.code == 1) {
        //     this.InitData();
        // }
        // else {

        // }

        // AdM.onSendEvent('duihuan_success', '兑换码-兑换成功', 'duihuan')
    }
}
