// 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 Common5 from "../../Platform/th/Common5";
import ZaoCanManager from "../Manager/ZaoCanManager";
import ZaoCanDianNpc from "./ZaoCanDianNpc";

const { ccclass, property } = cc._decorator;

export enum Npc_Direct {
    LEFT,
    RIGHT,
}

type Npc_Position_State = {
    isHaveNpc: boolean
}

@ccclass
export default class ZaoCanDian extends cc.Component {

    @property([cc.Prefab])
    npc_Prefabs: cc.Prefab[] = [];

    @property([cc.Node])
    npc_Positions: cc.Node[] = [];

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

    npcNodes: cc.Node[] = [];
    lastDirect: Npc_Direct = Npc_Direct.RIGHT;

    npc_Position_States: Npc_Position_State[] = [{ isHaveNpc: false }, { isHaveNpc: false }, { isHaveNpc: false }]

    ZaoCanArr = []


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start() {
        //NPC,头上带气泡

        //点击,点击完自动上架早餐

        //刷新早餐种类
        this.refreshZaoCan()
        EventMgr.onEvent_custom(ryw_Event.levelUpChange, () => {
            //人物升级会变更早餐
            this.refreshZaoCan()
        }, this)
    }

    refreshZaoCan() {
        let config = ZaoCanManager.getManagerConfigDate()
        for (let i = 0; i < config.length; i++) {
            let name = ZaoCanManager.getCurNameById(config[i])
            this.ZaoCanArr[i] = name
        }

        for (let i = this.npcNodes.length - 1; i >= 0; i--) {
            this.npcNodes[i].removeFromParent()
            this.npcNodes[i].destroy()
            this.npc_Position_States[i].isHaveNpc = false
        }
        this.npcNodes = []
    }

    runNpc() {
        let npc = cc.instantiate(this.npc_Prefabs[Common5.getRandomNumber(0, this.npc_Prefabs.length - 1)])
        this.npc_parent.addChild(npc)
        let position = this.getNpcEndPosition()
        let curDirect = this.lastDirect == Npc_Direct.LEFT ? Npc_Direct.RIGHT : Npc_Direct.LEFT
        let index = Common5.getRandomNumber(0, this.ZaoCanArr.length - 1)
        npc.getComponent(ZaoCanDianNpc).init(curDirect, position, this.ZaoCanArr[index], index, 1)
        this.lastDirect = curDirect
        this.npcNodes.push(npc)
    }

    getNpcEndPosition() {
        let position
        for (let i = 0; i < this.npc_Position_States.length; i++) {
            let state = this.npc_Position_States[i]
            if (!state.isHaveNpc) {
                position = this.npc_Positions[i].getPosition()
                this.npc_Position_States[i].isHaveNpc = true
                break
            }
        }
        return position
    }

    onTouchZaoCan(event, custom) {
        let _npc = this.getNpcByNeedStr(custom)
        if (!_npc) { //没有一个npc需要这个早餐直接返回
            //所属物品摊位放大缩小
            return
        }
        let _child
        let _position
        // if (custom == "馒头") {
        //     let tanwei = this.node.getChildByName("馒头摊位")
        //     let children = tanwei.children
        //     for (const child of children) {
        //         if (child.active) {
        //             _child = child
        //             break
        //         }
        //     }
        //     _position = tanwei.convertToNodeSpaceAR(_npc.parent.convertToWorldSpaceAR(_npc.getPosition()))
        // } else if (custom == "鸡蛋") {
        //     let tanwei = this.node.getChildByName("鸡蛋摊位")
        //     let children = tanwei.children
        //     for (const child of children) {
        //         if (child.active) {
        //             _child = child
        //             break
        //         }
        //     }
        //     _position = tanwei.convertToNodeSpaceAR(_npc.parent.convertToWorldSpaceAR(_npc.getPosition()))
        // }

        let tanwei = this.node.getChildByName(custom + "摊位")
        let children = tanwei.children
        for (const child of children) {
            if (child.active) {
                _child = child
                break
            }
        }
        _position = tanwei.convertToNodeSpaceAR(_npc.parent.convertToWorldSpaceAR(_npc.getPosition()))

        if (_child) {
            cc.tween(_child)
                .sequence(
                    cc.tween().to(0.5, { position: _position }),
                    cc.tween().call(() => {
                        let com = _npc.getComponent(ZaoCanDianNpc)
                        let needNum = com.getNeedNum() - 1
                        com.setNeedNum(needNum)
                        if (needNum == 0) {
                            this.npc_Position_States[com.getStateIndex()].isHaveNpc = false
                            com.getZaoCanAfter()
                        }
                    })
                )
                .start()
        }
    }

    getNpcByNeedStr(needStr) {
        let _npc = null
        for (const npc of this.npcNodes) {
            if (npc.getComponent(ZaoCanDianNpc).getNeedStr() == needStr) {
                _npc = npc
                break
            }
        }
        return _npc
    }

    onTouchYeWu() {

    }

    onTouchShengJi() {

    }

    // update (dt) {}
}