// 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 Common5 from "../../Platform/th/Common5";
import { Npc_Direct } from "./ZaoCanDian";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZaoCanDianNpc extends cc.Component {

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

    @property(cc.Sprite)
    wuping: cc.Sprite = null;
    
    @property(cc.Label)
    numLabel: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    m_direct = null
    needStr: string;
    stateIndex: number;
    needNum: number;

    start() {


    }

    /**
     * 
     * @param direct 来的方向
     * @param position 停留位置
     * @param needStr 早餐名字
     * @param stateIndex 站立位置的index,站位
     * @param needNum 需要几个早餐
     */
    init(direct: Npc_Direct, position: cc.Vec2, needStr: string, stateIndex: number, needNum: number) {
        this.m_direct = direct
        this.needNode.active = false
        this.stateIndex = stateIndex
        this.setNeedNum(needNum)
        let size = cc.view.getVisibleSize();
        let x = size.width / 2 + this.node.getContentSize().width
        if (direct == Npc_Direct.LEFT) {
            this.node.setPosition(cc.v3(-x, 0))
        } else if (direct == Npc_Direct.RIGHT) {
            this.node.setPosition(cc.v3(x, 0))
            this.node.scaleX *= -1
        }
        //有位置说明需要物品,没位置就逛街
        if (position) {
            cc.tween(this.node)
                .sequence(
                    cc.tween().to(1, { position: cc.v3(position.x, 0) }),
                    cc.tween().to(0.5, { position: position }),
                    cc.tween().call(() => {
                        //随机需要的早餐
                        this.needStr = needStr
                        this.needNode.active = true
                        Common5.getSpriteFrameFromBundle('ZaoCanDian', `res/${this.needStr}`, this.wuping)
                        this.numLabel.string = `${this.getNeedNum()}`
                    })
                )
                .start()
        } else {
            cc.tween(this.node)
                .sequence(
                    cc.tween().to(1, { position: cc.v3(direct == Npc_Direct.LEFT ? x : -x, 0) }),
                    cc.tween().call(() => {
                        this.node.removeAllChildren()
                        this.node.destroy()
                    })
                )
                .start()
        }
    }

    getNeedStr() {
        return this.needStr
    }

    getStateIndex() {
        return this.stateIndex
    }

    setNeedNum(num) {
        this.needNum = num
        this.numLabel.string = `${num}`
    }

    getNeedNum() {
        return this.needNum
    }

    getZaoCanAfter() {
        this.needNode.active = false
        let size = cc.view.getVisibleSize();
        let x = size.width / 2 + this.node.getContentSize().width
        if (this.m_direct == Npc_Direct.LEFT) {
        } else if (this.m_direct == Npc_Direct.RIGHT) {
            x *= -1
        }
        cc.tween(this.node)
            .sequence(
                cc.tween().to(1, { position: cc.v3(x, this.node.position.y) }),
                cc.tween().call(() => {
                    this.node.removeAllChildren()
                    this.node.destroy()
                })
            )
            .start()
    }

    // update (dt) {}
}