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

cc.Class({
    extends: cc.Component,

    properties: {
        starIcon: cc.Sprite,
        starNameLabel: cc.Label,
        subTitleRichText: cc.RichText,
        subTitleRichText1: cc.RichText,
        starUseTitleRichText: cc.RichText,
        starUseBtn: cc.Button,
        starUserSprite: cc.Sprite,
        countLabel: cc.Label,
        countNode: cc.Node,

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        GameEvent.on("Star_Buy_success", this, (areaType, buyGold, starTypeCount) => {
            this._buyGold = buyGold;
            /// 如果是明星所需种类
            if (this._starData.itemId == 102) {
                if (this.__starTypeCount != starTypeCount) {
                    this._starTypeCount = starTypeCount;
                    this.setUpIsCanSign();
                }
                /// 如果是明星需要个数
            } else if (this._starData.itemId == 101) {                
                this.setUpIsCanSign();
                /// 地区需要明星多少个
            } else {
                /// 如果该地区与需要的这个地区匹配
                if (this._starData.itemId == areaType) {
                    this._starData.areaStarCount += 1;
                    this.setUpIsCanSign();
                }
            }
        });
    },

    start () {

    },

    onDestroy() {
        GameEvent.off("Star_Buy_success", this);
    },

    update (dt) {
        /// 如果是锁住的 那就什么都不做嘛 这里就是更新金币
        if (this._isRoomLocked == false && this._isStarLocked == false) {
            /// 如果是没反应的 说明金币不够
            if (this.starUseBtn.interactable == false) {
                if (GameModule.userInfo.gold >= this._buyGold) {
                    this.setUpUseBtnBg(true);
                }  
            }
        }
    },
    // isUnlocked		
    // starCount	明星数量
    // starId		
    // uid
    //areaStarCount
    //itemId
    //itemCount
    init(starData, buyGold, starTypeCount) {
        this._starData = starData;
        this._buyGold = buyGold;
        this._starTypeCount = starTypeCount;
        this._starInfo = Global.BuildingManager.getStarInfo(this._starData.starId);

        this.updateData();

        this.starNameLabel.string = this._starInfo.name;

    },

    /// 实时更新数据
    updateData() {
        this.setUpIsCanSign();
        this.undateStarCount();
    },

    undateStarCount() {
        if (this._starData.starCount > 0) {
            this.countNode.active = true;
            this.countLabel.string = this._starData.starCount + '/' + this._starData.starCount;
        } else {
            this.countNode.active = false;
        }
    },

    /// 设置是否能签约
    setUpIsCanSign() {
        let buyStarCount = GameModule.userInfo.buyStarCount;
        let roomId = this._starData.signRoomId;
        let isUnlocked = Global.BuildingManager.getRoomIsUnlocked(roomId);
        let roomName = Global.BuildingManager.getRoomName(roomId);

        this._isRoomLocked = false;
        this._isStarLocked = false;
        /// 需要的房间锁住的。
        if (isUnlocked == 0) {
            this.subTitleRichText.string = "<img src='star_false'/><color=#2E2E2E> 开启 " + roomName + "</c>";
            this._isRoomLocked = true;
        } else {
            this.subTitleRichText.string = "<img src='star_true'/><color=#2E2E2E> 开启 " + roomName + "</c>";
        }
    
        let needCount = this._starData.itemCount;
        if (needCount > 0) {
            /// 如要明星的个数为解锁条件
            if (this._starData.itemId == 101) {
                /// 如果明星数量足够
                if (buyStarCount >= needCount) {
                    this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "</c>";
                } else {
                    this._isStarLocked = true;
                    this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + buyStarCount + "/" + needCount + ")" + "</c>";
                }
            } else if (this._starData.itemId == 102) {
                if (this._starTypeCount >= needCount) {
                    this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starTypeCount + "/" + needCount + ")" + "</c>";
                } else {
                    this._isStarLocked = true;
                    this.subTitleRichText1.string = "<img src='star_fale'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this.__starTypeCount + "/" + needCount + ")" + "</c>";
                }
            } else {
                if (this._starData.areaStarCount >= needCount) {
                    this.subTitleRichText1.string = "<img src='star_true'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + needCount + ")" + "</c>";
                } else {
                    this._isStarLocked = true;
                    this.subTitleRichText1.string = "<img src='star_false'/><color=#2E2E2E>" + this._starInfo.desc + " (" + this._starData.areaStarCount + "/" + needCount + ")" + "</c>";
                }
            }
        } else {
            this.subTitleRichText1.string = "";
        }
       

        ///说明满足明星和房间的条件 已经解锁 那么只需要判断金币进行预约了
        if (this._isStarLocked == false && this._isRoomLocked == false) {
            /// 如果金币满足
            this.setUpUseBtnBg(GameModule.userInfo.gold >= this._buyGold);
            this.starUseTitleRichText.string = `<color=#ffffff>签约</c><br/><img src='coin_small'/><color=#ffffff>${DWTool.coinParse(this._buyGold)}</c>`;

        } else {
            this.starUseTitleRichText.string = `<img src='skill_lock'/><br/><color=#ffffff>未解锁</c>`;
            this.setUpUseBtnBg(false);
        }

    },
    /// 设置签约的button是否能点击
    setUpUseBtnBg(isActive) {
        this.starUseBtn.interactable = isActive;
        let path = isActive ? './textures/skill/skill_bg_green' : './textures/skill/skill_bg_gray';
        DWTool.loadResSpriteFrame(path)
            .then((spriteFrame) => {
            this.starUserSprite.spriteFrame = spriteFrame;
        });
    },

    signUseBtnAction() {
        this.starUseBtn.interactable = false;
        this.buyStar().then((respondData) => {
            GameModule.userInfo.gold -= this._buyGold;
            /// 本地购买的明星数量加1
            GameModule.userInfo.buyStarCount += 1;
            this._starData.starCount += 1;
            this.undateStarCount();
            GameEvent.fire(GameNotificationKey.StarEnterRoom, this._starData.starId, respondData.roomId);
            GameEvent.fire("Star_Buy_success", this._starInfo.areaType, respondData.buyGold, respondData.starTypeCount);
        }).catch((code, msg) => {
            console.log(code,msg);
        });
    },

    buyStar() {
        return new Promise((resolve, reject) => {
            //// 购买明星
            StarApi.buyStar(this._starData.starId, (respondData) => {
                resolve(respondData);
            }, (code, msg) => {
                reject(code, msg);
            });
        });
    },
});
