import { Vec3, Node, MeshRenderer, RenderableComponent, Color } 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 { VehicleModelComp } from "./model/VehicleModelComp";
import { VehicleViewComp } from "./view/VehicleViewComp";
import { VehicleSitSystem } from "./bll/VehicleCheck";

/**
 * 关卡实体
 * 1、生成关卡初始话数据
 */
@ecs.register('Vehicle')
export class Vehicle extends ecs.Entity {


    VehicleModel!: VehicleModelComp;

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

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

    /** 加载车辆显示对象（cc.Component在创建后，添加到ECS框架中，使实体上任何一个ECS组件都可以通过 ECS API 获取到视图层对象 */
    load(parent: Node,color:string,pos: Vec3 = Vec3.ZERO) {
        var node = ViewUtil.createPrefabNode("game/prefab/car");
          // 获取MeshRenderer组件
          let rendererComponet = node.getComponentInChildren(MeshRenderer) as RenderableComponent
          debugger
          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('mainColor', colorStr);
  
                  rendererComponet.material = materialIns
              }
          }
        var mv = node.getComponent(VehicleViewComp)!;
        this.add(mv);
        node.parent = parent;
        node.setPosition(pos);
    }
}

export class EcsVehicleSystem extends ecs.System {
    constructor() {
        super();

        this.add(new VehicleSitSystem());

    }
}