// 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 List from "../../Platform/th/List";
import UiBase from "../GameBase/UiBase";
import ShipuManager from "../Manager/ShipuManager";
import TanWeiManager from "../Manager/TanWeiManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import TanweiItem from "./TanweiItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TanweiScript extends UiBase {

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

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

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

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    start(): void {
        TanWeiManager.initManager()
        this.scheduleOnce(() => {
            this.setList()
        }, 0.01)

        this.init()
        EventMgr.onEvent_custom(ryw_Event.refreshTanWei, () => {
            this.init()
        }, this)
    }

    init() {
        let getUnLockNum = TanWeiManager.getUnLockNum()
        this.wupingNum.string = `已获得物品:${getUnLockNum}`
        let getAllShipuInCome = TanWeiManager.getAllShipuInCome()
        this.jiaChengNum.string = `收益总加成:${getAllShipuInCome == 1 ? 0 : getAllShipuInCome}`
    }


    dataListTW = []



    setList() {
        let array = User.getTanweiArray()
        this.dataListTW = []
        for (let n = 0; n < array.length; n++) {
            this.dataListTW.push(array[n]);
        }
        this.listTW.numItems = this.dataListTW.length;

        // let index = TanWeiManager.getLockItemIndex()
        // this.listTW.scrollTo(index)

        this.guideShipu()
    }

    guideShipu() {
        let guideIndexArray = User.getGuideIndexArray()
        // if (guideIndexArray[0] == 1 && guideIndexArray[1] == 1 && guideIndexArray[2] == 0) {
        if (TanWeiManager.getCurShipuCanUnLock(0) == 'all' && TanWeiManager.getLockById(0)) {
            let node = this.node.getChildByName('guideNode')
            this.guideView([node], 100)
            // guideIndexArray[2] = 1
            // User.setGuideIndexArray(guideIndexArray)
        } else {
            let index = TanWeiManager.getLockItemIndex()
            this.listTW.scrollTo(index)

        }
    }

    guideView(nodeArray, opacity = -1) {
        PrefabManage.loadPrefabByType(GameType.GuideMskNode, null, (prefab) => {
            // prefab.zIndex = 199
            let guideNodeArray = nodeArray
            let firstNode = guideNodeArray.shift()
            prefab.getComponent(NewGuideScript).setBindNode(firstNode, guideNodeArray)
            if (opacity != -1) {
                prefab.getComponent(NewGuideScript).setOpacityMaskNode(opacity)
            }
        })
    }

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

        let config = this.dataListTW[idx]
        // console.log(config)
        let danItem: TanweiItem = item.getComponent('TanweiItem')
        danItem.setViewDate(config)
    }

    // update (dt) {}
}