import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../../../state/app/modules/settings/bloc/settings_cubit.dart';
import '../../../../widget/centered_leading.dart';
import '../../../../widget/confirm_dialog.dart';
import '../../../../widget/details_bottom_sheet.dart';
import '../data/default_settings.dart';
import '../widgets/privacy_info.dart';
import 'dev_tools_section.dart';

class AboutSection extends StatelessWidget {
  const AboutSection({super.key});

  @override
  Widget build(BuildContext context) {
    final settings = context.watch<SettingsCubit>();
    return Column(
      children: [
        ListTile(
          leading: const Icon(Icons.live_help_outlined),
          title: const Text('Informationen und Lizenzen'),
          onTap: () => _showAppInfo(context),
          trailing: const Icon(Icons.arrow_right),
        ),
        ListTile(
          leading: const Icon(Icons.policy_outlined),
          title: const Text('Impressum & Datenschutz'),
          onTap: () => _showPrivacyDialog(context),
          trailing: const Icon(Icons.arrow_right),
        ),
        const Divider(),
        ListTile(
          leading: const CenteredLeading(Icon(Icons.code)),
          title: const Text('Quellcode MarianumMobile/Client'),
          subtitle: const Text('GNU GPL v3'),
          onTap: () => ConfirmDialog.openBrowser(
            context,
            'https://mhsl.eu/gitea/MarianumMobile/Client',
          ),
        ),
        ListTile(
          leading: const Icon(Icons.developer_mode_outlined),
          title: const Text('Entwicklermodus'),
          trailing: Checkbox(
            value: settings.val().devToolsEnabled,
            onChanged: (state) =>
                _toggleDeveloperMode(context, settings, state),
          ),
        ),
        Visibility(
          visible: settings.val().devToolsEnabled,
          child: DevToolsSection(settings: settings),
        ),
      ],
    );
  }

  Future<void> _showAppInfo(BuildContext context) async {
    final appInfo = await PackageInfo.fromPlatform();
    if (!context.mounted) return;
    showAboutDialog(
      context: context,
      applicationIcon: const Icon(Icons.apps),
      applicationName: appInfo.appName,
      applicationVersion:
          '${appInfo.packageName}\n\nVersion: ${appInfo.version}\nBuild/Relase-nummer: ${appInfo.buildNumber}',
      applicationLegalese:
          'Alles für deinen Schulalltag am Marianum Fulda.\n\n'
          "${kReleaseMode ? "Production" : "Development ${kProfileMode ? "(Profiling)" : "(Debug)"}"} build.\n\n"
          'Marianum Fulda\n2019-2020 & 2022-${DateTime.now().year}\nElias Müller',
    );
  }

  void _showPrivacyDialog(BuildContext context) => showDetailsBottomSheet(
    context,
    children: (sheetCtx) => [
      ListTile(
        leading: const CenteredLeading(Icon(Icons.school_outlined)),
        title: const Text('Infos zum Marianum Fulda'),
        subtitle: const Text('Für Talk-Chats und Cloud-Dateien'),
        trailing: const Icon(Icons.arrow_right),
        onTap: () => PrivacyInfo(
          providerText: 'Marianum',
          imprintUrl: 'https://www.marianum-fulda.de/impressum',
          privacyUrl: 'https://www.marianum-fulda.de/datenschutz',
        ).showPopup(sheetCtx),
      ),
      ListTile(
        leading: const CenteredLeading(Icon(Icons.date_range_outlined)),
        title: const Text('Infos zu (Web) Untis'),
        subtitle: const Text('Für den Stundenplan'),
        trailing: const Icon(Icons.arrow_right),
        onTap: () => PrivacyInfo(
          providerText: 'Untis',
          imprintUrl: 'https://www.untis.at/impressum',
          privacyUrl: 'https://www.untis.at/datenschutz-wu-apps',
        ).showPopup(sheetCtx),
      ),
      ListTile(
        leading: const CenteredLeading(
          Icon(Icons.send_time_extension_outlined),
        ),
        title: const Text('Infos zu mhsl'),
        subtitle: const Text('Für Push, Kalendertermine, Marianum Message und mehr'),
        trailing: const Icon(Icons.arrow_right),
        onTap: () => PrivacyInfo(
          providerText: 'mhsl',
          imprintUrl: 'https://mhsl.eu/id.html',
          privacyUrl: 'https://mhsl.eu/datenschutz.html',
        ).showPopup(sheetCtx),
      ),
    ],
  );

  void _toggleDeveloperMode(
    BuildContext context,
    SettingsCubit settings,
    bool? state,
  ) {
    void apply() {
      final enabled = state ?? false;
      settings.val(write: true).devToolsEnabled = enabled;
      if (!enabled) {
        settings.val(write: true).devToolsSettings =
            DefaultSettings.get().devToolsSettings;
      }
    }

    if (!state!) {
      apply();
      return;
    }

    ConfirmDialog(
      title: 'Entwicklermodus',
      content:
          'Die Entwickleransicht bietet erweiterte Funktionen, die für den üblichen Gebrauch nicht benötigt werden.\n\n'
          'Die Verwendung der Tools kann darüber hinaus bei falscher Verwendung zu Fehlern führen.\n\n'
          'Aktivieren auf eigene Verantwortung.',
      confirmButton: 'Ja, ich verstehe das Risiko',
      cancelButton: 'Nein, zurück zur App',
      onConfirm: apply,
    ).asDialog(context);
  }
}
