import { Vec3, Node, MeshRenderer, Color, RenderableComponent, Material, CapsuleCollider } from "cc";
import { ViewUtil } from "../../../../extensions/oops-plugin-framework/assets/core/utils/ViewUtil";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { PuppetModelComp } from "./model/PuppetModelComp";
import { PuppetViewComp } from "./view/PuppetViewComp";
import { PathFindComp } from "../common/ecs/path/PathFind";
import { PuppetPathSystem } from "./bll/PuppetPath";
import { PuppetSKinEnum } from "./model/PuppetEnum";

/**
 * 人物实体
 */
@ecs.register('Puppet')
export class Puppet extends ecs.Entity {


    PuppetModel!: PuppetModelComp;

    PathFind!: PathFindComp;

    PuppetView!: PuppetViewComp;


    protected init() {
        // 添加关卡数据组件
        this.addComponents<ecs.Comp>(PuppetModelComp);
    }


    destroy(): void {
        // 如果该组件对象是由ecs系统外部创建的，则不可回收，需要用户自己手动进行回收。
        this.remove(PuppetViewComp);
        super.destroy();
    }

    /** 加载关卡显示对象（cc.Component在创建后，添加到ECS框架中，使实体上任何一个ECS组件都可以通过 ECS API 获取到视图层对象 */
    load(parent: Node, color: string, pos: Vec3 = Vec3.ZERO, skinId: number = 1) {
        const node = ViewUtil.createPrefabNode(this.getSkinModelPath(skinId))
        var mv = node.getComponent(PuppetViewComp)!;
        if (!this.PuppetModel.withColider) node.getComponent(CapsuleCollider)?.destroy()
        this.add(mv);
        this.PuppetView.changeColor(color)
        node.parent = parent;
        node.setPosition(pos);
    }

    // loadWithOutColider(parent: Node, color: string, pos: Vec3 = Vec3.ZERO, skinId: number = 1){
    //     const node = ViewUtil.createPrefabNode(this.getSkinModelPath(skinId))
    //     var mv = node.getComponent(PuppetViewComp)!;
    //     node.getComponent(CapsuleCollider)?.destroy()
    //     this.add(mv);
    //     this.PuppetView.changeColor(color)
    //     node.parent = parent;
    //     node.setPosition(pos);
    // }

    changeSkin(skinId: number) {
        const { position, parent } = this.PuppetView.node
        const { curState } = this.PuppetView.animator
        this.remove(PuppetViewComp)
        this.load(parent, this.PuppetModel.color, position, skinId)
        this.PuppetView?.animator.playAni(curState)
    }

    getSkinModelPath(skinId: number) {
        if (skinId === PuppetSKinEnum.Balloon) {
            return "game/prefab/man_balloon";
        } else if (skinId === PuppetSKinEnum.Hat) {
            return "game/prefab/man_hat";
        } else if (skinId === PuppetSKinEnum.Cane) {
            return "game/prefab/man_cane";
        }
        return "game/prefab/man";
    }
}

export class EcsPuppetSystem extends ecs.System {
    constructor() {
        super();
        // this.add(new PuppetPathSystem())
    }
}