// 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 TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import BagManager from "../Manager/BagManager";
import ShipuManager from "../Manager/ShipuManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";
import ShipuItem from "./ShipuItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShipuScript extends UiBase {
    @property(List)
    listSP: List = null; //秘书

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

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

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}
    start(): void {
        ShipuManager.initManager()


        this.scheduleOnce(() => {
            this.setList()
        }, 0.01)

        this.checkFinshTask()

        EventMgr.onEvent_custom(ryw_Event.exitShiPu, () => {
            this.node.removeFromParent()
            this.node.destroy()

        }, this)

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

    }

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

    checkFinshTask() {
        // let mainTaskInfo: any = TaskManager.getCurUnLockMainTaskInfo()
        // let mainId = mainTaskInfo.Id
        // if (mainId == MainTaskIdEnum.MainTask_529) {
        //     let id = 7
        //     let data = ShipuManager.getShipuUserDate(id)
        //     if (data.isLock == false || data.level > 0) {
        //         TaskManager.finishCurMainTask(MainTaskIdEnum.MainTask_529)
        //         TaskManager.setCurTask(MainTaskIdEnum.MainTask_529_2)
        //         let bagConfig = {
        //             goodId: 1721,
        //             goodNum: 1,
        //         }
        //         BagManager.addBagList(bagConfig)
        //     }
        // }

    }


    dataListSP = []

    setList() {
        let array = User.getShiPuArray()
        this.dataListSP = []
        for (let n = 0; n < array.length; n++) {
            this.dataListSP.push(array[n]);
        }
        this.listSP.numItems = this.dataListSP.length;


        this.guideShipu()

    }

    guideShipu() {
        let guideIndexArray = User.getGuideIndexArray()
        // if (guideIndexArray[0] == 1 && guideIndexArray[1] == 1 && guideIndexArray[2] == 0) {
        if (ShipuManager.getCurShipuCanUnLock(0) == 'all' && ShipuManager.getLockById(0)) {
            let node = this.node.getChildByName('guideNode')
            this.guideView([node], 100)
            // guideIndexArray[2] = 1
            // User.setGuideIndexArray(guideIndexArray)
        } else {
            let index = ShipuManager.getLockItemIndex()
            this.listSP.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.dataListSP[idx]
        // console.log(config)
        let danItem: ShipuItem = item.getComponent('ShipuItem')
        danItem.setViewDate(config)
    }

    // update (dt) {}
}