export interface ColorMapping {
  hex: string | string[];
  isGradient?: boolean;
}

export const bambuLabColors: Record<string, ColorMapping> = {
  // PLA Basic Colors
  'Mistletoe Green': { hex: '#4F6359' },
  'Indigo Purple': { hex: '#482960' },
  'Black': { hex: '#000000' },
  'Jade White': { hex: '#F5F5F5' },
  'Gray': { hex: '#8C9091' },
  'Grey': { hex: '#8C9091' },
  'Red': { hex: '#C33F45' },
  'Hot Pink': { hex: '#F5547C' },
  'Cocoa Brown': { hex: '#6F5034' },
  'White': { hex: '#FFFFFF' },
  'Cotton Candy Cloud': { hex: ['#E7C1D5', '#8EC9E9'], isGradient: true },
  'Sunflower Yellow': { hex: '#FEC600' },
  'Yellow': { hex: '#FFD700' },
  'Magenta': { hex: '#FF00FF' },
  'Beige': { hex: '#F5DEB3' },
  'Cyan': { hex: '#00FFFF' },
  
  // PLA Matte Colors
  'Scarlet Red': { hex: '#FF2400' },
  'Mandarin Orange': { hex: '#FF8C00' },
  'Marine Blue': { hex: '#000080' },
  'Charcoal': { hex: '#36454F' },
  'Ivory White': { hex: '#FFFFF0' },
  
  // Additional colors from filamentcolors.xyz
  'Orange': { hex: '#FF7146' },
  'Blue': { hex: '#4F9CCC' },
  'Green': { hex: '#4F6359' },
  'Dark Green': { hex: '#656A4D' },
  'Alpine Green': { hex: '#4F6359' },
  'Dark Gray': { hex: '#616364' },
  'Dark Grey': { hex: '#616364' },
  'Blue Gray': { hex: '#647988' },
  'Blue Grey': { hex: '#647988' },
  'Translucent Orange': { hex: '#EF8E5B' },
  
  // Default fallback
  'Unknown': { hex: '#CCCCCC' }
};

export function getFilamentColor(colorName: string): ColorMapping {
  // First try exact match
  if (bambuLabColors[colorName]) {
    return bambuLabColors[colorName];
  }
  
  // Try case-insensitive match
  const lowerColorName = colorName.toLowerCase();
  const match = Object.keys(bambuLabColors).find(
    key => key.toLowerCase() === lowerColorName
  );
  
  if (match) {
    return bambuLabColors[match];
  }
  
  // Try partial match (e.g., "PLA Red" matches "Red")
  const partialMatch = Object.keys(bambuLabColors).find(
    key => colorName.includes(key) || key.includes(colorName)
  );
  
  if (partialMatch) {
    return bambuLabColors[partialMatch];
  }
  
  // Return default unknown color
  return bambuLabColors['Unknown'];
}

export function getColorStyle(colorMapping: ColorMapping): React.CSSProperties {
  if (colorMapping.isGradient && Array.isArray(colorMapping.hex)) {
    return {
      background: `linear-gradient(90deg, ${colorMapping.hex[0]} 0%, ${colorMapping.hex[1]} 100%)`
    };
  }
  
  return {
    backgroundColor: Array.isArray(colorMapping.hex) ? colorMapping.hex[0] : colorMapping.hex
  };
}

export function getContrastColor(hexColor: string): string {
  // Convert hex to RGB
  const hex = hexColor.replace('#', '');
  const r = parseInt(hex.substr(0, 2), 16);
  const g = parseInt(hex.substr(2, 2), 16);
  const b = parseInt(hex.substr(4, 2), 16);
  
  // Calculate relative luminance
  const luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
  
  // Return black or white based on luminance
  return luminance > 0.5 ? '#000000' : '#FFFFFF';
}