import 'dart:io';

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_analytics/observer.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:tangheem/ui/screens/bookmark_screen.dart';
import 'package:tangheem/ui/screens/contact_us_screen.dart';
import 'package:tangheem/ui/screens/content_info_screen.dart';
import 'package:tangheem/ui/screens/member_screen.dart';
import 'package:tangheem/ui/screens/pdf_viewer_screen.dart';

import 'classes/colors.dart';
import 'ui/common_appbar.dart';
import 'ui/screens/forgot_password_screen.dart';
import 'ui/screens/home_screen.dart';
import 'ui/screens/login_screen.dart';
import 'ui/screens/quran_screen.dart';
import 'ui/screens/registration_screen.dart';
import 'ui/screens/tangheem_detail_screen.dart';
import 'ui/screens/tangheem_screen.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  // await Firebase.initializeApp();
  runApp(Application());
}

class Application extends StatelessWidget {
  static FirebaseAnalytics analytics = FirebaseAnalytics();
  static FirebaseAnalyticsObserver observer = FirebaseAnalyticsObserver(analytics: analytics);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Tangheem',
      theme: lightTheme(),
      debugShowCheckedModeBanner: false,
      //  navigatorObservers: <NavigatorObserver>[observer],
      initialRoute: HomeScreen.routeName,
      onGenerateRoute: (settings) {
        var className;
        switch (settings.name) {
          case LoginScreen.routeName:
            className = CommonAppbar(child: LoginScreen());
            break;
          case RegistrationScreen.routeName:
            className = CommonAppbar(child: RegistrationScreen());
            break;
          case ForgotPasswordScreen.routeName:
            className = CommonAppbar(child: ForgotPasswordScreen());
            break;
          case HomeScreen.routeName:
            className = CommonAppbar(showDrawer: true, child: HomeScreen(analytics));
            break;
          case ContactUsScreen.routeName:
            className = CommonAppbar(child: ContactUsScreen());
            break;
          case BookmarkScreen.routeName:
            className = CommonAppbar(child: BookmarkScreen());
            break;
          case MemberScreen.routeName:
            className = CommonAppbar(child: MemberScreen());
            break;
          case PdfListScreen.routeName:
            className = CommonAppbar(child: PdfListScreen());
            break;
          case PdfViewerScreen.routeName:
            className = CommonAppbar(child: PdfViewerScreen(settings.arguments));
            break;
          case ContentInfoScreen.routeName:
            var index = settings.arguments;
            className = CommonAppbar(child: ContentInfoScreen(contentId: index));
            break;
          case TangheemScreen.routeName:
            Map<String, Object> data = settings.arguments;
            className = CommonAppbar(
              child: TangheemScreen(
                surah: data["surahData"],
                tangheemTypeName: data["tangheemTypeName"],
                tangheemQuery: data["tangheemQuery"],
              ),
            );
            break;
          case TangheemDetailScreen.routeName:
            className = CommonAppbar(
              child: TangheemDetailScreen(tangheemDetailParams: settings.arguments),
            );
            break;
          case QuranScreen.routeName:
            var bookMark = settings.arguments;
            className = CommonAppbar(child: QuranScreen(bookmark: bookMark));
            break;
        }
        if (className == null) {
          return null;
        }
        var eventName = settings.name;
        eventName = eventName.replaceAll("/", "");
        if (eventName.isEmpty) eventName = "home";
        try {
          analytics
              .setCurrentScreen(
                screenName: eventName,
                screenClassOverride: eventName,
              )
              .then((value) => print("EventSent:$eventName"));
        } catch (ex) {
          print("Analytics:$ex");
        }
        return goToNavigation(className);
      },
    );
  }

  PageRoute goToNavigation(var className) {
    if (Platform.isIOS) {
      return CupertinoPageRoute(builder: (context) => className);
    }
    return MaterialPageRoute(builder: (context) => className);
  }

  ThemeData lightTheme() {
    return ThemeData(
      fontFamily: 'DroidKufi',
      brightness: Brightness.light,
      backgroundColor: ColorConsts.secondaryWhite,
      primarySwatch: Colors.blue,
      primaryColor: Colors.white,
      primaryColorBrightness: Brightness.light,
      primaryColorLight: Colors.white,
      visualDensity: VisualDensity.adaptivePlatformDensity,
    );
  }
}
