
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:in_app_review/in_app_review.dart';
import 'package:marianum_mobile/extensions/renderNotNull.dart';
import 'package:persistent_bottom_nav_bar_v2/persistent_bottom_nav_bar_v2.dart';

import '../../widget/ListItem.dart';
import '../../widget/centeredLeading.dart';
import '../../widget/infoDialog.dart';
import '../settings/settings.dart';
import 'more/feedback/feedbackDialog.dart';
import 'more/gradeAverages/gradeAverage.dart';
import 'more/holidays/holidays.dart';
import 'more/message/message.dart';
import 'more/roomplan/roomplan.dart';
import 'more/share/selectShareTypeDialog.dart';

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

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        title: const Text("Mehr"),
        actions: [
          IconButton(onPressed: () => pushScreen(context, screen: const Settings(), withNavBar: false), icon: const Icon(Icons.settings))
        ],
      ),
      body: ListView(
        children: [
          const ListItemNavigator(icon: Icons.newspaper, text: "Marianum Message", target: Message()),
          const ListItemNavigator(icon: Icons.room, text: "Raumplan", target: Roomplan()),
          const ListItemNavigator(icon: Icons.calculate, text: "Notendurschnittsrechner", target: GradeAverage()),
          const ListItemNavigator(icon: Icons.calendar_month, text: "Schulferien", target: Holidays()),
          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: () => showDialog(context: context, builder: (context) => const SelectShareTypeDialog())
          ),
          FutureBuilder(
            future: InAppReview.instance.isAvailable(),
            builder: (context, snapshot) {
              if(!snapshot.hasData) return const SizedBox.shrink();

              String? getPlatformStoreName() {
                if(Platform.isAndroid) return "Play store";
                if(Platform.isIOS) return "App store";
                return null;
              }

              return ListTile(
                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: () {
                  InAppReview.instance.openStoreListing(appStoreId: "6458789560").then(
                          (value) => InfoDialog.show(context, "Vielen Dank!"),
                      onError: (error) => InfoDialog.show(context, error.toString())
                  );
                },
              );
            },
          ),
          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: () => showDialog(context: context, barrierDismissible: false, builder: (context) => const FeedbackDialog()),
          ),
        ],
      ),
    );
  }
}
