// 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 Common5 from "../../Platform/th/Common5";
import List from "../../Platform/th/List";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import FangChanManager from "../Manager/FangChanManager";
import ZiChanManager from "../Manager/ZiChanManager";
import ZiChanItemNode from "./ZiChanItemNode";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZiChan extends cc.Component {

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

    @property(cc.Prefab)
    item: cc.Prefab = null;

    @property(List)
    listCJ: List = null;
    dataListCJ: any[];

    @property(cc.Label)
    curshouyi: cc.Label = null;
    @property(cc.Label)
    getnum: cc.Label = null;
    @property(cc.Label)
    shouyi: cc.Label = null;
    Config: { configId: number; icon: string; isNeedAd: boolean; descLab: string; shuliang: number; miaozhuan: number[]; jiesuoMoney: number[]; jiesuoBeiShu: number[]; str1: string; str2: string; unLockNum: number; unLockLevel: number; }[];


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.init()
        this.scheduleOnce(() => {
            this.setList()
        }, 0.01)
        EventMgr.onEvent_custom(ryw_Event.RefreshZiChan, () => {
            this.init()
        }, this)
        cc.sys.localStorage.setItem("资产引导", '1')
    }

    init() {
        this.Config = ZiChanManager.getManagerConfigs()
        let ziChanData = User.getZiChanData()
        // this.curshouyi.string = `当前收益:${ziChanData.curshouyi}`
        let getnum = 0
        let allnum = 0
        let beishu = 0
        let iconIndex = 0
        for (let i = 0; i < ziChanData.unLockNum.length; i++) {
            if (ziChanData.unLockNum[i] > 0) {
                getnum += ziChanData.unLockNum[i]
                beishu += this.Config[i].jiesuoBeiShu[ziChanData.unLockNum[i] - 1]
                iconIndex = i
            }
        }
        for (let i = 0; i < this.Config.length; i++) {
            allnum += this.Config[i].shuliang
        }
        this.getnum.string = `已获得资产:${getnum}/${allnum}`
        this.shouyi.string = `秒赚倍数:${beishu}倍`

        let arr = [2, 3, 4, 4]
        Common5.getSpriteFrameFromBundle("ZiChan", `res/big/${arr[iconIndex]}`, this.icon)
    }

    setList() {
        let array = this.Config
        this.dataListCJ = []
        for (let n = 0; n < array.length; n++) {
            this.dataListCJ.push(array[n]);
        }
        this.listCJ.numItems = this.dataListCJ.length;

        // let index = TanWeiManager.getLockItemIndex()
        this.listCJ.scrollTo(0)
    }
    onListMGridRender(item: cc.Node, idx: number) {

        let config = this.dataListCJ[idx]
        // console.log(config)
        let danItem: ZiChanItemNode = item.getComponent('ZiChanItemNode')
        danItem.init(config)
    }

    onTouchClose() {
        if (TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_514_2 && !cc.sys.localStorage.getItem("房产引导") && FangChanManager.getRoomState(3) != 1) {
            User.setFirstStepIndex(1102)
            EventMgr.emitEvent_custom(ryw_Event.My_Guide)
        }
        this.node.removeFromParent()
        this.node.destroy()
    }

    // update (dt) {}
}