content: import 'package:flutter/material.dart';
  
  import 'package:fluffychat/config/app_config.dart';
  import 'package:fluffychat/config/setting_keys.dart';
  import 'package:fluffychat/utils/account_config.dart';
  import 'package:fluffychat/utils/file_selector.dart';
  import 'package:fluffychat/widgets/future_loading_dialog.dart';
  import 'package:fluffychat/widgets/theme_builder.dart';
  import '../../widgets/matrix.dart';
  import 'settings_style_view.dart';
  
  class SettingsStyle extends StatefulWidget {
    const SettingsStyle({super.key});
  
    @override
    SettingsStyleController createState() => SettingsStyleController();
  }
  
  class SettingsStyleController extends State<SettingsStyle> {
    void setChatColor(Color? color) async {
      AppConfig.colorSchemeSeed = color;
      ThemeController.of(context).setPrimaryColor(color);
    }
  
    void setWallpaper() async {
      final client = Matrix.of(context).client;
      final picked = await selectFiles(
        context,
        type: FileSelectorType.images,
      );
      final pickedFile = picked.firstOrNull;
      if (pickedFile == null) return;
  
      await showFutureLoadingDialog(
        context: context,
        future: () async {
          final url = await client.uploadContent(
            await pickedFile.readAsBytes(),
            filename: pickedFile.name,
          );
          await client.updateApplicationAccountConfig(
            ApplicationAccountConfig(wallpaperUrl: url),
          );
        },
      );
    }
  
    double get wallpaperOpacity =>
        _wallpaperOpacity ??
        Matrix.of(context).client.applicationAccountConfig.wallpaperOpacity ??
        0.5;
  
    double? _wallpaperOpacity;
  
    void saveWallpaperOpacity(double opacity) async {
      final client = Matrix.of(context).client;
      final result = await showFutureLoadingDialog(
        context: context,
        future: () => client.updateApplicationAccountConfig(
          ApplicationAccountConfig(wallpaperOpacity: opacity),
        ),
      );
      if (result.isValue) return;
  
      setState(() {
        _wallpaperOpacity = client.applicationAccountConfig.wallpaperOpacity;
      });
    }
  
    void updateWallpaperOpacity(double opacity) {
      setState(() {
        _wallpaperOpacity = opacity;
      });
    }
  
    double get wallpaperBlur =>
        _wallpaperBlur ??
        Matrix.of(context).client.applicationAccountConfig.wallpaperBlur ??
        0.5;
    double? _wallpaperBlur;
  
    void saveWallpaperBlur(double blur) async {
      final client = Matrix.of(context).client;
      final result = await showFutureLoadingDialog(
        context: context,
        future: () => client.updateApplicationAccountConfig(
          ApplicationAccountConfig(wallpaperBlur: blur),
        ),
      );
      if (result.isValue) return;
  
      setState(() {
        _wallpaperBlur = client.applicationAccountConfig.wallpaperBlur;
      });
    }
  
    void updateWallpaperBlur(double blur) {
      setState(() {
        _wallpaperBlur = blur;
      });
    }
  
    void deleteChatWallpaper() => showFutureLoadingDialog(
          context: context,
          future: () => Matrix.of(context).client.setApplicationAccountConfig(
                const ApplicationAccountConfig(
                  wallpaperUrl: null,
                  wallpaperBlur: null,
                ),
              ),
        );
  
    ThemeMode get currentTheme => ThemeController.of(context).themeMode;
    Color? get currentColor => ThemeController.of(context).primaryColor;
  
    static final List<Color?> customColors = [
      null,
      AppConfig.chatColor,
      Colors.indigo,
      Colors.blue,
      Colors.blueAccent,
      Colors.teal,
      Colors.tealAccent,
      Colors.green,
      Colors.greenAccent,
      Colors.yellow,
      Colors.yellowAccent,
      Colors.orange,
      Colors.orangeAccent,
      Colors.red,
      Colors.redAccent,
      Colors.pink,
      Colors.pinkAccent,
      Colors.purple,
      Colors.purpleAccent,
      Colors.blueGrey,
      Colors.grey,
      Colors.white,
      Colors.black,
    ];
  
    void switchTheme(ThemeMode? newTheme) {
      if (newTheme == null) return;
      switch (newTheme) {
        case ThemeMode.light:
          ThemeController.of(context).setThemeMode(ThemeMode.light);
          break;
        case ThemeMode.dark:
          ThemeController.of(context).setThemeMode(ThemeMode.dark);
          break;
        case ThemeMode.system:
          ThemeController.of(context).setThemeMode(ThemeMode.system);
          break;
      }
      setState(() {});
    }
  
    void changeFontSizeFactor(double d) {
      setState(() => AppConfig.fontSizeFactor = d);
      Matrix.of(context).store.setString(
            SettingKeys.fontSizeFactor,
            AppConfig.fontSizeFactor.toString(),
          );
    }
  
    @override
    Widget build(BuildContext context) => SettingsStyleView(this);
  }
  