import React, { createContext, useState, useContext, useEffect } from 'react';
import { getCurrentTheme, setCurrentTheme, getThemeConfig } from '../config/themes';

const ThemeContext = createContext({});

export const useTheme = () => useContext(ThemeContext);

export const ThemeProvider = ({ children }) => {
  const [theme, setTheme] = useState(getCurrentTheme());
  const [config, setConfig] = useState(getThemeConfig());

  useEffect(() => {
    document.documentElement.setAttribute('data-theme', theme);
  }, [theme]);

  const changeTheme = (newTheme) => {
    setCurrentTheme(newTheme);
    setTheme(newTheme);
    setConfig(getThemeConfig());
    window.location.reload(); // Перезагружаем для применения новой темы
  };

  const value = {
    theme,
    config,
    changeTheme,
  };

  return (
    <ThemeContext.Provider value={value}>
      {children}
    </ThemeContext.Provider>
  );
};
