// 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 TanWeiManager from "../Manager/TanWeiManager";
import UserManager from "../Manager/UserManager";
import PrefabManage from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TanweiItem 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;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    @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 = TanWeiManager.getShipuUserDate(id)

        let configDate = TanWeiManager.getShipuConfigDate(this.config.id)
        this.nameLabel.string = configDate.name + `(${this.levelName[this.config.level]})`
        Common5.addUrlSprite_custom('tanweiTexture/icon/' + 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', 1)
            this.multipleLab.string = `点击收益x${configDate.inComeMultiple}倍`
            if (TanWeiManager.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) + ''

                    if (UserManager.getCurMoney() - configDate.unLockMoney >= 0) {
                        this.redPoint1.active = true
                        this.redPoint2.active = true
                    } else {
                        this.redPoint1.active = false
                        this.redPoint2.active = false
                    }
                } else {
                    this.moneyLab.string = '0'
                }

                this.levelLimitLab.node.active = false

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

                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 (TanWeiManager.getCurShipuCanUnLock(this.config.id) == 'caipu') {
                this.node.getChildByName('钞票').active = false
                this.node.getChildByName('moneyLab').active = false
                this.buyBtn.active = false
                this.levelUpBtn.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("未解锁").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 = TanWeiManager.getShipuInComeNextLevel(this.config.id)
            let curLevel = TanWeiManager.getShipuInComeCurLevel(this.config.id)

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

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


            this.multipleLab.string = `点击收益x${curLevel}倍`
            // this.nextLevelLab.string = `下一级:${nextLevel}倍`


            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 = TanWeiManager.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'
                // }
            }
        }
    }


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

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

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

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)
        }
    }
    goToGameBtn() {

    }
    // update (dt) {}
}