import 'package:flutter/material.dart';

import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:get/get.dart';

import 'package:f0ckapp/controller/localizationcontroller.dart';
import 'package:f0ckapp/controller/settingscontroller.dart';
import 'package:f0ckapp/utils/animatedtransition.dart';

class SettingsPage extends StatefulWidget {
  const SettingsPage({super.key});

  @override
  State<StatefulWidget> createState() => _SettingsPageState();
}

class _SettingsPageState extends State<SettingsPage> {
  final SettingsController settingsController = Get.find<SettingsController>();
  final LocalizationController localizationController = Get.find<LocalizationController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverAppBar(
            floating: false,
            pinned: true,
            title: Text('settings_title'.tr),
          ),
          SliverList(
            delegate: SliverChildListDelegate([
              ListTile(
                title: Text('settings_numberofcolumns_title'.tr),
                trailing: Obx(
                  () => DropdownButton<int>(
                    value: settingsController.crossAxisCount.value,
                    dropdownColor: const Color.fromARGB(255, 43, 43, 43),
                    iconEnabledColor: Colors.white,
                    items: [0, 3, 4, 5].map((int value) {
                      return DropdownMenuItem<int>(
                        value: value,
                        child: Text(
                          value == 0
                              ? 'auto'
                              : 'settings_numberofcolumns_columns'.trParams({
                                  'count': value.toString(),
                                }),
                        ),
                      );
                    }).toList(),
                    onChanged: (int? newValue) async {
                      if (newValue != null) {
                        await settingsController.setCrossAxisCount(newValue);
                      }
                    },
                  ),
                ),
              ),
              const Divider(),
              ListTile(
                title: Text('settings_pageanimation_title'.tr),
                trailing: Obx(
                  () => DropdownButton<PageTransition>(
                    value: settingsController.transitionType.value,
                    dropdownColor: const Color.fromARGB(255, 43, 43, 43),
                    iconEnabledColor: Colors.white,
                    items: PageTransition.values.map((PageTransition type) {
                      String label;
                      switch (type) {
                        case PageTransition.opacity:
                          label = 'Opacity';
                          break;
                        case PageTransition.scale:
                          label = 'Scale';
                          break;
                        case PageTransition.slide:
                          label = 'Slide';
                          break;
                        case PageTransition.rotate:
                          label = 'Rotate';
                          break;
                        case PageTransition.flip:
                          label = 'Flip';
                          break;
                      }
                      return DropdownMenuItem<PageTransition>(
                        value: type,
                        child: Text(label),
                      );
                    }).toList(),
                    onChanged: (PageTransition? newValue) async {
                      if (newValue != null) {
                        await settingsController.setTransitionType(newValue);
                      }
                    },
                  ),
                ),
              ),

              const Divider(),
              SwitchListTile(
                title: Text('settings_drawer_title'.tr),
                subtitle: Text('settings_drawer_subtitle'.tr),
                value: settingsController.drawerSwipeEnabled.value,
                onChanged: (bool value) async {
                  await settingsController.setDrawerSwipeEnabled(value);
                  setState(() {});
                },
              ),
              const Divider(),
              ListTile(
                title: Text('settings_language'.tr),
                trailing: Obx(
                  () => DropdownButton<Locale>(
                    value: localizationController.currentLocale.value,
                    dropdownColor: const Color.fromARGB(255, 43, 43, 43),
                    iconEnabledColor: Colors.white,
                    items: const [
                      DropdownMenuItem<Locale>(
                        value: Locale('en', 'US'),
                        child: Text('English'),
                      ),
                      DropdownMenuItem<Locale>(
                        value: Locale('de', 'DE'),
                        child: Text('Deutsch'),
                      ),
                      DropdownMenuItem<Locale>(
                        value: Locale('fr', 'FR'),
                        child: Text('Français'),
                      ),
                      DropdownMenuItem<Locale>(
                        value: Locale('nl', 'NL'),
                        child: Text('Nederlands'),
                      ),
                    ],
                    onChanged: (Locale? newLocale) async {
                      if (newLocale != null) {
                        await localizationController.changeLocale(newLocale);
                      }
                    },
                  ),
                ),
              ),
              const Divider(),
              ListTile(
                title: Text('settings_cache_title'.tr),
                trailing: ElevatedButton(
                  onPressed: () async {
                    await DefaultCacheManager().emptyCache();
                    if (!mounted) return;
                    Get.snackbar('', 'der Cache wurde geleert.');
                  },
                  child: Text('settings_cache_clear_button'.tr),
                ),
              ),
            ]),
          ),
        ],
      ),
    );
  }
}
