import { Vec3, Node, MeshRenderer, Color, RenderableComponent, Material } 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";

/**
 * 人物实体
 */
@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,) {
        var node = ViewUtil.createPrefabNode("game/prefab/man");
        // 获取MeshRenderer组件
        let rendererComponet = node.getComponentInChildren(MeshRenderer) as RenderableComponent

        if (rendererComponet) {
            // 获取材质实例
            let materialIns = rendererComponet.material

            if (materialIns) {
                // 修改材质实例的颜色

                let colorStr;
                switch (color.toLowerCase()) {
                    case "red":
                        colorStr = Color.RED;
                        break;
                    case "blue":
                        colorStr = Color.BLUE;
                        break;
                    case "yellow":
                        colorStr = Color.YELLOW;
                        break;
                    default:
                        colorStr = Color.WHITE; // 默认颜色
                }

                materialIns.setProperty('albedo', colorStr);

                rendererComponet.material = materialIns
            }
        }
        var mv = node.getComponent(PuppetViewComp)!;
        this.add(mv);
        node.parent = parent;
        node.setPosition(pos);
    }
}

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