import { Color, Material, MeshRenderer, RenderableComponent, SkeletalAnimation, _decorator } from "cc";
import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { PuppetViewController } from "./PuppetViewController";
import { Puppet } from "../puppet";
import { PuppetViewAnimator } from "./PuppetViewAnimator";
import { adjustColor, adjustColor1, colorToHexString } from "../../common/utils/color";
import { MaterialPool } from "../../common/utils/MaterialPool";

const { ccclass, property } = _decorator;

/** 关卡显示组件 */
@ccclass('PuppetViewComp')                   // 定义为 Cocos Creator 组件
@ecs.register('PuppetView', false)           // 定义为 ECS 组件
export class PuppetViewComp extends CCComp {

    @property({ type: SkeletalAnimation, tooltip: '角色动画' })
    ske: SkeletalAnimation = null!;

    // /** 角色动画资源管理 */
    // loader: PuppetViewLoader = null!;
    /** 角色动画规则管理 */
    animator: PuppetViewAnimator = null!;
    /** 角色控制器 */
    controller: PuppetViewController = null!;

    /** 视图层逻辑代码分离演示 */
    onLoad() {
        const puppet = this.ent as Puppet;

        // this.loader = this.node.addComponent(PuppetViewLoader);
        // this.node.emit("load", puppet);

        this.animator = this.ske.getComponent(PuppetViewAnimator)!;
        this.animator.puppet = puppet;

        this.controller = this.node.addComponent(PuppetViewController);
        this.controller.puppet = puppet;

    }

    protected lateUpdate(dt: number): void {
        const on = (this.ent as Puppet)?.PathFind?.canReach
        this.switchOutLine(on)
    }

    switchOutLine(on: boolean) {
        const puppet = this.ent as Puppet;
        if(!puppet?.PuppetModel)return
        // 获取MeshRenderer组件
        let rendererComponets = this.node.getComponentsInChildren(MeshRenderer) as RenderableComponent[]
        rendererComponets.forEach((rendererComponet) => {
             // 获取材质实例
             let material = rendererComponet.sharedMaterial
             if (material) {
                const lineWidth = material.getProperty('lineWidth')
                if(on&&lineWidth===20)return
                if(!on&&lineWidth==5)return
                // const color = material.getProperty('mainColor')
                // if(!color)return
                 rendererComponet.material = MaterialPool.getManMaterialWithColorLineBold(puppet.PuppetModel.color)
             }
        })
       
    }

    changeColor(color: string) {
        // return
        // 获取MeshRenderer组件
        let rendererComponets = this.node.getComponentsInChildren(MeshRenderer) as RenderableComponent[]
        rendererComponets.forEach((rendererComponet) => {
            // 获取材质实例
            let materialIns = MaterialPool.getManMaterialWithColor(color)
            if (materialIns) {
                // 修改材质实例的颜色
                // const newColor = adjustColor(color,1,1.1)
                // materialIns.setProperty('mainColor', new Color(newColor));
                // materialIns.setProperty('shadeColor1', new Color(newColor));
                // materialIns.setProperty('shadeColor2', new Color(adjustColor(color,0.78,1.5)));
                rendererComponet.material = materialIns
            }
        })
    }



    reset() {
        this.animator?.destroy()
        this.controller?.destroy()
        this.node.destroy();
    }
}