import { Temperature } from '../../types';

/**
 * Orton Cone temperature reference data (self-supporting, end point temperatures)
 * Based on standard firing rates for ceramics in Fahrenheit
 */
export interface ConeData {
  cone: string;
  fahrenheit: number;
  celsius: number;
  description: string;
}

export const CONE_TEMPERATURE_CHART: ConeData[] = [
  { cone: '022', fahrenheit: 1087, celsius: 586, description: 'Very low fire - overglaze' },
  { cone: '021', fahrenheit: 1112, celsius: 600, description: 'Very low fire' },
  { cone: '020', fahrenheit: 1159, celsius: 626, description: 'Low fire' },
  { cone: '019', fahrenheit: 1213, celsius: 656, description: 'Low fire' },
  { cone: '018', fahrenheit: 1267, celsius: 686, description: 'Low fire' },
  { cone: '017', fahrenheit: 1301, celsius: 705, description: 'Low fire' },
  { cone: '016', fahrenheit: 1368, celsius: 742, description: 'Low fire' },
  { cone: '015', fahrenheit: 1436, celsius: 780, description: 'Low fire' },
  { cone: '014', fahrenheit: 1485, celsius: 807, description: 'Low fire' },
  { cone: '013', fahrenheit: 1539, celsius: 837, description: 'Low fire' },
  { cone: '012', fahrenheit: 1582, celsius: 861, description: 'Low fire' },
  { cone: '011', fahrenheit: 1641, celsius: 894, description: 'Low fire' },
  { cone: '010', fahrenheit: 1657, celsius: 903, description: 'Low fire' },
  { cone: '09', fahrenheit: 1688, celsius: 920, description: 'Low fire' },
  { cone: '08', fahrenheit: 1728, celsius: 942, description: 'Low fire' },
  { cone: '07', fahrenheit: 1789, celsius: 976, description: 'Low fire' },
  { cone: '06', fahrenheit: 1828, celsius: 998, description: 'Earthenware / Low fire glaze' },
  { cone: '05', fahrenheit: 1888, celsius: 1031, description: 'Earthenware / Low fire glaze' },
  { cone: '04', fahrenheit: 1945, celsius: 1063, description: 'Bisque firing / Low fire glaze' },
  { cone: '03', fahrenheit: 1987, celsius: 1086, description: 'Bisque firing' },
  { cone: '02', fahrenheit: 2016, celsius: 1102, description: 'Mid-range' },
  { cone: '01', fahrenheit: 2046, celsius: 1119, description: 'Mid-range' },
  { cone: '1', fahrenheit: 2079, celsius: 1137, description: 'Mid-range' },
  { cone: '2', fahrenheit: 2088, celsius: 1142, description: 'Mid-range' },
  { cone: '3', fahrenheit: 2106, celsius: 1152, description: 'Mid-range' },
  { cone: '4', fahrenheit: 2124, celsius: 1162, description: 'Mid-range' },
  { cone: '5', fahrenheit: 2167, celsius: 1186, description: 'Mid-range / Stoneware' },
  { cone: '6', fahrenheit: 2232, celsius: 1222, description: 'Stoneware / Mid-range glaze' },
  { cone: '7', fahrenheit: 2262, celsius: 1239, description: 'Stoneware' },
  { cone: '8', fahrenheit: 2280, celsius: 1249, description: 'Stoneware' },
  { cone: '9', fahrenheit: 2300, celsius: 1260, description: 'High fire' },
  { cone: '10', fahrenheit: 2345, celsius: 1285, description: 'High fire / Stoneware' },
  { cone: '11', fahrenheit: 2361, celsius: 1294, description: 'High fire' },
  { cone: '12', fahrenheit: 2383, celsius: 1306, description: 'High fire' },
  { cone: '13', fahrenheit: 2410, celsius: 1321, description: 'Very high fire' },
  { cone: '14', fahrenheit: 2431, celsius: 1332, description: 'Very high fire / Porcelain' },
];

/**
 * Get temperature for a specific Orton cone
 * @param cone - The cone number (e.g., "04", "6", "10")
 * @param preferredUnit - The user's preferred temperature unit ('F' or 'C')
 */
export function getConeTemperature(cone: string, preferredUnit: 'F' | 'C' = 'F'): Temperature | null {
  const normalized = cone.trim().toLowerCase().replace(/^0+(?=[1-9])/, '0');
  const coneData = CONE_TEMPERATURE_CHART.find(
    c => c.cone.toLowerCase() === normalized
  );

  if (!coneData) {
    return null;
  }

  return {
    value: preferredUnit === 'C' ? coneData.celsius : coneData.fahrenheit,
    unit: preferredUnit,
  };
}

/**
 * Get cone data by cone number
 */
export function getConeData(cone: string): ConeData | null {
  const normalized = cone.trim().toLowerCase().replace(/^0+(?=[1-9])/, '0');
  return CONE_TEMPERATURE_CHART.find(
    c => c.cone.toLowerCase() === normalized
  ) || null;
}

/**
 * Suggest cone based on temperature (returns closest match)
 */
export function suggestConeFromTemperature(temp: Temperature): ConeData | null {
  const fahrenheit = temp.unit === 'F' ? temp.value : temp.value * 9/5 + 32;

  let closest = CONE_TEMPERATURE_CHART[0];
  let minDiff = Math.abs(closest.fahrenheit - fahrenheit);

  for (const coneData of CONE_TEMPERATURE_CHART) {
    const diff = Math.abs(coneData.fahrenheit - fahrenheit);
    if (diff < minDiff) {
      minDiff = diff;
      closest = coneData;
    }
  }

  return minDiff <= 100 ? closest : null; // Only suggest if within 100°F
}

/**
 * Get all available cone numbers
 */
export function getAllCones(): string[] {
  return CONE_TEMPERATURE_CHART.map(c => c.cone);
}

/**
 * Validate if a cone number exists in the chart
 */
export function isValidCone(cone: string): boolean {
  return getConeData(cone) !== null;
}

/**
 * Format cone for display (e.g., "04" -> "Cone 04")
 */
export function formatCone(cone: string): string {
  return `Cone ${cone}`;
}

/**
 * Get common bisque firing cones
 */
export function getBisqueCones(): string[] {
  return ['04', '03', '02', '01'];
}

/**
 * Get common glaze firing cones
 */
export function getGlazeFiringCones(): string[] {
  return ['06', '05', '04', '5', '6', '7', '8', '9', '10'];
}
