// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import ShipuManager from "../Manager/ShipuManager";
import UserManager from "../Manager/UserManager";
import PrefabManage from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShipuItem extends cc.Component {

    @property(cc.Label)
    nameLabel: cc.Label = null;
    @property(cc.Label)
    levelLab: cc.Label = null;
    @property(cc.Label)
    multipleLab: cc.Label = null;
    @property(cc.Label)
    levelLimitLab: cc.Label = null;
    @property(cc.Label)
    nextLevelLab: cc.Label = null;
    @property(cc.Label)
    moneyLab: cc.Label = null; //当前解锁的钱和升级需要的钱


    @property(cc.Node)
    levelUpBtn: cc.Node = null;

    @property(cc.Node)
    buyBtn: cc.Node = null;

    @property(cc.Sprite)
    iconSpr: cc.Sprite = null;

    @property(cc.Node)
    redPoint1: cc.Node = null;

    @property(cc.Node)
    redPoint2: cc.Node = null;

    @property(cc.Node)
    videoNode: cc.Node = null;
    @property(cc.Label)
    levelLabDesc: cc.Label = null;


    // LIFE-CYCLE CALLBACKS:
    MaxLevel = 6
    // onLoad () {}
    config = null

    levelBgName = ['物品-灰_', '物品-灰_', '物品-绿', '物品-蓝', '物品-紫', '物品-橙', '物品-彩']
    levelName = ['粗糙', '粗糙', '普通', '高级', '优秀', '极品', '神话']
    start() {

    }

    setViewDate(config) {

        this.refreshView(config.id)
    }

    refreshView(id) {
        this.config = ShipuManager.getShipuUserDate(id)

        let configDate = ShipuManager.getShipuConfigDate(this.config.id)
        this.nameLabel.string = configDate.name + `(${this.levelName[this.config.level]})`
        Common5.addUrlSprite_custom('shipuIcon/' + configDate.name, this.iconSpr)
        this.levelLab.string = 'Lv' + this.config.level
        Common5.getSpriteFrameFromBundle('ShiPu', 'texture/' + this.levelBgName[this.config.level], this.node.getChildByName("底框").getComponent(cc.Sprite))

        this.node.getChildByName('goGameBtn').active = false
        this.node.getChildByName('goGameLab').active = false



        if (this.config.isLock) {
            this.nextLevelLab.node.active = false
            this.levelLimitLab.node.active = false
            this.node.getChildByName('满级').active = false
            this.node.getChildByName('kuang').active = false
            this.node.getChildByName('已解锁').active = false
            this.redPoint1.active = false
            this.redPoint2.active = false
            //待解锁
            //let num = Common5.getNumberChangeHanzi(configDate.inComeMultiple,'1',4)
            let num = configDate.inComeMultiple
            this.multipleLab.string = `秒赚收益x${num}倍`
            if (ShipuManager.getCurShipuCanUnLock(this.config.id) == 'all') {
                //金币解锁
                this.buyBtn.active = true
                this.node.getChildByName('kuang').active = true
                this.levelUpBtn.active = false
                this.node.getChildByName('钞票').active = true
                this.node.getChildByName('moneyLab').active = true
                if (configDate.needShipuId == -9999) {
                    this.moneyLab.string = Common5.getNumberChangeHanzi(configDate.unLockMoney, '1', 1) + ''
                } else {
                    this.moneyLab.string = '0'
                }
                this.levelLimitLab.node.active = false
                if (UserManager.getCurMoney() - configDate.unLockMoney >= 0) {
                    this.redPoint1.active = true
                    this.redPoint2.active = true
                } else {
                    this.redPoint1.active = false
                    this.redPoint2.active = false
                }

                this.node.getChildByName("未解锁").active = false
                this.node.getChildByName("条件").active = false
            } else if (ShipuManager.getCurShipuCanUnLock(this.config.id) == 'myLevel') {
                this.node.getChildByName('钞票').active = false
                this.node.getChildByName('moneyLab').active = false
                this.buyBtn.active = false
                this.levelLimitLab.string = configDate.unlLock1 + '级开启'
                this.levelUpBtn.active = false

                this.node.getChildByName("未解锁").active = true
                this.node.getChildByName("条件").active = true
                this.node.getChildByName("条件").getComponent(cc.Label).string = `人物等级:${configDate.unlLock1}`
                this.node.getChildByName('kuang').active = true
            } else if (ShipuManager.getCurShipuCanUnLock(this.config.id) == 'caipu') {
                this.levelUpBtn.active = false
                this.node.getChildByName('钞票').active = false
                this.node.getChildByName('moneyLab').active = false
                this.buyBtn.active = false

                this.node.getChildByName('goGameBtn').active = true
                this.node.getChildByName('goGameLab').active = true



                this.node.getChildByName('goGameLab').getComponent(cc.Label).string = `城市${configDate.gameName}处获得`
                // this.node.getChildByName('goGameBtn')['GameType'] =  fix me

                this.node.getChildByName("未解锁").active = false
                this.node.getChildByName("条件").active = false
            }


        } else {
            //已经解锁
            this.node.getChildByName("未解锁").active = false
            this.node.getChildByName("条件").active = false
            this.levelLimitLab.node.active = false
            this.nextLevelLab.node.active = false

            let nextLevel = ShipuManager.getShipuInComeNextLevel(this.config.id)
            let curLevel = ShipuManager.getShipuInComeCurLevel(this.config.id)

            let num = curLevel//Common5.getNumberChangeHanzi(curLevel,'1',4)

            let num2 = nextLevel//Common5.getNumberChangeHanzi(nextLevel,'1',4)

            this.multipleLab.string = `秒赚收益x${num}倍`
            // this.nextLevelLab.string = `下一级:${num2}倍`
            this.nextLevelLab.string = ``


            if (this.config.level >= this.MaxLevel) {
                this.buyBtn.active = false
                this.levelUpBtn.active = false
                this.node.getChildByName('钞票').active = false
                this.node.getChildByName('moneyLab').active = false
                this.node.getChildByName('满级').active = true
                this.node.getChildByName("kuang").active = false
                this.node.getChildByName('已解锁').active = false
                this.redPoint1.active = false
                this.redPoint2.active = false
            } else {
                this.node.getChildByName('满级').active = false
                this.node.getChildByName('已解锁').active = true
                this.buyBtn.active = false
                this.levelUpBtn.active = false
                this.node.getChildByName('钞票').active = false
                this.node.getChildByName('moneyLab').active = false
                this.node.getChildByName("kuang").active = false

                let spendMoney = ShipuManager.levelUpShipuSpendMoney(this.config.id)
                this.moneyLab.string = Common5.getNumberChangeHanzi(spendMoney, '1', 1) + ''
                // if(UserManager.getCurMoney()-spendMoney>=0){
                //     this.redPoint1.active = true
                //     this.redPoint2.active = true
                //     this.videoNode.active = false
                //     // this.levelLabDesc.string = '升级'
                // }else{
                //     this.redPoint1.active = false
                //     this.redPoint2.active = false
                //     this.videoNode.active = true
                //     //   this.levelLabDesc.string = '升级+15'
                // }
            }
        }





    }


    goToGameBtn() {
        //Common5.playEffect("sound/按键点击")
        let configDate = ShipuManager.getShipuConfigDate(this.config.id)
        PrefabManage.loadPrefabByType(configDate.GameType)
        EventMgr.emitEvent_custom(ryw_Event.exitShiPu);
    }

    checkFinshTask() {
        // let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id
        // if (mainId == MainTaskIdEnum.MainTask_529) {
        //     if (this.config.id == 7) {
        //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_529)
        //         TaskManager.setCurTask(MainTaskIdEnum.MainTask_529_2)
        //         let bagConfig = {
        //             goodId: 1721,
        //             goodNum: 1,
        //         }
        //         BagManager.addBagList(bagConfig)
        //     }
        // }

    }

    unLockShipuClickEvent() {
        Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        let configDate = ShipuManager.getShipuConfigDate(this.config.id)
        let unLockMoney = configDate.unLockMoney
        let needShipuId = configDate.needShipuId
        if (needShipuId != -9999) {
            ShipuManager.shipuUnLockById(this.config.id)
            this.checkFinshTask()
            this.refreshView(this.config.id)
        } else {
            if (UserManager.getCurMoney() - unLockMoney >= 0) {
                UserManager.subMoney(unLockMoney)
                ShipuManager.shipuUnLockById(this.config.id)
                this.checkFinshTask()
                this.refreshView(this.config.id)
            } else {
                //
                PrefabManage.showTextTips('余额不足!')
            }
        }
        EventMgr.emitEvent_custom(ryw_Event.refreshShiPu)
    }

    LevelUpShipuClickEvent() {
        Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        let spendMoney = ShipuManager.levelUpShipuSpendMoney(this.config.id)
        if (UserManager.getCurMoney() - spendMoney >= 0) {

            UserManager.subMoney(spendMoney)
            ShipuManager.shipuLevelUpById(this.config.id)
            this.refreshView(this.config.id)
        } else {
            //
            //
            //看广告
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        Common5.ReportDY("inLevel", "食谱-AD-升级加15");
                        ShipuManager.shipuLevelUpById(this.config.id, 15)
                        this.refreshView(this.config.id)
                    }
                    else {
                        Common5.showTips_custom("广告未观看完");
                    }

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)

            //PrefabManage.showTextTips('余额不足!')
        }
    }

    // update (dt) {}
}