import 'package:fcs/localization/app_translations_delegate.dart';
import 'package:fcs/localization/transalation.dart';
import 'package:fcs/pages/carton/model/carton_model.dart';
import 'package:fcs/pages/chat/model/message_model.dart';
import 'package:fcs/pages/contact/model/contact_model.dart';
import 'package:fcs/pages/customer/model/customer_model.dart';
import 'package:fcs/pages/delivery_address/model/delivery_address_model.dart';
import 'package:fcs/pages/discount/model/discount_model.dart';
import 'package:fcs/pages/faq/model/faq_model.dart';
import 'package:fcs/pages/fcs_shipment/model/fcs_shipment_model.dart';
import 'package:fcs/pages/main/home_page.dart';
import 'package:fcs/pages/main/initial_language_selection.dart';
import 'package:fcs/pages/invoice/model/invoice_model.dart';
import 'package:fcs/pages/market/model/market_model.dart';
import 'package:fcs/pages/main/model/language_model.dart';
import 'package:fcs/pages/main/model/main_model.dart';
import 'package:fcs/pages/package/model/package_model.dart';
import 'package:fcs/pages/payment_methods/model/payment_method_model.dart';
import 'package:fcs/pages/rates/model/shipment_rate_model.dart';
import 'package:fcs/pages/shipment/model/shipment_model.dart';
import 'package:fcs/pages/main/splash_page.dart';
import 'package:fcs/pages/staff/model/staff_model.dart';
import 'package:fcs/pages/term/model/term_model.dart';
import 'package:fcs/pages/main/welcome_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:provider/provider.dart';

import 'pages/delivery/model/delivery_model.dart';

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  final MainModel mainModel = new MainModel();
  final ContactModel contactModel = new ContactModel();
  final TermModel termModel = new TermModel();
  final FAQModel faqModel = new FAQModel();
  final PaymentMethodModel paymentMethodModel = new PaymentMethodModel();
  final FcsShipmentModel fcsShipmentModel = new FcsShipmentModel();
  final LanguageModel lanuguageModel = new LanguageModel();
  final ShipmentModel shipmentModel = new ShipmentModel();
  final ShipmentRateModel shipmentRateModel = new ShipmentRateModel();
  final CartonModel boxModel = new CartonModel();
  final MessageModel messageModel = new MessageModel();
  final InvoiceModel invoiceModel = new InvoiceModel();
  final CustomerModel customerModel = new CustomerModel();
  final DiscountModel discountModel = new DiscountModel();
  final StaffModel staffModel = new StaffModel();
  final DeliveryAddressModel deliveryAddressModel = new DeliveryAddressModel();
  final PackageModel packageModel = new PackageModel();
  final MarketModel marketModel = new MarketModel();
  final DeliveryModel deliveryModel = new DeliveryModel();

  AppTranslationsDelegate _newLocaleDelegate;

  @override
  void initState() {
    super.initState();
    mainModel
      ..addModel(customerModel)
      ..addModel(staffModel)
      ..addModel(deliveryAddressModel)
      ..addModel(packageModel)
      ..addModel(messageModel)
      ..addModel(fcsShipmentModel)
      ..addModel(discountModel)
      ..addModel(shipmentRateModel)
      ..addModel(boxModel)
      ..addModel(shipmentModel)
      ..addModel(invoiceModel)
      ..addModel(marketModel)
      ..addModel(deliveryModel);

    _newLocaleDelegate = AppTranslationsDelegate(newLocale: null);
    Translation().onLocaleChanged = onLocaleChange;
  }

  void onLocaleChange(Locale locale) {
    setState(() {
      _newLocaleDelegate = AppTranslationsDelegate(newLocale: locale);
    });
  }

  Map<String, WidgetBuilder> route(BuildContext context) {
    final routes = <String, WidgetBuilder>{
      '/': (_) => SplashScreen(),
      '/welcome': (_) => WelcomePage(),
      '/home': (_) => HomePage(),
      '/language_selection': (context) => InitialLanguageSelectionPage(),
    };
    return routes;
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: mainModel),
          ChangeNotifierProvider.value(value: staffModel),
          ChangeNotifierProvider.value(value: lanuguageModel),
          ChangeNotifierProvider.value(value: shipmentModel),
          ChangeNotifierProvider.value(value: shipmentRateModel),
          ChangeNotifierProvider.value(value: deliveryAddressModel),
          ChangeNotifierProvider.value(value: packageModel),
          ChangeNotifierProvider.value(value: boxModel),
          ChangeNotifierProvider.value(value: messageModel),
          ChangeNotifierProvider.value(value: invoiceModel),
          ChangeNotifierProvider.value(value: customerModel),
          ChangeNotifierProvider.value(value: discountModel),
          ChangeNotifierProvider.value(value: contactModel),
          ChangeNotifierProvider.value(value: termModel),
          ChangeNotifierProvider.value(value: faqModel),
          ChangeNotifierProvider.value(value: paymentMethodModel),
          ChangeNotifierProvider.value(value: marketModel),
          ChangeNotifierProvider.value(value: fcsShipmentModel),
          ChangeNotifierProvider.value(value: deliveryModel),
        ],
        child: Consumer<LanguageModel>(
          builder: (context, value, child) {
            return CupertinoApp(
                debugShowCheckedModeBanner: false,
                title: 'FCS',
                // theme: CupertinoThemeData(
                //     textTheme: CupertinoTextThemeData(
                //   navActionTextStyle: TextStyle(color: Colors.white),
                //   navTitleTextStyle: TextStyle(
                //       color: Colors.white,
                //       fontSize: value.isEng ? 20 : 18,
                //       fontWeight: FontWeight.w600),
                //   navLargeTitleTextStyle: TextStyle(
                //       color: Colors.white,
                //       fontSize: value.isEng ? 40 : 24,
                //       fontWeight: FontWeight.w600),
                //   primaryColor: primaryColor,
                //   textStyle: TextStyle(color: Colors.white),
                //   actionTextStyle: TextStyle(color: Colors.white),
                // )),
                routes: route(context),
                localizationsDelegates: [
                  _newLocaleDelegate,
                  GlobalMaterialLocalizations.delegate,
                  GlobalWidgetsLocalizations.delegate,
                ],
                supportedLocales: Translation().supportedLocales());
          },
        ));
  }
}
