// 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 UiBase from "../GameBase/UiBase";
import TaskManager, { MainTaskIdEnum } from "../JuQingChat/TaskManager";
import FangChanManager from "../Manager/FangChanManager";
import UserManager from "../Manager/UserManager";
import NewGuideScript from "../NewGuide/NewGuideScript";
import PrefabManage, { GameType } from "../PrefabManager/PrefabManage";

const { ccclass, property } = cc._decorator;

@ccclass
export default class FangChanGame extends UiBase {

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


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


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

    @property(cc.Node)
    itemNode: cc.Node = null;
    @property(cc.Node)
    contentNode: cc.Node = null;
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        this.refrehView()
        EventMgr.onEvent_custom(ryw_Event.FangChanIncome, () => {
            this.refrehView()
        }, this);

        this.createItem()
        cc.sys.localStorage.setItem("房产引导", '1')
    }

    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)
            }
        })
    }

    refrehView() {
        let dangqian = FangChanManager.getCurIncomeMoney()
        let mairu = FangChanManager.getAllBuyMoney()
        let shouyi = FangChanManager.getAllInComeMoney()

        this.dangqianLabel.string = '当前价:' + Common5.getNumberChangeHanzi(dangqian) + ''
        this.mairuLabel.string = '买入价:' + Common5.getNumberChangeHanzi(mairu) + ''
        this.shouyiLabel.string = '收益:' + Common5.getNumberChangeHanzi(shouyi) + ''
        if (shouyi > 0) {
            this.node.getChildByName('shouyiBtn').getChildByName('红点').active = true
        } else {
            this.node.getChildByName('shouyiBtn').getChildByName('红点').active = false
        }

    }

    createItem() {
        let configs = FangChanManager.getManagerConfigs()
        for (let i = 0; i < configs.length; i++) {
            let config = configs[i]
            this.scheduleOnce(() => {
                let node = cc.instantiate(this.itemNode)
                node.getComponent('FangChanItem').setDateView(config)
                node.active = true
                node.opacity = 255
                node.setPosition(cc.v2(0, 0))
                this.contentNode.addChild(node)
                if (i == configs.length - 1 && TaskManager.getCurUnLockMainTaskId() == MainTaskIdEnum.MainTask_514_2 && FangChanManager.getRoomState(3) == 0) {
                    let childPosition = this.contentNode.children[2].position
                    this.scrollview.scrollToOffset(cc.v2(0, -childPosition.y))
                    let guide = this.contentNode.children[3].getChildByName("buyBtn")
                    this.guideView([guide], 100)
                    User.setFirstStepIndex(-1)
                }
            }, 0.05 + 0.05 * i)


        }
    }




    clickShouyiEvent(event) {
        let target = event.target
        let allNum = FangChanManager.getAllInComeMoney()
        if (allNum > 0) {
            UserManager.addMoney(allNum, target)
        }

        FangChanManager.resetAllInComeMoney()

    }
    // update (dt) {}
}