import { _decorator, v3 } from "cc";
import { AnimatorSkeletal } from "../../../../../extensions/oops-plugin-framework/assets/libs/animator/AnimatorSkeletal";
import { AnimatorStateLogic } from "../../../../../extensions/oops-plugin-framework/assets/libs/animator/core/AnimatorStateLogic";
import { Vehicle } from "../Vehicle";
import { VehicleAnimatorType } from "../model/VehicleEnum";
import { VehicleStateOpenDoor } from "./animator/VehicleStateOpenDoor";
import { MoveToComp } from "../../common/ecs/position/MoveTo";
import { oops } from "../../../../../extensions/oops-plugin-framework/assets/core/Oops";


const { ccclass } = _decorator;

@ccclass("VehicleViewAnimator")
export class VehicleViewAnimator extends AnimatorSkeletal {
    /**关门完成完成 */
    onOpenDoorComplete: Function = null!;
    onLeaveComplete: Function = null!;
    onWaitComplete: Function = null!;
    onStationComplete: Function = null!;

    /** 角色对象 */
    vehicle: Vehicle = null!;


    timer = null

    start() {
        super.start();
        // // 动画状态机
        // let asl: Map<string, AnimatorStateLogic> = new Map();
        // asl.set(VehicleAnimatorType.OpenDoor, new VehicleStateOpenDoor(this.vehicle));
        // this.initArgs(asl);
    }

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

    moveToStation() {
        clearTimeout(this.timer)
        const moveTo = this.vehicle.add(MoveToComp, true)
        moveTo.speed = 3
        moveTo.node = this.vehicle.VehicleView.node
        moveTo.onComplete = this.onStationComplete
        moveTo.target = v3(0, 0, 0)
        oops.audio.playEffect('common/audio/bus_arrive_leave')
    }

    moveToWait() {

        this.timer = setTimeout(() => {
            if (this.vehicle) {
                const moveTo = this.vehicle.add(MoveToComp, true)
                moveTo.speed = 3
                moveTo.node = this.vehicle.VehicleView.node
                moveTo.target = v3(-3, 0, 0)
                moveTo.onComplete = this.onWaitComplete
            }

            clearTimeout(this.timer)
        }, 1000)
    }

    moveToLeave() {
        const moveTo = this.vehicle.add(MoveToComp, true)
        moveTo.speed = 3
        moveTo.node = this.vehicle.VehicleView.node
        moveTo.target = v3(10, 0, 0)
        moveTo.onComplete = this.onLeaveComplete
        oops.audio.playEffect('common/audio/bus_arrive_leave')

    }

    protected onDestroy(): void {
        clearTimeout(this.timer)
        super.destroy()
    }

}