import { Animation, Vec3, _decorator, Node, v3, tween } from "cc";
import { AnimatorSkeletal } from "../../../../../extensions/oops-plugin-framework/assets/libs/animator/AnimatorSkeletal";
import { Puppet } from "../puppet";
import { AnimatorStateLogic } from "../../../../../extensions/oops-plugin-framework/assets/libs/animator/core/AnimatorStateLogic";
import { PuppetAnimatorType } from "../model/PuppetEnum";
import { PuppetStateRun } from "./animator/PuppetStateRun";
import { PuppetStateFly } from "./animator/PuppetStateFly";
import { MoveToPathComp } from "../../common/ecs/path/MoveToPath";
import { smc } from "../../common/SingletonModuleComp";
import { MoveToComp } from "../../common/ecs/position/MoveTo";

const { ccclass, property, requireComponent, disallowMultiple } = _decorator;

@ccclass("PuppetViewAnimator")
export class PuppetViewAnimator extends AnimatorSkeletal {
    /** 路径完成 */
    onRunPathComplete: Function = null!;

    /** 行为完成 */
    onRunComplete: Function = null!;

    /** 行为完成 */
    onScaleComplete: Function = null!;

    /** 角色对象 */
    puppet: Puppet = null!;

    curState = PuppetAnimatorType.Idle


    start() {
        super.start();
        // 动画状态机
        let asl: Map<string, AnimatorStateLogic> = new Map();
        asl.set(PuppetAnimatorType.Run, new PuppetStateRun(this.puppet));
        this.initArgs(asl);
    }

    playAnimation(animName: string, loop: boolean): void {
        super.playAnimation(animName, loop)
    }

    moveToPath(end: Vec3) {
        // const checkpoint = smc.initialize.account.checkpoint
        const moveToPath = this.puppet.add(MoveToPathComp)
        moveToPath.speed = 4
        moveToPath.paths = this.puppet.PathFind.path.concat([end])
        moveToPath.node = this.puppet.PuppetView.node
        moveToPath.ns = Node.NodeSpace.WORLD
        const forwardClone = this.puppet.PuppetView.node.forward.clone()
        moveToPath.onComplete = () => {
            // console.log('移动完成')
            this.playAni(PuppetAnimatorType.Idle)
            this.puppet.PuppetView.node.forward = forwardClone
            this.onRunPathComplete && this.onRunPathComplete()
        }
        this.playAni(PuppetAnimatorType.Run)
    }

    moveToTarget(end: Vec3 = v3(0, 0, 0)) {
        const moveTo = this.puppet.add(MoveToComp)
        moveTo.speed = 4
        moveTo.node = this.puppet.PuppetView.node
        moveTo.target = end
        moveTo.ns = Node.NodeSpace.WORLD
        const forwardClone = this.puppet.PuppetView.node.forward.clone()
        moveTo.onComplete = () => {
            this.playAni(PuppetAnimatorType.Idle)
            this.puppet.PuppetView.node.forward = forwardClone
            this.onRunComplete && this.onRunComplete()
        }
        this.playAni(PuppetAnimatorType.Run) 
    }

    playAni(state: PuppetAnimatorType) {
        switch (state) {
            case PuppetAnimatorType.Run:
                this.puppet.PuppetView.ske.play('run')
                this.curState = PuppetAnimatorType.Run
                break;
            case PuppetAnimatorType.Sit:
                this.puppet.PuppetView.ske.play('sit')
                this.curState = PuppetAnimatorType.Sit
                break;
            default:
                this.puppet.PuppetView.ske.play('idle')
                this.curState = PuppetAnimatorType.Idle
                break;
        }
    }

    toRight() {
        this.puppet.PuppetView.node.forward = v3(1, 0, 0)
    }

    toLeft() {
        this.puppet.PuppetView.node.forward = v3(-1, 0, 0)
    }


    toStation() {
        this.puppet.PuppetView.node.forward = v3(0, 0, -1)
    }

    scale() {
        this.puppet.PuppetView.node.scale = Vec3.ZERO
        tween(this.puppet.PuppetView.node)
            .to(1, { scale: new Vec3(1, 1, 1) })
            .call(() => {
                this.onScaleComplete && this.onScaleComplete()
            })
            .start();
    }

}

