import React, { createContext, useContext, useState, ReactNode } from 'react';

export type IndustryType = "粮食" | "油脂" | "种业" | "果蔬" | "畜牧" | "水产" | "鲜食" | "泛盐";

// 产业主题色映射
const industryColors: Record<IndustryType, string> = {
  "粮食": "#C5FF92",
  "油脂": "#f6b02e",
  "种业": "#5def8b",
  "果蔬": "#fff586",
  "畜牧": "#f593a5",
  "水产": "#6be9ff",
  "鲜食": "#de7e66",
  "泛盐": "#92a5fe"
};

interface ThemeContextType {
  currentIndustry: IndustryType;
  themeColor: string;
  setIndustry: (industry: IndustryType) => void;
}

const ThemeContext = createContext<ThemeContextType | undefined>(undefined);

interface ThemeProviderProps {
  children: ReactNode;
  defaultIndustry?: IndustryType;
}

export const ThemeProvider: React.FC<ThemeProviderProps> = ({
  children,
  defaultIndustry = "粮食"
}) => {
  const [currentIndustry, setCurrentIndustry] = useState<IndustryType>(defaultIndustry);

  const themeColor = industryColors[currentIndustry];

  const setIndustry = (industry: IndustryType) => {
    setCurrentIndustry(industry);
  };

  return (
    <ThemeContext.Provider value={{
      currentIndustry,
      themeColor,
      setIndustry
    }}>
      {children}
    </ThemeContext.Provider>
  );
};

export const useTheme = (): ThemeContextType => {
  const context = useContext(ThemeContext);
  if (!context) {
    throw new Error('useTheme must be used within a ThemeProvider');
  }
  return context;
};

export default ThemeContext;