// 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 User from "../../FrameWork/User/User";
import AppPlatform from "../../FrameWork/Util/AppPlatform";
import Common5 from "../../Platform/th/Common5";
import TaskManager from "../JuQingChat/TaskManager";
import MiShuManager from "../Manager/MiShuManager";
import UserManager from "../Manager/UserManager";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class MishuItem extends cc.Component {


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

    @property(cc.Node)
    levelUpNode: cc.Node = null; //升级
    @property(cc.Node)
    unLockBtnNode: cc.Node = null; //未解锁

    @property(cc.Node)
    guyongBtn: cc.Node = null; //雇佣

    @property(cc.Label)
    miaozhuanLab: cc.Label = null;
    @property(cc.Label)
    nameLab: cc.Label = null;
    @property(cc.Label)
    levelLab: cc.Label = null;
    @property(cc.Node)
    redPoint1: cc.Node = null;

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

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    userDate = null
    isMishu = false
    levelUpNeedAd = false
    guyongNeedAd = false
    levelUpNeedMoney = 0
    guyongNeedMoney = 0

    start() {

        EventMgr.onEvent_custom(ryw_Event.mishuLock, () => {
            this.refreshShowBtn()
            // this.refreshLevelBtn()
            this.refreshGuyongBtn()
        }, this)


        EventMgr.onEvent_custom(ryw_Event.mishuLevelUp, () => {
            this.refreshShowBtn()
            // this.refreshLevelBtn()
            this.refreshGuyongBtn()
        }, this)

        EventMgr.onEvent_custom(ryw_Event.updateMoney, () => {

            // this.refreshLevelBtn2()

        }, this)
    }

    initView() {
        this.redPoint1.active = false
        this.redPoint2.active = false


        this.node.getChildByName('已经满级').active = false
        let configDate = MiShuManager.getConfigDateById(this.userDate.mishuId)
        console.log(this.userDate.mishuId, configDate.selfLevelStr, configDate.lockLevelStr)

        if (configDate.name == '习婉工') {
            this.nameLab.string = '席婉工'
        } else {
            this.nameLab.string = configDate.name
        }

        let curMyLevel = User.getMyLevel()
        let selfLevel = configDate.selfLevel


        let lockLab = this.node.getChildByName('lockLab').getComponent(cc.Label)
        lockLab.string = ''

        if (configDate.selfLevelStr != '') {
            lockLab.string = configDate.selfLevelStr
            // if (curMyLevel >= selfLevel) {
            //     if (configDate.lockLevelStr != '') {
            //         lockLab.string = configDate.lockLevelStr
            //     }
            // }
        } else {
            // if (configDate.lockLevelStr != '') {
            //     lockLab.string = configDate.lockLevelStr
            // }
        }

        if (configDate.name == '茜滢') {
            Common5.addUrlSprite_custom("guyongTexture/茜滢2", this.iconSpr)

        } else {
            Common5.addUrlSprite_custom("guyongTexture/" + configDate.name, this.iconSpr)

        }


        this.guyongNeedMoney = configDate.lockMoney
        if (configDate.name == '茜滢') {
            this.guyongNeedMoney = 0
        }

        let labMoney = this.guyongBtn.getChildByName('labMoney').getComponent(cc.Label)

        if (configDate.name == '茜滢') {
            labMoney.string = '免费'
        } else {
            labMoney.string = Common5.getNumberChangeHanzi(configDate.lockMoney) + ''
        }

        this.refreshShowBtn()
        // this.refreshLevelBtn()
        this.refreshGuyongBtn()
    }

    setViewDate(userDate, isMishu: boolean) {

        this.userDate = userDate
        this.isMishu = isMishu
        this.initView()


    }

    refreshShowBtn() {
        let mishuId = this.userDate.mishuId
        let curUserDate = MiShuManager.getMishuUserDateById(mishuId)

        if (curUserDate.isLock) {
            //没有解锁
            let needLock = MiShuManager.getMishuIsLockById(mishuId)

            // console.log(needLock, mishuId)
            if (needLock) {
                // 可以解锁
                this.unLockBtnNode.active = false
                this.guyongBtn.active = true
                this.levelUpNode.active = false
                let lockLab = this.node.getChildByName('lockLab')
                lockLab.active = false

                let zhezhao = this.node.getChildByName('遮罩')
                zhezhao.active = false
            } else {
                this.unLockBtnNode.active = true
                this.guyongBtn.active = false
                this.levelUpNode.active = false
                let lockLab = this.node.getChildByName('lockLab')
                lockLab.active = true

                let zhezhao = this.node.getChildByName('遮罩')
                zhezhao.active = true

                let unLockBtnLab = this.unLockBtnNode.getChildByName('lab').getComponent(cc.Label)
                let lsbStr = ''
                let configDate = MiShuManager.getConfigDateById(mishuId)
                // if (configDate.lockLevelStr && configDate.lockLevelStr.length > 0) {
                //     lsbStr = configDate.lockLevelStr
                // } else {
                    lsbStr = configDate.selfLevelStr
                // }
                unLockBtnLab.string = lsbStr
            }
            this.miaozhuanLab.string = '0'
            this.levelLab.string = '0级'
            //是否可以雇佣
        } else {
            this.unLockBtnNode.active = false
            this.guyongBtn.active = false
            this.levelUpNode.active = true
            let lockLab = this.node.getChildByName('lockLab')
            lockLab.active = false

            let zhezhao = this.node.getChildByName('遮罩')
            zhezhao.active = false



        }
    }

    refreshLevelBtn2() {
        if (this.levelUpNode.active && this.levelUpNode.getComponent(cc.Button).enabled) {
            let mishuId = this.userDate.mishuId
            this.levelUpNeedMoney = MiShuManager.getlevelUpMoneyById(mishuId)
            let lab = this.levelUpNode.getChildByName('lay').getChildByName('lab')
            let adIcon = this.levelUpNode.getChildByName('lay').getChildByName('adIcon')
            if (UserManager.getCurMoney() - this.levelUpNeedMoney >= 0) {
                this.levelUpNeedAd = false
                adIcon.active = false
                lab.getComponent(cc.Label).string = '升级'
                // this.redPoint1.active = true
                // this.redPoint2.active = true

            } else {
                this.levelUpNeedAd = true
                //出现广告
                adIcon.active = true
                lab.getComponent(cc.Label).string = '升级+5'
                this.redPoint1.active = false
                this.redPoint2.active = false

            }
        }
    }


    refreshLevelBtn() {
        //升级钱够不够

        if (this.levelUpNode.active) {
            let mishuId = this.userDate.mishuId
            this.levelUpNeedMoney = MiShuManager.getlevelUpMoneyById(mishuId)
            let labMoney = this.levelUpNode.getChildByName('labMoney')
            let lab = this.levelUpNode.getChildByName('lay').getChildByName('lab')
            let adIcon = this.levelUpNode.getChildByName('lay').getChildByName('adIcon')
            let chaopiao = this.levelUpNode.getChildByName('钞票')
            labMoney.getComponent(cc.Label).string = Common5.getNumberChangeHanzi(this.levelUpNeedMoney, '1', 1) + ''
            this.levelUpNode.getChildByName('lay').active = true
            this.levelUpNode.getChildByName('maxLab').active = false
            labMoney.active = true
            chaopiao.active = true
            this.levelUpNode.getComponent(cc.Button).enabled = true
            this.redPoint1.active = false
            this.redPoint2.active = false
            if (UserManager.getCurMoney() - this.levelUpNeedMoney >= 0) {
                this.levelUpNeedAd = false
                adIcon.active = false
                lab.getComponent(cc.Label).string = '升级'
                // this.redPoint1.active = true
                // this.redPoint2.active = true
            } else {
                this.levelUpNeedAd = true
                //出现广告
                adIcon.active = true
                lab.getComponent(cc.Label).string = '升级+5'
                this.redPoint1.active = false
                this.redPoint2.active = false

            }

            let curUserDate = MiShuManager.getMishuUserDateById(mishuId)
            let shouyi = MiShuManager.getIncomeById(mishuId)
            this.miaozhuanLab.string = Common5.getNumberChangeHanzi(shouyi) + ''
            this.levelLab.string = curUserDate.mishuLevel + '级'

            let isMaxLevel = MiShuManager.isMaxLevel(mishuId)
            if (isMaxLevel) {
                this.levelUpNode.getChildByName('lay').active = false
                this.node.getChildByName('kuang').active = false
                this.levelUpNode.getChildByName('maxLab').active = true
                this.levelUpNode.getComponent(cc.Button).enabled = false
                this.levelUpNode.active = false
                this.node.getChildByName('已经满级').active = true
                labMoney.active = false
                chaopiao.active = false
                this.redPoint1.active = false
                this.redPoint2.active = false


            } else {
                this.node.getChildByName('已经满级').active = false
            }


        }
    }


    refreshGuyongBtn() {
        // 雇佣钱够不够

        if (this.guyongBtn.active) {
            let lab = this.guyongBtn.getChildByName('lay').getChildByName('lab')
            let adIcon = this.guyongBtn.getChildByName('lay').getChildByName('adIcon')

            if (UserManager.getCurMoney() - this.guyongNeedMoney >= 0) {
                this.guyongNeedAd = false
                adIcon.active = false
                // lab.getComponent(cc.Label).string = '雇佣'
                // this.redPoint1.active = true
                this.redPoint2.active = true
            } else {
                this.guyongNeedAd = true
                //出现广告
                adIcon.active = true
                // lab.getComponent(cc.Label).string = '免费雇佣'
                this.redPoint1.active = false
                this.redPoint2.active = false

            }


        }
    }
    // update (dt) {}
    levelUpBtnClick() {
        Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        if (this.levelUpNeedAd) {
            //看广告
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-秘书升级`)
                        // Common5.ReportDY("inLevel", "秘书-AD-升级");
                        let isFirst20 = MiShuManager.upDateMishuLevelUp(this.userDate.mishuId, this.isMishu, 5)

                        let curUserDate = MiShuManager.getMishuUserDateById(this.userDate.mishuId)
                        if (curUserDate.mishuLevel >= 20 && isFirst20) {
                            Common5.playRemoteAudioEffect('sound/diandiandian/20级音效')

                            PrefabManage.loadPrefabByType(GameType.MishuReward, null, (prefab) => {
                                prefab.getComponent('MishuReward').setViewDate(this.userDate.mishuId, this.isMishu, 'levelUp')
                            })
                        }

                    }
                    else {

                        Common5.showTips_custom("广告未观看完");
                    }

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)

        } else {
            // 花钱
            UserManager.subMoney(this.levelUpNeedMoney)
            let isFirst20 = MiShuManager.upDateMishuLevelUp(this.userDate.mishuId, this.isMishu)
            let curUserDate = MiShuManager.getMishuUserDateById(this.userDate.mishuId)
            if (curUserDate.mishuLevel >= 20 && isFirst20) {
                Common5.playRemoteAudioEffect('sound/diandiandian/20级音效')

                PrefabManage.loadPrefabByType(GameType.MishuReward, null, (prefab) => {
                    prefab.getComponent('MishuReward').setViewDate(this.userDate.mishuId, this.isMishu, 'levelUp')
                })
            }
        }
    }
    guyongBtnClick() {
        Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        if (this.guyongNeedAd) {
            //看广告    


            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-秘书雇佣`)
                        // Common5.ReportDY("inLevel", "秘书-AD-雇佣");
                        MiShuManager.setMishuLock(this.userDate.mishuId, this.isMishu)
                        PrefabManage.loadPrefabByType(GameType.MishuReward, null, (prefab) => {
                            prefab.getComponent('MishuReward').setViewDate(this.userDate.mishuId, this.isMishu, 'lock')
                        })
                    }
                    else {
                        Common5.showTips_custom("广告未观看完");
                    }

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)


        } else {
            // 花钱
            UserManager.subMoney(this.guyongNeedMoney)
            MiShuManager.setMishuLock(this.userDate.mishuId, this.isMishu)
            PrefabManage.loadPrefabByType(GameType.MishuReward, null, (prefab) => {
                prefab.getComponent('MishuReward').setViewDate(this.userDate.mishuId, this.isMishu, 'lock')
            })
        }
    }
}