import { _decorator, Component, Material, Color, EffectAsset } from 'cc';
import { adjustColor } from './color';
const { ccclass, property } = _decorator;

@ccclass('MaterialPool')
export class MaterialPool extends Component {

    static materialMap = new Map<string, Material>();
    static materialBoldMap = new Map<string, Material>();

    static getManMaterialWithColor(color: string): Material {
        // 将颜色转换为字符串作为键值
        const colorKey = color.toUpperCase();
        
        // 检查材质池中是否已有该颜色的材质
        if (this.materialMap.has(colorKey)) {
            return this.materialMap.get(colorKey);
        } else {
            // 如果没有，创建一个新的材质并设置其颜色
            let newMaterial = new Material();
            newMaterial.initialize({
                effectName: 'builtin-toon', // 可以根据需要选择不同的effect
                defines: {
                    USE_OUTLINE_PASS: true,
                    USE_INSTANCING: true
                }
            });
            newMaterial.setProperty('lineWidth', 5); // 设置线宽度为 5
            const newColor = adjustColor(color,1,1.1)
            newMaterial.setProperty('mainColor', new Color(newColor));
            newMaterial.setProperty('shadeColor1', new Color(newColor));
            newMaterial.setProperty('shadeColor2', new Color(adjustColor(color,0.78,1.5)));
            newMaterial.setProperty('baseStep', 0.9);
            newMaterial.setProperty('baseFeather', 0);
            newMaterial.setProperty('shadeStep', 0.6);
            newMaterial.setProperty('shadeFeather', 0.2);
            // 将新创建的材质保存到材质池
            this.materialMap.set(colorKey, newMaterial);

            return newMaterial;
        }
    }
    static getManMaterialWithColorLineBold(color: string): Material {
        // 将颜色转换为字符串作为键值
        const colorKey = color.toUpperCase();
        
        // 检查材质池中是否已有该颜色的材质
        if (this.materialBoldMap.has(colorKey)) {
            return this.materialBoldMap.get(colorKey);
        } else {
            // 如果没有，创建一个新的材质并设置其颜色
            let newMaterial = new Material();
            newMaterial.initialize({
                effectName: 'builtin-toon', // 可以根据需要选择不同的effect
                defines: {
                    USE_OUTLINE_PASS: true,
                    USE_INSTANCING: true
                }
            });
            newMaterial.setProperty('lineWidth', 20); 
            const newColor = adjustColor(color,1,1.1)
            newMaterial.setProperty('mainColor', new Color(newColor));
            newMaterial.setProperty('shadeColor1', new Color(newColor));
            newMaterial.setProperty('shadeColor2', new Color(adjustColor(color,0.78,1.5)));
            newMaterial.setProperty('baseStep', 0.9);
            newMaterial.setProperty('baseFeather', 0);
            newMaterial.setProperty('shadeStep', 0.6);
            newMaterial.setProperty('shadeFeather', 0.2);
            // 将新创建的材质保存到材质池
            this.materialBoldMap.set(colorKey, newMaterial);

            return newMaterial;
        }
    }
}