import 'package:app_hit_a_wod/app_config.dart';
import 'package:app_hit_a_wod/src/pages/configuration/configuration.page.dart';
import 'package:app_hit_a_wod/src/pages/exercise/exercise-filter.page.dart';
import 'package:app_hit_a_wod/src/pages/exercise/exercise.page.dart';
import 'package:app_hit_a_wod/src/pages/legal/legal.page.dart';
import 'package:app_hit_a_wod/src/pages/login/change-password.page.dart';
import 'package:app_hit_a_wod/src/pages/login/forgot-password.page.dart';
import 'package:app_hit_a_wod/src/pages/login/login.page.dart';
import 'package:app_hit_a_wod/src/pages/my_orders/my_order_detail.page.dart';
import 'package:app_hit_a_wod/src/pages/my_orders/my_orders.page.dart';
import 'package:app_hit_a_wod/src/pages/my_subscriptions/my_subscriptions.page.dart';
import 'package:app_hit_a_wod/src/pages/news/news.page.dart';
import 'package:app_hit_a_wod/src/pages/news/news_detail.page.dart';
import 'package:app_hit_a_wod/src/pages/no-subscriptions/no_subscriptions.page.dart';
import 'package:app_hit_a_wod/src/pages/training_session/training-session-detail.page.dart';
import 'package:app_hit_a_wod/src/pages/training_workout/training-workout-detail.page.dart';
import 'package:app_hit_a_wod/src/pages/training_workout/training-workout-filter.page.dart';
import 'package:app_hit_a_wod/src/pages/user/user-detail.page.dart';
import 'package:app_hit_a_wod/src/pages/user_benchmark/user-benchmark-create.page.dart';
import 'package:app_hit_a_wod/src/pages/user_benchmark/user-benchmark-detail.page.dart';
import 'package:app_hit_a_wod/src/providers/api.provider.dart';
import 'package:app_hit_a_wod/src/providers/auth.provider.dart';
import 'package:app_hit_a_wod/src/providers/main_scaffold.provider.dart';
import 'package:app_hit_a_wod/src/providers/snackbar.provider.dart';
import 'package:app_hit_a_wod/src/providers/user-subscriptions.provider.dart';
import 'package:app_hit_a_wod/src/styles.dart';
import 'package:flutter/material.dart';
import 'package:app_hit_a_wod/src/pages/home.page.dart';
import 'package:flutter/services.dart';
import 'package:flutter_neumorphic/flutter_neumorphic.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:onesignal_flutter/onesignal_flutter.dart';
import 'dart:io' show Platform;

class MyApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    // Remove this method to stop OneSignal Debugging 
    // OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);

    if (Platform.isAndroid) {
      OneSignal.shared.init(
        "cbbd7059-16c2-4a9f-a4ea-302529493118",
        iOSSettings: {
          OSiOSSettings.autoPrompt: false,
          OSiOSSettings.inAppLaunchUrl: false
        }
      );
      OneSignal.shared.setInFocusDisplayType(OSNotificationDisplayType.notification);

      // The promptForPushNotificationsWithUserResponse function will show the iOS push notification prompt. We recommend removing the following code and instead using an In-App Message to prompt for notification permission
      // OneSignal.shared.promptUserForPushNotificationPermission(fallbackToSettings: true);
    }
    
    //load config
    var config = AppConfig.of(context);

    //estils barra superior
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp,]);
    SystemChrome.setSystemUIOverlayStyle(
      SystemUiOverlayStyle(
        statusBarColor: Colors.white, //top bar color
        statusBarIconBrightness: Brightness.dark, //top bar icons
        statusBarBrightness: Brightness.light
      ) 
    );

    Intl.defaultLocale = "es";
    
    return MultiProvider(
      providers: [
        Provider.value(value: SnackbarProvider(context)),
        Provider.value(value: MainScaffoldProvider()),
        ProxyProvider<SnackbarProvider, ApiProvider>(update: (_, snack, __) => ApiProvider(snack, url: config.apiBaseUrl)),
        ProxyProvider<ApiProvider, UserSubscriptionsProvider>(update: (_, api, __) => UserSubscriptionsProvider(context, api)),
        ProxyProvider2<ApiProvider, UserSubscriptionsProvider, AuthProvider>(update: (_, api, us, __) => AuthProvider(api: api, us: us),),
      ],
      child: MaterialApp(
        locale: Locale('es','ES'),
        debugShowCheckedModeBanner: false,
        title: config.appName,
        initialRoute: 'login',
        routes: {
          'home' : (BuildContext context) => HomePage(),
          'login' : (BuildContext context) => LoginPage(),
          'forgot-password' : (BuildContext context) => ForgotPasswordPage(),
          'change-password' : (BuildContext context) => ChangePasswordPage(),
          'no-subscriptions' : (BuildContext context) => NoSubscriptionsPage(),
          'user-detail' : (BuildContext context) => UserDetailPage(),
          'my-orders' : (BuildContext context) => MyOrdersPage(),
          'my-orders-detail' : (BuildContext context) => MyOrderDetailPage(),
          'my-subscriptions' : (BuildContext context) => MySubscriptionsPage(),
          'training-session-detail' : (BuildContext context) => TrainingSessionDetailPage(),
          'training-workout-detail'  : (BuildContext context) => TrainingWorkoutDetailPage(),
          'training-workout-filter'  : (BuildContext context) => TrainingWorkoutFilterPage(),
          'user-benchmark-detail' : (BuildContext context) => UserBenchmarkDetailPage(),
          'news' : (BuildContext context) => NewsPage(),
          'news-detail' : (BuildContext context) => NewsDetailPage(),
          'exercise-detail' : (BuildContext context) => ExerciseDetailPage(),
          'exercise-filter' : (BuildContext context) => ExerciseFilterPage(),
          'user-benchmark-create' : (BuildContext context) => UserBenchmarkCreatePage(),
          'configuration' : (BuildContext context) => ConfigurationPage(),
          'legal' : (BuildContext context) => LegalPage(),
        },
        theme: ThemeData(
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          brightness: Brightness.light,
          fontFamily: 'Basier',
          textTheme: TextTheme(
            headline: TextStyle(fontSize: 72.0, color: Color(0xFF575F6B)),
            title: TextStyle(fontSize: 24.0, color: Color(0xFF575F6B)),
            subtitle: TextStyle(fontSize: 18.0, color: Color(0xFF575F6B),fontWeight: FontWeight.w500),
            overline: TextStyle(fontSize: 16.0, color: Color(0xFF575F6B)),
            body1: TextStyle(fontSize: 16.0, color: Color(0xFF575F6B)),
            display1: TextStyle(fontSize: 16.0, color: colors["primary"]),
            display2: TextStyle(fontSize: 16.0, color: colors["white"]),
            display3: TextStyle(fontSize: 16.0, color: colors["seven"]),
            display4: TextStyle(fontSize: 16.0, color: Color(0xFF51CE86)),
          ),
          primaryColor: Color.fromRGBO(141, 39, 144, 1)
        ),
        localizationsDelegates: [
          // ... app-specific localization delegate[s] here
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('es'),
        ],
      ),
    );
  }

  /* Future<void> initPlatformState() async {
    if (!mounted) return;

    OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);

    OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);

    var settings = {
      OSiOSSettings.autoPrompt: false,
      OSiOSSettings.promptBeforeOpeningPushUrl: true
    };

    OneSignal.shared.setNotificationReceivedHandler((OSNotification notification) {
      this.setState(() {
        _debugLabelString =
            "Received notification: \n${notification.jsonRepresentation().replaceAll("\\n", "\n")}";
      });
    });

    OneSignal.shared
        .setNotificationOpenedHandler((OSNotificationOpenedResult result) {
      this.setState(() {
        _debugLabelString =
            "Opened notification: \n${result.notification.jsonRepresentation().replaceAll("\\n", "\n")}";
      });
    });

    OneSignal.shared
    .setInAppMessageClickedHandler((OSInAppMessageAction action) {
      this.setState(() {
        _debugLabelString =
            "In App Message Clicked: \n${action.jsonRepresentation().replaceAll("\\n", "\n")}";
      });
    });

    OneSignal.shared
        .setSubscriptionObserver((OSSubscriptionStateChanges changes) {
      print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
    });

    OneSignal.shared.setPermissionObserver((OSPermissionStateChanges changes) {
      print("PERMISSION STATE CHANGED: ${changes.jsonRepresentation()}");
    });

    OneSignal.shared.setEmailSubscriptionObserver(
        (OSEmailSubscriptionStateChanges changes) {
      print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
    });

    // NOTE: Replace with your own app ID from https://www.onesignal.com
    await OneSignal.shared
        .init("cbbd7059-16c2-4a9f-a4ea-302529493118", iOSSettings: settings);

    OneSignal.shared
        .setInFocusDisplayType(OSNotificationDisplayType.notification);

    bool requiresConsent = await OneSignal.shared.requiresUserPrivacyConsent();

    this.setState(() {
      _enableConsentButton = requiresConsent;
    });

    /* // Some examples of how to use In App Messaging public methods with OneSignal SDK
    oneSignalInAppMessagingTriggerExamples();

    // Some examples of how to use Outcome Events public methods with OneSignal SDK
    oneSignalOutcomeEventsExamples(); */
  }

  void _handleGetTags() {
    OneSignal.shared.getTags().then((tags) {
      if (tags == null) return;

      setState((() {
        _debugLabelString = "$tags";
      }));
    }).catchError((error) {
      setState(() {
        _debugLabelString = "$error";
      });
    });
  } */
}