import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class ThemeProvider extends ChangeNotifier {
  final _storage = const FlutterSecureStorage();

  ThemeMode _themeMode = ThemeMode.system;
  Color _accentColor = const Color(0xFF24A1DE);

  ThemeMode get themeMode => _themeMode;
  Color get accentColor => _accentColor;

  ThemeProvider() {
    _loadSettings();
  }

  // Загрузка при старте
  Future<void> _loadSettings() async {
    final mode = await _storage.read(key: 'theme_mode');
    final color = await _storage.read(key: 'accent_color');

    if (mode != null) {
      _themeMode = mode == 'dark' ? ThemeMode.dark : ThemeMode.light;
    }
    if (color != null) _accentColor = Color(int.parse(color));
    notifyListeners();
  }

  void toggleTheme(bool isDark) {
    _themeMode = isDark ? ThemeMode.dark : ThemeMode.light;
    _storage.write(key: 'theme_mode', value: isDark ? 'dark' : 'light');
    notifyListeners();
  }

  void updateAccentColor(Color newColor) {
    _accentColor = newColor;
    _storage.write(key: 'accent_color', value: newColor.value.toString());
    notifyListeners();
  }

  ThemeData get themeData => ThemeData(
    useMaterial3: true,
    brightness: _themeMode == ThemeMode.dark
        ? Brightness.dark
        : Brightness.light,
    colorScheme: ColorScheme.fromSeed(
      seedColor: _accentColor,
      brightness: _themeMode == ThemeMode.dark
          ? Brightness.dark
          : Brightness.light,
    ),
    // Настраиваем глобальные стили текста
    textTheme: const TextTheme(
      bodyLarge: TextStyle(fontSize: 16.0),
      bodyMedium: TextStyle(fontSize: 14.0),
    ),
    appBarTheme: AppBarTheme(
      backgroundColor: _accentColor,
      foregroundColor: Colors.white,
      elevation: 0,
    ),
  );
}
