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

cc.Class({
    extends: cc.Component,

    properties: {

        starNameLabel: cc.Label,

        bottomTitleLabel: cc.Label,

        starNode: cc.Node,

        againButton: cc.Button,

        againRichText: cc.RichText,

        againSprite: cc.Sprite,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },

    init(drawData, typeId, backGroudId) {
        this.typeId = typeId;
        this.drawData = drawData;
        this.starNameLabel.string = '获得   ' + drawData.propName;
        this.bottomTitleLabel.string = drawData.propName + '加入你的明星图集';
        this.starNode.getComponent("DrawStarContent").init(backGroudId, drawData.propId);
        
        if (this.typeId == 1) {
            this._diamond = this.drawData.diamond1;
        } else if (this.typeId == 2) {
            this._diamond = this.drawData.diamond2;
        } else {
            this._diamond = this.drawData.diamond3;
        }

        this.againRichText.string =  `<img src='skill_diamond'/><b><color=#ffffff> ${this._diamond}</c></b>`;
        ////发通知 已经获取明星
        GameEvent.fire(GameNotificationKey.StarEnterRoom, this.drawData.propId, this.drawData.roomId);
        if (GameModule.userInfo.diamond >= this._diamond) {
            this.setUpUseBtnBg(false);
        }
    },

       /// 设置签约的button是否能点击 type 123
    setUpUseBtnBg(isActive) {
        let path = isActive ? './textures/draw/draw_green_btn_bg' : './textures/draw/draw_gray_btn_bg';
        this.againButton.interactable = isActive;
        DWTool.loadResSpriteFrame(path)
            .then((spriteFrame) => {
            this.againSprite.spriteFrame = spriteFrame;
        });
    },
    

    dismissAction () {
        this.node.destroy(); 
        GameEvent.fire("draw_done_action");
     },

    scrollAgainAction() {
        this.againButton.interactable = false
        this.startDrawRequest(this.typeId).then((respondData) => {
            this.againButton.interactable = true;
            GameModule.userInfo.diamond -= this._diamond;
            this.node.destroy();
            GameEvent.fire("draw_again", respondData);
        }).catch((code, msg) => {
            this.againButton.interactable = true;
            cc.log(code, msg);
        });
    },

    startDrawRequest(typeId) {
        return new Promise((resolve, reject) => {
            ///开始抽奖
            DrawApi.startLottery(typeId, 1, (respondData) => {
                resolve(respondData);
            }, (code, msg) => {
                reject(code, msg);
            });
        })
    },
    // update (dt) {},
});
