import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class ColorMappingService {
  private COLOR_MAPPING = [
    { code: '00', hex: '#3498db', name: '天空蓝' },
    { code: '01', hex: '#e74c3c', name: '热情红' },
    { code: '02', hex: '#2ecc71', name: '森林绿' },
    { code: '03', hex: '#f1c40f', name: '阳光黄' },
    { code: '04', hex: '#9b59b6', name: '紫罗兰' },
    { code: '05', hex: '#1abc9c', name: '碧波绿' },
    { code: '06', hex: '#e67e22', name: '落日橙' },
    { code: '07', hex: '#2c3e50', name: '午夜蓝' },
    { code: '08', hex: '#ecf0f1', name: '云朵白' },
    { code: '09', hex: '#e84393', name: '玫瑰粉' },
    { code: '0A', hex: '#ffeaa7', name: '奶油黄' },
    { code: '0B', hex: '#ffb6c1', name: '樱花粉' },
    { code: '0C', hex: '#98ff98', name: '薄荷绿' },
    { code: '0D', hex: '#b399d4', name: '薰衣草' },
    { code: '0E', hex: '#ff7f50', name: '珊瑚橙' },
    { code: '0F', hex: '#89cff0', name: '海洋蓝' },
    { code: '10', hex: '#ff9aa2', name: '西瓜红' },
    { code: '11', hex: '#40e0d0', name: '蓝绿色' },
    { code: '12', hex: '#f0e68c', name: '卡其黄' },
    { code: '13', hex: '#c8a2c8', name: '丁香紫' },
  ];

  constructor() {}

  getAllColors() {
    return this.COLOR_MAPPING;
  }

  getColorInfo(code: string) {
    const foundColor = this.COLOR_MAPPING.find(c => c.code === code);
    return foundColor || { code, hex: '#CCCCCC', name: '未知色' };
  }

  darkenColor(color: string, percent: number): string {
    const num = parseInt(color.replace('#', ''), 16);
    const amt = Math.round(2.55 * percent);
    const R = Math.max(0, (num >> 16) - amt);
    const G = Math.max(0, (num >> 8 & 0x00FF) - amt);
    const B = Math.max(0, (num & 0x0000FF) - amt);
    
    return '#' + (
      0x1000000 +
      (R < 255 ? R : 255) * 0x10000 +
      (G < 255 ? G : 255) * 0x100 +
      (B < 255 ? B : 255)
    ).toString(16).slice(1);
  }
}