// 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";

enum MYDIRECT {
    LEFT,
    RIGHT
}

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;

    @property(sp.Skeleton)
    npcSpine: sp.Skeleton = null;

    @property({ type: cc.Enum(MYDIRECT), displayName: '人物自身方向' })
    my_direct: MYDIRECT = MYDIRECT.RIGHT

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

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

    chatConfig = [
        { effectUrl: 'DH/MainHall/sound/味道怎么怪怪的!', delayTime: 3.54, str: '味道怎么怪怪的!' },
        { effectUrl: 'DH/MainHall/sound/听说他老婆跑了...', delayTime: 1.31, str: '听说他老婆跑了...' },
        { effectUrl: 'DH/MainHall/sound/能有叉烧包就好了!', delayTime: 1.32, str: '能有叉烧包就好了!' },
        { effectUrl: 'DH/MainHall/sound/呦,长得蛮俊俏!', delayTime: 2.85, str: '呦,长得蛮俊俏!' },
        { effectUrl: 'DH/MainHall/sound/一天不吃,浑身难受!', delayTime: 3.63, str: '一天不吃,浑身难受!' },
        { effectUrl: 'DH/MainHall/sound/味道不错,下次还来!', delayTime: 2.8, str: '味道不错,下次还来!' },
        { effectUrl: 'DH/MainHall/sound/价格太贵了,奸商!', delayTime: 2.31, str: '价格太贵了,奸商!' },
        { effectUrl: 'DH/MainHall/sound/快快快,要迟到了!', delayTime: 2.31, str: '快快快,要迟到了!' },
    ]

    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) {
            if (this.my_direct == MYDIRECT.LEFT) {
                this.npcSpine.node.scaleX = -1
                // this.numLabel.node.setScale(cc.v3(-1, 1, 1))
            }
            this.node.setPosition(cc.v3(-x, 100))
        } else if (direct == Npc_Direct.RIGHT) {
            if (this.my_direct == MYDIRECT.LEFT) {
                this.npcSpine.node.scaleX = 1
            } else {
                this.npcSpine.node.scaleX = -1
                // this.numLabel.node.setScale(cc.v3(-1, 1, 1))
            }
            this.node.setPosition(cc.v3(x, 100))
        }

        //有的动画锚点在侧边......
        // this.npcSpine.node.scaleX *= 0.9
        // this.npcSpine.node.scaleY *= 0.9
        if (this.npcSpine.node.scaleX < 0) {
            this.npcSpine.node.setPosition(cc.v2(this.npcSpine.node.position.x * this.npcSpine.node.scaleX, this.npcSpine.node.position.y))
        }
        let scaleX = this.node.scaleX
        let scaleY = this.node.scaleY
        this.npcSpine.setAnimation(0, "走路", true)
        //有位置说明需要物品,没位置就逛街
        if (position) {
            cc.tween(this.node)
                .sequence(
                    cc.tween().set({ scaleX: scaleX * 0.7, scaleY: scaleY * 0.7 }),
                    cc.tween().to(2, { position: cc.v3(position.x, 100) }),
                    cc.tween().call(() => {
                        this.npcSpine.setAnimation(0, "待机", true)
                    }),
                    cc.tween().parallel(
                        cc.tween().to(0.5, { scaleX: scaleX, scaleY: scaleY }),
                        cc.tween().to(0.5, { position: position }),
                    ),
                    cc.tween().call(() => {
                        //随机需要的早餐
                        this.npcSpine.setAnimation(0, "待机", true)
                        this.needStr = needStr
                        this.needNode.active = true
                        Common5.getSpriteFrameFromBundle('ZaoCanDian', `res/icon/${this.needStr}`, this.wuping)
                        this.numLabel.string = `${this.getNeedNum()}`
                    })
                )
                .start()
        } else {
            cc.tween(this.node)
                .sequence(
                    cc.tween().to(5, { 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

    }

    setNeedNumLabel() {
        //为什么不用this.needNum去设置
        // 这里是因为判断npc需要的时候this.needNum已经为0了,但是物品移动的动效还没播完,会导致npc都走了,还有物品往他头上飞,其实每进一次这个函数就是物品移过去一个了
        this.numLabel.string = `${Number(this.numLabel.string) - 1}`
    }

    getNeedNumLabel() {
        return Number(this.numLabel.string)
    }

    getNeedNum() {
        return this.needNum
    }

    getZaoCanAfter() {

        this.npcSpine.setAnimation(0, "摆手", false)

        let qipao = ""
        if (this.getStateIndex() == 1) {
            qipao = "气泡右边"
        } else {
            qipao = "气泡左边"
        }
        this.node.getChildByName(qipao).active = true
        let chat = this.chatConfig[Common5.getRandomNumber(0, this.chatConfig.length - 1)]
        this.node.getChildByName(qipao).getChildByName("str").getComponent(cc.Label).string = `${chat.str}`
        this.scheduleOnce(() => {
            // this.npcSpine.setCompleteListener(() => {
            //     this.npcSpine.setCompleteListener(null)
            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
            }
            this.npcSpine.setAnimation(0, "走路", true)
            this.node.setSiblingIndex(0)
            let scaleX = this.node.scaleX
            let scaleY = this.node.scaleY

            let posY = this.node.position.y + 100
            cc.tween(this.node)
                .sequence(
                    cc.tween().parallel(
                        cc.tween().to(0.5, { scaleX: scaleX * 0.7, scaleY: scaleY * 0.7 }),
                        cc.tween().to(0.5, { position: cc.v3(this.node.position.x, posY) }),
                    ),
                    cc.tween().to(1, { position: cc.v3(x, posY) }),
                    cc.tween().call(() => {
                        this.node.removeAllChildren()
                        this.node.destroy()
                    })
                )
                .start()
            cc.tween(this.node.getChildByName(qipao))
                .to(0.5, { scaleX: 1.2, scaleY: 1.2 })
                .start()
            // })
        }, 1)


        this.needNode.active = false
    }

    getZaoCanWorldPosition() {
        return this.needNode.parent.convertToWorldSpaceAR(this.needNode.getPosition())
    }

    runMoney(money) {
        let label = cc.instantiate(this.node.getChildByName("money"))
        label.active = true
        label.parent = this.node
        if (money && money > 0) {
            label.getComponent(cc.Label).string = "+" + Common5.getNumberChangeHanzi(money, '1', 1)
        }
        cc.tween(label)
            .delay(0.2)
            .by(1, { y: 20, opacity: 0 })
            .call(() => {
                label.removeFromParent()
                label.destroy()
            })
            .start()
    }

    // update (dt) {}
}