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

export const bambuLabColors: Record<string, ColorMapping> = {
  'Alpine Green Sparkle': { hex: '#3F5443' },
  'Apple Green': { hex: '#C6E188' },
  'Arctic Whisper': { hex: '#ECF7F8' },
  'Ash Grey': { hex: '#9B9EA0' },
  'Aurora Purple': { hex: '#285BB7' },
  'Azure': { hex: '#489FDF' },
  'Baby Blue': { hex: '#AEC3ED' },
  'Bambu Green': { hex: '#00AE42' },
  'Beige': { hex: '#F7E6DE' },
  'Black': { hex: '#000000' },
  'Black Walnut': { hex: '#4D4229' },
  'Blaze': { hex: '#E78390' },
  'Blue': { hex: '#0A2989' },
  'Blue Grey': { hex: '#5B6579' },
  'Blue Hawaii': { hex: '#739FE6' },
  'Blueberry Bubblegum': { hex: '#BADCF4' },
  'Bone White': { hex: '#C8C5B6' },
  'Bright Green': { hex: '#BDCF00' },
  'Brick Red': { hex: '#9F332A' },
  'Bronze': { hex: '#847D48' },
  'Brown': { hex: '#9D432C' },
  'Burgundy Red': { hex: '#951E23' },
  'Candy Green': { hex: '#408619' },
  'Candy Red': { hex: '#BB3A2E' },
  'Caramel': { hex: '#A4845C' },
  'Champagne': { hex: '#EBD0B1' },
  'Charcoal': { hex: '#000000' },
  'Cherry Pink': { hex: '#E9B6CC' },
  'Chocolate': { hex: '#4A3729' },
  'Classic Birch': { hex: '#E8D5B7' },
  'Classic Gold Sparkle': { hex: '#E4BD68' },
  'Clay Brown': { hex: '#8E621A' },
  'Clear': { hex: '#FAFAFA' },
  'Clear Black': { hex: '#5A5161' },
  'Cobalt Blue': { hex: '#0055B8' },
  'Cobalt Blue Metallic': { hex: '#39699E' },
  'Cocoa Brown': { hex: '#6F5034' },
  'Copper Brown Metallic': { hex: '#AA6443' },
  'Cotton Candy Cloud': { hex: '#E9E2EC' },
  'Cream': { hex: '#F3E0B8' },
  'Crimson Red Sparkle': { hex: '#792B36' },
  'Cyan': { hex: '#0086D6' },
  'Dark Blue': { hex: '#042F56' },
  'Dark Brown': { hex: '#7D6556' },
  'Dark Chocolate': { hex: '#4A3729' },
  'Dark Gray': { hex: '#555555' },
  'Dark Green': { hex: '#68724D' },
  'Dark Red': { hex: '#BB3D43' },
  'Dawn Radiance': { hex: '#C472A1' },
  'Desert Tan': { hex: '#E8DBB7' },
  'Dusk Glare': { hex: '#F6B790' },
  'Forest Green': { hex: '#415520' },
  'Frozen': { hex: '#A6DEF3' },
  'Gilded Rose': { hex: '#ED982C' },
  'Glow Blue': { hex: '#7AC0E9' },
  'Glow Green': { hex: '#A1FFAC' },
  'Glow Orange': { hex: '#FF9D5B' },
  'Glow Pink': { hex: '#F17B8F' },
  'Glow Yellow': { hex: '#F8FF80' },
  'Gold': { hex: '#E4BD68' },
  'Gray': { hex: '#8E9089' },
  'Green': { hex: '#3B665E' },
  'Hot Pink': { hex: '#F5547D' },
  'Ice Blue': { hex: '#A3D8E1' },
  'Indigo Blue': { hex: '#324585' },
  'Indigo Purple': { hex: '#482A60' },
  'Iridium Gold Metallic': { hex: '#B39B84' },
  'Iris Purple': { hex: '#69398E' },
  'Iron Gray Metallic': { hex: '#6B6C6F' },
  'IronGray Metallic': { hex: '#6B6C6F' },
  'Ivory White': { hex: '#FFFFFF' },
  'Jade White': { hex: '#FFFFFF' },
  'Jeans Blue': { hex: '#6E88BC' },
  'Lake Blue': { hex: '#4672E4' },
  'Latte Brown': { hex: '#D3B7A7' },
  'Lava Gray': { hex: '#4D5054' },
  'Lavender': { hex: '#B5AAD5' },
  'Lemon Yellow': { hex: '#F7D959' },
  'Light Blue': { hex: '#61B0FF' },
  'Light Cyan': { hex: '#B9E3DF' },
  'Light Gray': { hex: '#D0D2D4' },
  'Light Jade': { hex: '#A4D6AD' },
  'Lilac Purple': { hex: '#AE96D4' },
  'Lime': { hex: '#C5ED48' },
  'Lime Green': { hex: '#8EE43D' },
  'Magenta': { hex: '#EC008C' },
  'Malachite Green': { hex: '#16B08E' },
  'Mandarin Orange': { hex: '#F99963' },
  'Marine Blue': { hex: '#0078BF' },
  'Maroon Red': { hex: '#0A2989' },
  'Matcha Green': { hex: '#5C9748' },
  'Mellow Yellow': { hex: '#EFDCAA' },
  'Midnight Blaze': { hex: '#0047BB' },
  'Mint': { hex: '#A5DAB7' },
  'Mint Lime': { hex: '#BAF382' },
  'Mistletoe Green': { hex: '#3F8E43' },
  'Nardo Gray': { hex: '#747474' },
  'Navy Blue': { hex: '#0C2340' },
  'Nebulae': { hex: '#424379' },
  'Nebulane': { hex: '#424379' },
  'Neon City': { hex: '#0047BB' },
  'Neon Green': { hex: '#ABFF1E' },
  'Neon Orange': { hex: '#F68A1B' },
  'Ochre Yellow': { hex: '#BC8B39' },
  'Ocean to Meadow': { hex: '#A1E4CA' },
  'Olive': { hex: '#748C45' },
  'Onyx Black Sparkle': { hex: '#2D2B28' },
  'Orange': { hex: '#FF6A13' },
  'Oxide Green Metallic': { hex: '#1D7C6A' },
  'Peanut Brown': { hex: '#7E5A1F' },
  'Pink': { hex: '#F55A74' },
  'Pink Citrus': { hex: '#F8C4BC' },
  'Plum': { hex: '#851A52' },
  'Pumpkin Orange': { hex: '#FF8E16' },
  'Purple': { hex: '#5E43B7' },
  'Red': { hex: '#C12E1F' },
  'Red Granite': { hex: '#AD4E38' },
  'Rose Gold': { hex: '#B29593' },
  'Rosewood': { hex: '#472A22' },
  'Royal Blue': { hex: '#2842AD' },
  'Royal Purple Sparkle': { hex: '#483D8B' },
  'Sakura Pink': { hex: '#E8AFCF' },
  'Scarlet Red': { hex: '#DE4343' },
  'Silver': { hex: '#A6A9AA' },
  'Sky Blue': { hex: '#73B2E5' },
  'Slate Gray Sparkle': { hex: '#8E9089' },
  'Solar Breeze': { hex: '#F3D9D5' },
  'South Beach': { hex: '#468791' },
  'Sunflower Yellow': { hex: '#FEC601' },
  'Tangerine Yellow': { hex: '#FFC72C' },
  'Teal': { hex: '#77EDD7' },
  'Terracotta': { hex: '#A25A37' },
  'Titan Gray': { hex: '#606367' },
  'Transparent': { hex: '#FFFFFF' },
  'Turquoise': { hex: '#00B1B7' },
  'Velvet Eclipse': { hex: '#000000' },
  'Violet Purple': { hex: '#583061' },
  'White': { hex: '#FFFFFF' },
  'White Marble': { hex: '#F7F3F0' },
  'White Oak': { hex: '#D2CCA2' },
  'Yellow': { hex: '#F4EE2A' },

  // ABS Colors
  'ABS GF Yellow': { hex: '#FDD835' },
  'ABS GF Orange': { hex: '#F48438' },
  'ABS Azure': { hex: '#489FDF' },
  'ABS Olive': { hex: '#748C45' },
  'ABS Blue': { hex: '#0A2989' },
  'ABS Tangerine Yellow': { hex: '#FFC72C' },
  'ABS Navy Blue': { hex: '#0C2340' },
  'ABS Orange': { hex: '#FF6A13' },
  'ABS Bambu Green': { hex: '#00AE42' },
  'ABS Red': { hex: '#C12E1F' },
  'ABS White': { hex: '#FFFFFF' },
  'ABS Black': { hex: '#000000' },
  'ABS Silver': { hex: '#A6A9AA' },

  // PETG Translucent Colors
  'Translucent Gray': { hex: '#B8B8B8' },
  'Translucent Brown': { hex: '#C89A74' },
  'Translucent Purple': { hex: '#C5A8D8' },
  'Translucent Orange': { hex: '#FFB380' },
  'Translucent Olive': { hex: '#A4B885' },
  'Translucent Pink': { hex: '#F9B8D0' },
  'Translucent Light Blue': { hex: '#A8D8F0' },
  'Translucent Tea': { hex: '#D9C7A8' },

  // PLA Matte Colors
  'Matte Ivory White': { hex: '#FFFFF0' },
  'Matte Charcoal': { hex: '#333333' },
  'Matte Scarlet Red': { hex: '#DE4343' },
  'Matte Marine Blue': { hex: '#0078BF' },
  'Matte Mandarin Orange': { hex: '#F99963' },
  'Matte Ash Gray': { hex: '#9B9EA0' },
  'Matte Desert Tan': { hex: '#E8DBB7' },
  'Matte Nardo Gray': { hex: '#747474' },
  'Matte Apple Green': { hex: '#C6E188' },
  'Matte Bone White': { hex: '#C8C5B6' },
  'Matte Caramel': { hex: '#A4845C' },
  'Matte Dark Blue': { hex: '#042F56' },
  'Matte Dark Brown': { hex: '#7D6556' },
  'Matte Dark Chocolate': { hex: '#4A3729' },
  'Matte Dark Green': { hex: '#68724D' },
  'Matte Dark Red': { hex: '#BB3D43' },
  'Matte Grass Green': { hex: '#7CB342' },
  'Matte Ice Blue': { hex: '#A3D8E1' },
  'Matte Lemon Yellow': { hex: '#F7D959' },
  'Matte Lilac Purple': { hex: '#AE96D4' },
  'Matte Plum': { hex: '#851A52' },
  'Matte Sakura Pink': { hex: '#E8AFCF' },
  'Matte Sky Blue': { hex: '#73B2E5' },
  'Matte Latte Brown': { hex: '#D3B7A7' },
  'Matte Terracotta': { hex: '#A25A37' },

  // PLA Silk Multi-Color
  'Silk Aurora Purple': { hex: ['#7F3696', '#006EC9'], isGradient: true },
  'Silk Phantom Blue': { hex: ['#00629B', '#000000'], isGradient: true },
  'Silk Mystic Magenta': { hex: ['#720062', '#3A913F'], isGradient: true },

  // TPU Colors
  'Flesh': { hex: '#E8C4A2' },
  'Grape Jelly': { hex: '#6B2D75' },
  'Crystal Blue': { hex: '#5BC0EB' },
  'Quicksilver': { hex: '#A6A9AA' },
  'TPU 95A HF Yellow': { hex: '#F3E600' },

  // 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
  };
}