// 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 List from "../../Platform/th/List";
import UiBase from "../GameBase/UiBase";
import TaskManager from "../JuQingChat/TaskManager";
import LevelUpManager from "../Manager/LevelUpManager";
import MiShuManager from "../Manager/MiShuManager";
import TanWeiManager from "../Manager/TanWeiManager";

import UserManager from "../Manager/UserManager";
import ZaoCanManager from "../Manager/ZaoCanManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

import jiachengItem from "./jiachengItem";
import zaocanItem from "./zaocanItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class PeopleLevelUpScript extends UiBase {

    @property(cc.RichText)
    levelAndChengHao: cc.RichText = null;

    @property(cc.Label)
    levelLabel: cc.Label = null;

    @property(cc.Label)
    chenghaoLabel: cc.Label = null;

    @property(cc.Label)
    spendLabel: cc.Label = null;

    @property(cc.Label)
    incomeLab: cc.Label = null;

    @property(cc.Label)
    guyongPercentLab: cc.Label = null;

    @property(cc.Label)
    chenghaoPercentLab: cc.Label = null;

    @property(cc.Label)
    shipuPercentLab: cc.Label = null;

    @property(List)
    listZc: List = null; //早餐

    @property(List)
    listJc: List = null; //早餐加成

    @property(List)
    listMs: List = null; //秘书

    @property(List)
    listQy: List = null; //企业

    @property(cc.Node)
    adBtn: cc.Node = null;
    @property(cc.Label)
    adBtnLab: cc.Label = null;

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

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

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


    // LIFE-CYCLE CALLBACKS:
    dataListZc = []
    dataListJc = []
    dataListMs = []
    dataListQy = []
    // onLoad () {}

    onDestroy() {

    }

    start() {
        MiShuManager.initManager()
        let headSpr = this.node.getChildByName("头像").getComponent(cc.Sprite)
        Common5.addUrlSprite_custom("head/" + User.getHeadImg(), headSpr);
        this.scheduleOnce(() => {
            this.setListZaoCan()
            this.setListJc()
            // this.setChengHaoJiaCheng()
            // this.setListM()
            // this.setListQ()
        }, 0.1)

        this.refreshView()
        EventMgr.onEvent_custom(ryw_Event.levelUpChange, () => {
            this.refreshView()
        }, this)

        EventMgr.onEvent_custom(ryw_Event.updateMoney, () => {
            this.refreshView()
        }, this)

        // let index = User.getFirstStepIndex()
        // if(index == 3){
        //     let nodeArray = [this.shengjiBtn, this.node.getChildByName('关闭')]
        //     this.guideView(nodeArray, false, 150)
        // }
    }

    guideView(nodeArray, isPlayAnim = true, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
            //prefab.zIndex = 199
            if (!isPlayAnim) {
                prefab.getComponent(NewGuideScript).setCanPlayAnim(isPlayAnim)
            }
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)

            }
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
        })
    }


    refreshView() {
        let level = User.getMyLevel()
        let chenghao = LevelUpManager.getChengHao()
        let levelUpSpned = LevelUpManager.levelUpSpned()
        //let clickInComeMoney = LevelUpManager.clickInComeMoney()
        let isMaxLevel = LevelUpManager.isPeopleLevelUpMax()
        let myMoney = UserManager.getCurMoney()
        // if (myMoney - levelUpSpned >= 0) {
        //     this.redPoint.active = true
        //     this.adBtn.active = false
        //     this.adBtnLab.string = '升级'
        // } else {
        //     this.redPoint.active = false
        //     this.adBtn.active = true
        //     this.adBtnLab.string = '升级+5'
        // }

        if (isMaxLevel) {
            this.shengjiBtn.active = false
            // this.node.getChildByName('满级').active = true
        } else {

        }
        let jiachengGY = MiShuManager.getMishuAllClickInCome()
        let jiachengTw = TanWeiManager.getAllClickInCome()
        let jiachengCh = LevelUpManager.getAllChengHaoinCome()
        // let clickIncomeNum = User.getClickIncomeNum()
        // let fanbei = User.getShouyiFanbeiNum()

        // if(clickIncomeNum == 0){
        //     clickIncomeNum = 1
        // }
        this.levelAndChengHao.string = `<color=#cd4e25><bold>Lv.${level}</c><color=#4e6d18><bold> ${chenghao}</color>`
        // this.levelLabel.string = '等级:Lv.' + level + ' '
        // this.chenghaoLabel.string = '称号:' + chenghao
        // if(jiachengGY!=0){
        //     clickInComeMoney = clickInComeMoney*(1+jiachengGY)
        // }
        // if(jiachengTw!=0){
        //     clickInComeMoney = clickInComeMoney*(1+jiachengTw)
        // }

        let CurClickIncome = LevelUpManager.getCurClickIncome()
        this.spendLabel.string = '' + Common5.getNumberChangeHanzi(levelUpSpned)
        this.incomeLab.string = '' + Common5.getNumberChangeHanzi(CurClickIncome)


        let strNum1 = Math.ceil(jiachengGY * 100)
        let strNum2 = Math.ceil(jiachengTw * 100)
        this.guyongPercentLab.string = strNum1 + '%'
        this.shipuPercentLab.string = strNum2 + '%'
        this.chenghaoPercentLab.string = Math.ceil(jiachengCh * 100) + '%'

        // let chenghaoid = LevelUpManager.getChengHaoId()
        // let x = 142.9
        // let y = 61.3
        // let light = this.chenghao.getChildByName("kuang").getChildByName("light")
        // if (chenghaoid == 0) {
        //     light.position = cc.v3(-x, y)
        // } else if (chenghaoid == 1) {
        //     light.position = cc.v3(x, y)
        // } else if (chenghaoid == 2) {
        //     light.position = cc.v3(-x, y - 41)
        // } else if (chenghaoid == 3) {
        //     light.position = cc.v3(x, y - 41)
        // } else if (chenghaoid == 4) {
        //     light.position = cc.v3(-x, y - 82)
        // } else if (chenghaoid == 5) {
        //     light.position = cc.v3(x, y - 82)
        // } else if (chenghaoid == 6) {
        //     light.position = cc.v3(-x, y - 123)
        // } else if (chenghaoid == 7) {
        //     light.position = cc.v3(x, y - 123)
        // }
    }

    setChengHaoJiaCheng() {
        for (let i = 0; i < 8; i++) {
            this.chenghao.getChildByName("kuang").getChildByName("JC" + i).getComponent(cc.Label).string = `${Math.ceil(LevelUpManager.getChengHaoinComeById(i) * 100)}%`
        }
    }

    setListM() {
        let mishuConfig = MiShuManager.getManagerConfigDate()
        for (let i = 0; i < mishuConfig.length; i++) {
            this.dataListMs.push(i)
        }

        this.listMs.numItems = this.dataListMs.length;
    }

    setListZaoCan() {
        let zaocanConfig = ZaoCanManager.getManagerConfigDate()
        this.dataListZc = []
        for (let i = 0; i < zaocanConfig.length; i++) {
            this.dataListZc.push(i)
        }

        this.listZc.numItems = this.dataListZc.length;
    }

    onListZaoCanGridRender(item: cc.Node, idx: number) {
        let index = this.dataListZc[idx]
        let zaocanConfig = ZaoCanManager.getManagerConfigDate()
        let _zaocanItem: zaocanItem = item.getComponent(zaocanItem)
        _zaocanItem.setViewDate(zaocanConfig[index])
    }

    setListJc() {
        let zaocanConfig = ZaoCanManager.getAllZaoCan()
        this.dataListJc = []
        for (let i = 0; i < zaocanConfig.length; i++) {
            this.dataListJc.push(i)
        }

        this.listJc.numItems = this.dataListJc.length;
    }

    onListJcGridRender(item: cc.Node, idx: number) {
        let index = this.dataListJc[idx]
        let getAllZaoCan = ZaoCanManager.getAllZaoCan()
        let _jcItem: jiachengItem = item.getComponent(jiachengItem)
        _jcItem.setViewDate(getAllZaoCan[index], '早餐加成')
    }

    setListQ() {

        let configs = TanWeiManager.getManagerConfigDate()
        for (let i = 0; i < configs.length; i++) {
            this.dataListQy.push(i)
        }
        this.listQy.numItems = this.dataListQy.length;


    }

    onListMGridRender(item: cc.Node, idx: number) {
        //item.getComponent(ListItem).title.getComponent(cc.Label).string = this.data[idx] + '';

        let index = this.dataListMs[idx]
        let mishuConfig = MiShuManager.getManagerConfigDate()

        let danItem: jiachengItem = item.getComponent('jiachengItem')
        danItem.setViewDate(mishuConfig[index], 'mishu')
    }


    onListDGridRender(item: cc.Node, idx: number) {

        let index = this.dataListQy[idx]
        let configs = TanWeiManager.getManagerConfigDate()

        let danItem: jiachengItem = item.getComponent('jiachengItem')
        danItem.setViewDate(configs[index], 'tanwei')

    }


    levelUpClickEvent() {
        Common5.playRemoteAudioEffect('sound/diandiandian/升级音效')
        let myMoney = UserManager.getCurMoney()
        let levelUpSpned = LevelUpManager.levelUpSpned()
        if (myMoney - levelUpSpned >= 0) {
            UserManager.subMoney(levelUpSpned)
            let dataPro = LevelUpManager.peopleLevelUp(1)
            if (dataPro) {
                PrefabManage.loadPrefabByType(GameType.JieSuoPro, null, (prefab) => {
                    prefab.getComponent('JieSuoPro').setViewData(dataPro)
                })
            } else {

            }
            this.setListZaoCan()
            this.setListJc()
        } else {
            let tab = {
                onClose: (finish) => {
                    if (finish) {
                        User.setShowAdNum(User.getShowAdNum() + 1)
                        Common5.ReportDY("inLevel", `任务${TaskManager.getCurUnLockMainTaskId()}-AD-升级界面升级`)
                        // Common5.ReportDY("inLevel", "升级界面-AD-升级");
                        let dataPro = LevelUpManager.peopleLevelUp(5)
                        if (dataPro) {
                            PrefabManage.loadPrefabByType(GameType.JieSuoPro, null, (prefab) => {
                                prefab.getComponent('JieSuoPro').setViewData(dataPro)
                            })
                        } else {

                        }
                        this.setListZaoCan()
                    }
                    else {

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

                }, onFailed: () => {

                }
            }
            AppPlatform.playVideo_custom(tab)
        }




    }
    // update (dt) {}

    closeUiView(): void {
        let array = User.getGuideTanChuangBox()

        let guideIndexArray = User.getGuideIndexArray()
        let level = User.getMyLevel()




        if (User.getDeskReward() == false && (level >= 160) && array[1] == 0) {

            array[1] = 1
            User.setGuideTanChuangBox(array)
            PrefabManage.loadPrefabByType(GameType.AddDesk)
        } else if (guideIndexArray[0] == 0 && level >= 200) {
            guideIndexArray[0] = 1
            User.setGuideIndexArray(guideIndexArray)
        }


        this.node.removeFromParent()
        this.node.destroy()
        EventMgr.emitEvent_custom(ryw_Event.ExitBtnEvent, '');
    }

    clickSettingBtnEvent() {
        //Common5.playEffect("sound/按键点击")
        PrefabManage.loadPrefabByType(GameType.Setting)
    }
}