// src/utils/ThemeAndLanguageContext.js
import React, { createContext, useContext, useState, useEffect } from 'react';
import AsyncStorage from '@react-native-async-storage/async-storage';
import * as Localization from 'expo-localization';
import { I18n } from 'i18n-js';
import { Appearance } from 'react-native';
import * as Font from 'expo-font';
import { setStatusBarStyle } from 'expo-status-bar';

import { darkTheme } from '../themes/darkTheme';
import { lightTheme } from '../themes/lightTheme';
import { vibrantTheme } from '../themes/vibrantTheme';
import { secretGardenTheme } from '../themes/secretGardenTheme';
import { slateDarkTheme } from '../themes/slateDarkTheme';
import { blackOrangeTheme } from '../themes/blackOrangeTheme';

const i18n = new I18n({
  en: require('../locales/en.json'),
  tr: require('../locales/tr.json'),
  // Diğer dilleri buraya ekleyin
});

i18n.enableFallback = true;

const ThemeAndLanguageContext = createContext();

export const ThemeAndLanguageProvider = ({ children }) => {
  const [themeType, setThemeType] = useState('light');
  const [locale, setLocale] = useState(Localization.locale);
  const [isLoading, setIsLoading] = useState(true);
  const [fontsLoaded, setFontsLoaded] = useState(false);
  const [fontFamily, setFontFamily] = useState('Scheherazade'); // Varsayılan Arapça font

  useEffect(() => {
    const loadSettings = async () => {
      try {
        const storedTheme = await AsyncStorage.getItem('theme');
        const storedLocale = await AsyncStorage.getItem('locale');
        const storedFontFamily = await AsyncStorage.getItem('fontFamily');

        if (storedTheme) {
          setThemeType(storedTheme);
        } else {
          const defaultTheme = Appearance.getColorScheme();
          setThemeType(defaultTheme || 'light');
        }

        if (storedLocale) {
          setLocale(storedLocale);
          i18n.locale = storedLocale;
        } else {
          const defaultLocale = Localization.locale.startsWith('tr') ? 'tr' : 'en';
          i18n.locale = defaultLocale;
          setLocale(defaultLocale);
        }

        if (storedFontFamily) {
          setFontFamily(storedFontFamily);
        }
      } catch (error) {
        console.error('Error loading settings:', error);
      } finally {
        setIsLoading(false);
      }
    };

    const loadFonts = async () => {
      await Font.loadAsync({
        // Mevcut Arapça fontlarınız
        'Adobe Arabic': require('../../assets/fonts/adobearabic.ttf'),
        'Scheherazade': require('../../assets/fonts/scheherazade.ttf'),
        'Amiri': require('../../assets/fonts/amiri.ttf'),
        'Noto Naskh Arabic': require('../../assets/fonts/noto.ttf'),

        // Poppins fontları
        'Poppins-Regular': require('../../assets/fonts/Poppins-Regular.ttf'),
        'Poppins-Bold': require('../../assets/fonts/Poppins-Bold.ttf'),
        'Poppins-Italic': require('../../assets/fonts/Poppins-Italic.ttf'),
        // Diğer Poppins stillerini ekleyin
      });
      setFontsLoaded(true);
    };

    loadSettings();
    loadFonts();
  }, []);

  useEffect(() => {
    setStatusBarStyle(
      themeType === 'dark' ||
      themeType === 'blackOrange' ||
      themeType === 'slateDark' ||
      themeType === 'secretGarden' ||
      themeType === 'vibrant'
        ? 'light'
        : 'dark'
    );
  }, [themeType]);

  const theme =
    themeType === 'vibrant'
      ? vibrantTheme
      : themeType === 'secretGarden'
      ? secretGardenTheme
      : themeType === 'slateDark'
      ? slateDarkTheme
      : themeType === 'blackOrange'
      ? blackOrangeTheme
      : themeType === 'dark'
      ? darkTheme
      : lightTheme;

  const changeTheme = async (newThemeType) => {
    setThemeType(newThemeType);
    await AsyncStorage.setItem('theme', newThemeType);
  };

  const changeLanguage = async (newLocale) => {
    setLocale(newLocale);
    i18n.locale = newLocale;
    await AsyncStorage.setItem('locale', newLocale);
  };

  const updateFontFamily = async (newFontFamily) => {
    setFontFamily(newFontFamily);
    await AsyncStorage.setItem('fontFamily', newFontFamily);
  };

  if (isLoading || !fontsLoaded) {
    return null; // Yükleniyor durumunda hiçbir şey render edilmez
  }

  const contextValue = {
    theme,
    themeType,
    locale,
    setThemeType: changeTheme, // Temayı güncelleme fonksiyonu
    changeLanguage,
    t: (key, options) => i18n.t(key, options),
    arabicFont: fontFamily, // Global Arapça font
    setFontFamily: updateFontFamily, // Font güncelleme işlevi
    poppinsRegular: 'Poppins-Regular', // Poppins Regular
    poppinsBold: 'Poppins-Bold', // Poppins Bold
    poppinsItalic: 'Poppins-Italic', // Poppins Italic
    // Diğer Poppins stillerini ekleyebilirsiniz
  };

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

export const useThemeAndLanguage = () => {
  const context = useContext(ThemeAndLanguageContext);
  if (context === undefined) {
    throw new Error('useThemeAndLanguage must be used within a ThemeAndLanguageProvider');
  }
  return context;
}; hacı burada arapça fontlarımız var ama sadece tek çeşidi var. bunun boldlarını da ekleyelim.