import { Color, MeshRenderer, RenderableComponent, SkeletalAnimation, _decorator } from "cc";
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { VehicleViewController } from "./VehicleViewController";
import { Vehicle } from "../Vehicle";
import { VehicleViewAnimator } from "./VehicleViewAnimator";
import { smc } from "../../common/SingletonModuleComp";
import { Puppet } from "../../puppet/puppet";
import { PuppetModelComp } from "../../puppet/model/PuppetModelComp";
import { PuppetAnimatorType } from "../../puppet/model/PuppetEnum";
import { adjustColor } from "../../common/utils/color";

const { ccclass, property } = _decorator;

/** 车辆显示组件 */
@ccclass('VehicleViewComp')                   // 定义为 Cocos Creator 组件
@ecs.register('VehicleView', false)           // 定义为 ECS 组件
export class VehicleViewComp extends CCComp {

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

    // /** 车辆动画资源管理 */
    // loader: VehicleViewLoader = null!;
    /** 车辆动画规则管理 */
    animator: VehicleViewAnimator = null!;
    /** 车辆控制器 */
    controller: VehicleViewController = null!;

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

        // this.loader = this.node.addComponent(VehicleViewLoader);
        // this.node.emit("load", vehicle);

        this.animator = this.ske.getComponent(VehicleViewAnimator)!;
        this.animator.vehicle = vehicle;

        this.controller = this.node.addComponent(VehicleViewController);
        this.controller.vehicle = vehicle;
    }

    // switchOutLine(on: boolean) {
    //     // 获取MeshRenderer组件
    //     let rendererComponet = this.node.getComponentInChildren(MeshRenderer) as RenderableComponent

    //     if (rendererComponet) {
    //         // 获取材质实例
    //         let materialIns = rendererComponet.material
    //         console.log(materialIns)
    //         if (materialIns) {
    //             materialIns.passes[0].defines['USE_OUTLINE_PASS'] = on
    //             rendererComponet.material = materialIns
    //         }
    //     }
    // }

    changeColor(color: string) {
        // 获取MeshRenderer组件
        let rendererComponets = this.node.getComponentsInChildren(MeshRenderer) as RenderableComponent[]
        rendererComponets.forEach((rendererComponet) => {
            // 获取材质实例
            // let materialIns = rendererComponet.material
            // if (materialIns) {
            //     // 修改材质实例的颜色
            //     materialIns.setProperty('mainColor', new Color(color));
            //     rendererComponet.material = materialIns

            // }
            let materials = rendererComponet.materials; // 获取所有材质（如果是多材质情况）
            for (let i = 0; i < materials.length; i++) {
                let material = materials[i];
                if (materials.length > 6) {
                    if (material && [0,1,3, 7].includes(i)) {
                        // 修改特定材质实例的颜色
                        material.setProperty('mainColor', new Color(color));
                        materials[i] = material; // 更新材质
                    }
                } else {
                    // 修改特定材质实例的颜色
                    material.setProperty('mainColor', new Color(color));
                    materials[i] = material; // 更新材质
                    break
                }

            }
            rendererComponet.materials = materials; // 重新赋值更新后的材质数组
        })
    }



    createPuppet() {
        const v = this.ent as Vehicle;
        const account = smc.initialize.account
        const puppet = ecs.getEntity<Puppet>(Puppet)
        const puppetModel = puppet.get(PuppetModelComp)
        puppetModel.color = v.VehicleModel.color
        puppetModel.withColider = false
        puppet.load(this.node, v.VehicleModel.color, v.VehicleModel.sitPos[v.children.size], account.AccountModel.skin);
        v.addChild(puppet)
        puppet.PuppetView.animator.toRight()
        puppet.PuppetView.animator.playAni(PuppetAnimatorType.Sit)
        puppet.PuppetView.animator.onScaleComplete = () => {
            // console.log("结果",!checkpoint.CheckpointModel.puppets.some(val => val.PuppetModel?.color === this.vehicle.VehicleModel.color))
            // console.log(checkpoint.CheckpointModel.puppets) // ||!account.checkpoint.CheckpointModel.puppets.some(val => val.PuppetModel?.color === v.VehicleModel.color)
            if (v.children.size >= v.VehicleModel.capcity)
                v.VehicleModel.ready = true
        }
        puppet.PuppetView.animator.scale()
    }


    reset() {
        this.node.destroy();
    }
}