import 'dart:io';

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

import '../../extensions/render_not_null.dart';
import '../../routing/app_routes.dart';
import '../../state/app/modules/app_modules.dart';
import '../../widget/centered_leading.dart';
import '../../widget/info_dialog.dart';

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

  @override
  State<Overhang> createState() => _OverhangState();
}

class _OverhangState extends State<Overhang> {
  @override
  Widget build(BuildContext context) => Scaffold(
    appBar: AppBar(
      title: const Text('Mehr'),
      actions: [
        IconButton(
          tooltip: 'Einstellungen',
          onPressed: () => AppRoutes.openSettings(context),
          icon: const Icon(Icons.settings),
        ),
      ],
    ),
    body: _overhang(),
  );

  Widget _overhang() => ListView(
    children: [
      ...AppModule.getOverhangModules(
        context,
      ).map((e) => e.toListTile(context)),

      const Divider(),

      ListTile(
        leading: const Icon(Icons.share_outlined),
        title: const Text('Teile die App'),
        subtitle: const Text('Mit Freunden und deiner Klasse teilen'),
        trailing: const Icon(Icons.arrow_right),
        onTap: () => AppRoutes.openQrShare(context),
      ),
      FutureBuilder(
        future: InAppReview.instance.isAvailable(),
        builder: (context, snapshot) {
          String? getPlatformStoreName() {
            if (Platform.isAndroid) return 'Play store';
            if (Platform.isIOS) return 'App store';
            return null;
          }

          Future<void> rate() => InAppReview.instance
              .openStoreListing(appStoreId: '6458789560')
              .then(
                (value) {
              if (!context.mounted) return;
              InfoDialog.show(context, 'Vielen Dank!');
            },
            onError: (error) {
              if (!context.mounted) return;
              InfoDialog.show(
                context,
                error.toString(),
                copyable: true,
                title: 'Fehler',
              );
            },
          );

          final ready = snapshot.connectionState == ConnectionState.done;
          return ListTile(
            enabled: ready,
            leading: const CenteredLeading(Icon(Icons.star_rate_outlined)),
            title: const Text('App bewerten'),
            subtitle: getPlatformStoreName().wrapNullable(
              (data) => Text('Im $data'),
            ),
            trailing: const Icon(Icons.arrow_right),
            onTap: !ready ? null : rate
          );
        },
      ),
      ListTile(
        leading: const CenteredLeading(Icon(Icons.feedback_outlined)),
        title: const Text('Du hast eine Idee?'),
        subtitle: const Text('Fehler und Verbessungsvorschläge'),
        trailing: const Icon(Icons.arrow_right),
        onTap: () => AppRoutes.openFeedback(context),
      ),
    ],
  );
}
