import 'package:after_layout/after_layout.dart';
import 'package:diplomaticquarterapp/config/shared_pref_kay.dart';
import 'package:diplomaticquarterapp/core/model/ResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/hospitals/hospitals_model.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/requests/AddProductToCartRequestModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/packages_offers/PackagesOffersViewModel.dart';
import 'package:diplomaticquarterapp/pages/AlHabibMedicalService/h2o/h20_setting.dart';
import 'package:diplomaticquarterapp/pages/ToDoList/payment_method_select.dart';
import 'package:diplomaticquarterapp/pages/base/base_view.dart';
import 'package:diplomaticquarterapp/pages/packages_offers/PackageOrderCompletedPage.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/pharmacy-terms-conditions-page.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart' as utils;
import 'package:diplomaticquarterapp/uitl/utils_new.dart';
import 'package:diplomaticquarterapp/widgets/buttons/defaultButton.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/dialogs/radio_selection_dialog.dart';
import 'package:diplomaticquarterapp/widgets/in_app_browser/InAppBrowser.dart';
import 'package:diplomaticquarterapp/widgets/offers_packages/PackagesCartItemCard.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

dynamic languageID;
const _columnCount = 1;
bool _agreeTerms = false;
String _selectedPaymentMethod;
Map<dynamic, dynamic> _selectedPaymentParams;

AnimationController _animationController;

class PackagesCartPage extends StatefulWidget {
  PackagesCartPage();

  @override
  _PackagesCartPageState createState() => _PackagesCartPageState();
}

class _PackagesCartPageState extends State<PackagesCartPage> with AfterLayoutMixin<PackagesCartPage>, SingleTickerProviderStateMixin {
  getLanguageID() async {
    languageID = await sharedPref.getString(APP_LANGUAGE);
  }

  double subtotal, tax, total;
  int _selectedHospitalIndex = -1;
  HospitalsModel _selectedHospital;

  @override
  void initState() {
    _agreeTerms = false;
    _selectedPaymentMethod = null;
    _animationController = AnimationController(vsync: this, duration: Duration(seconds: 500));
    super.initState();
  }

  @override
  void dispose() {
    _animationController.dispose();
    viewModel.cartItemList.clear();
    super.dispose();
  }

  PackagesViewModel viewModel;

  bool loadWidgets = false;

  onTermsClick(bool isAgree) {
    setState(() => _agreeTerms = isAgree);
  }

  onTermsInfoClick() {
    Navigator.push(context, FadePage(page: PharmacyTermsConditions()));
  }

  onPayNowClick() async {
    await viewModel.service.placeOrder(context: context, projectID: _selectedHospital.iD, paymentParams: _selectedPaymentParams).then((orderId) {
      if (orderId.runtimeType == int) {
        var browser = MyInAppBrowser(context: context, onExitCallback: (data, isDone) => paymentClosed(orderId: orderId, withStatus: isDone, data: data));
        browser.openPackagesPaymentBrowser(customer_id: viewModel.service.customer.id, order_id: orderId);
      } else {
        utils.Utils.showErrorToast('Failed to place order, please try again later');
      }
    }).catchError((error) {
      utils.Utils.showErrorToast(error.toString());
    });
  }

  @override
  void afterFirstLayout(BuildContext context) {
    fetchData();
    viewModel.service.getHospitals();
  }

  @override
  Widget build(BuildContext context) {
    return BaseView<PackagesViewModel>(
      allowAny: true,
      onModelReady: (model) => viewModel = model,
      builder: (_, model, wi) {
        return AppScaffold(
          appBarTitle: TranslationBase.of(context).offerAndPackages,
          isShowAppBar: true,
          isPharmacy: false,
          showPharmacyCart: false,
          showHomeAppBarIcon: false,
          isOfferPackages: true,
          showOfferPackagesCart: false,
          isShowDecPage: false,
          showNewAppBar: true,
          showNewAppBarTitle: true,
          body: viewModel.cartItemList.length > 0
              ? Column(
                  children: [
                    Expanded(
                      child: ListView.separated(
                          padding: EdgeInsets.all(21),
                          physics: BouncingScrollPhysics(),
                          itemBuilder: (cxt, index) {
                            var item = viewModel.cartItemList[index];
                            return PackagesCartItemCard(
                              itemModel: item,
                              viewModel: viewModel,
                              getCartItems: fetchData,
                              shouldStepperChangeApply: (apply, total) async {
                                var request = AddProductToCartRequestModel(product_id: item.productId, quantity: apply);
                                ResponseModel response = await viewModel.service.addProductToCart(request, context: context, showLoading: false).catchError((error) {
                                  utils.Utils.showErrorToast(error);
                                });
                                if (response.status) {
                                  fetchData();
                                }
                                return response.status ?? false;
                              },
                            );
                          },
                          separatorBuilder: (cxt, index) => SizedBox(height: 12),
                          itemCount: viewModel.cartItemList.length),
                    ),
                    Container(
                      height: 1,
                      color: Color(0xffEFEFEF),
                    ),
                    if (viewModel.cartItemList.length > 0)
                      Container(
                        padding: EdgeInsets.only(left: 21, right: 21, top: 15, bottom: 15),
                        width: double.infinity,
                        color: Colors.white,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            CommonDropDownView(
                              TranslationBase.of(context).hospital,
                              getHospitalName(),
                              () => confirmSelectHospitalDialog(model.hospitals),
                            ).withBorderedContainer,
                            SizedBox(height: 12),
                            Text(
                              TranslationBase.of(context).YouCanPayByTheFollowingOptions,
                              style: TextStyle(
                                fontSize: 16.0,
                                fontWeight: FontWeight.w600,
                                color: Color(0xff2B353E),
                                letterSpacing: -0.64,
                              ),
                            ),
                            SizedBox(
                              width: MediaQuery.of(context).size.width * 0.75,
                              child: getPaymentMethods(),
                            ),
                            Padding(
                              padding: EdgeInsets.only(top: 12.0, bottom: 12),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  SizedBox(
                                    height: 16.0,
                                    width: 16.0,
                                    child: Checkbox(
                                        value: _agreeTerms,
                                        onChanged: (v) {
                                          setState(() => _agreeTerms = v);
                                        }),
                                  ),
                                  SizedBox(width: 10),
                                  Expanded(
                                    child: Text(
                                      TranslationBase.of(context).iAcceptTermsConditions,
                                      style: TextStyle(fontSize: 12, fontWeight: FontWeight.w600, color: CustomColors.textColor, letterSpacing: -0.48),
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                _amountView(TranslationBase.of(context).patientShareToDo, (subtotal ?? 0.0).toStringAsFixed(2) + " " + TranslationBase.of(context).sar, isBold: true),
                                mDivider(Color(0xffEFEFEF)),
                                _amountView(TranslationBase.of(context).patientTaxToDo, (tax ?? 0.0).toStringAsFixed(2) + " " + TranslationBase.of(context).sar, isBold: true),
                                mDivider(Color(0xffEFEFEF)),
                                _amountView(TranslationBase.of(context).patientShareTotalToDo, (total ?? 0.0).toStringAsFixed(2) + " " + TranslationBase.of(context).sar, isBold: true, isTotal: true),
                              ],
                            ),
                            SizedBox(height: 12),
                            DefaultButton(
                              TranslationBase.of(context).payNow,
                              (_agreeTerms && _selectedHospital != null)
                                  ? () {
                                      Navigator.push(context, FadePage(page: PaymentMethod(onSelectedMethod: (String metohd) {
                                        setState(() {});
                                      }))).then((value) {
                                        print(value);
                                        if (value != null) {
                                          _selectedPaymentMethod = value;
                                          _selectedPaymentParams = {"payment_method_system_name": "Payments.PayFort", "payment_option": value};
                                          onPayNowClick();
                                        }
                                      });
                                    }
                                  : null,
                              color: CustomColors.green,
                              disabledColor: CustomColors.grey2,
                            ),
                          ],
                        ),
                      )
                  ],
                )
              : getNoDataWidget(context),
        );
      },
    );
  }

  void confirmSelectHospitalDialog(List<HospitalsModel> hospitals) {
    List<RadioSelectionDialogModel> list = [
      for (int i = 0; i < hospitals.length; i++) RadioSelectionDialogModel(hospitals[i].name + ' ${hospitals[i].distanceInKilometers} ' + TranslationBase.of(context).km, i),
    ];
    showDialog(
      context: context,
      builder: (cxt) => RadioSelectionDialog(
        listData: list,
        selectedIndex: _selectedHospitalIndex,
        isScrollable: true,
        onValueSelected: (index) {
          _selectedHospitalIndex = index;
          _selectedHospital = hospitals[index];
          setState(() {});
        },
      ),
    );
  }

  String getHospitalName() {
    if (_selectedHospital != null)
      return _selectedHospital.name;
    else
      return TranslationBase.of(context).selectHospital;
  }

  fetchData() async {
    final cartResponse = await viewModel.service.cartItems(context: context).catchError((error) {});
    if(cartResponse != null){
      subtotal = cartResponse['subtotal'] ?? 0.0;
      tax = cartResponse['tax'] ?? 0.0;
      total = cartResponse['total'] ?? 0.0;
      viewModel.service.getTamaraOptions(context: context, showLoading: true).then((tamara_options){
        if(tamara_options !=  null || tamara_options.isNotEmpty) {
          viewModel.setTamaraIllegablity(total);
          setState(() {});
        }
      });
    }
  }

  paymentClosed({@required int orderId, @required bool withStatus, dynamic data}) async {
    viewModel.service.getOrderById(orderId, context: context).then((value) {
      var heading = withStatus ? "Success" : "Failed";
      var title = withStatus ? "Your order has been placed successfully" : "Failed to place your order";
      var subTitle = "Order# ${value.data.customOrderNumber}";
      Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (context) => PackageOrderCompletedPage(heading: heading, title: title, subTitle: subTitle)));
    }).catchError((error) {
      print(error);
    });
  }

}

// Widget _payNow(BuildContext context, {double subtotal, double tax, double total, @required VoidCallback onPayNowClick}) {
//   bool isPayNowAQctive = (_agreeTerms && (_selectedPaymentMethod != null));
//
//   String _subtotal = (subtotal ?? 0.0).toStringAsFixed(2);
//   String _tax = (tax ?? 0.0).toStringAsFixed(2);
//   String _total = (total ?? 0).toStringAsFixed(2);
//
//   return Padding(
//     padding: const EdgeInsets.all(5),
//     child: Container(
//         child: Row(
//       crossAxisAlignment: CrossAxisAlignment.end,
//       children: [
//         Padding(
//           padding: const EdgeInsets.all(5),
//           child: Column(
//             crossAxisAlignment: CrossAxisAlignment.start,
//             children: [
//               Texts('${TranslationBase.of(context).subtotal}:    $_subtotal ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.grey, fontSize: 8),
//               Texts('${TranslationBase.of(context).vat}:   $_tax ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.grey, fontSize: 8),
//               Padding(
//                 padding: const EdgeInsets.all(3),
//                 child: Container(
//                   height: 0.25,
//                   width: 120,
//                   color: Colors.grey[300],
//                 ),
//               ),
//               Texts('${TranslationBase.of(context).total}:   $_total ${TranslationBase.of(context).sar}', heightFactor: 1.5, fontWeight: FontWeight.bold, color: Colors.black54, fontSize: 15)
//             ],
//           ),
//         ),
//         Expanded(child: Container()),
//         RaisedButton(
//           elevation: 0,
//           child: Texts(
//             TranslationBase.of(context).payNow,
//             fontSize: 15,
//             color: Colors.white,
//             fontWeight: FontWeight.bold,
//           ),
//           padding: EdgeInsets.only(top: 5, bottom: 5, left: 0, right: 0),
//           shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(5), side: BorderSide(color: Theme.of(context).primaryColor, width: 0.5)),
//           color: Theme.of(context).primaryColor,
//           onPressed: isPayNowAQctive ? onPayNowClick : null,
//         ),
//       ],
//     )),
//   );
// }
_amountView(String title, String value, {bool isBold = false, bool isTotal = false}) {
  return Padding(
    padding: const EdgeInsets.only(top: 10, bottom: 10),
    child: Row(children: [
      Expanded(
        child: _getNormalText(title),
      ),
      Expanded(
        child: _getNormalText(value, isBold: isBold, isTotal: isTotal),
      ),
    ]),
  );
}

_getNormalText(text, {bool isBold = false, bool isTotal = false}) {
  return Text(
    text,
    style: TextStyle(
      fontSize: isBold
          ? isTotal
              ? 16
              : 12
          : 11,
      letterSpacing: -0.5,
      color: isBold ? Color(0xff2E303A) : Color(0xff575757),
      fontWeight: isTotal ? FontWeight.bold : FontWeight.w600,
    ),
  );
}

Widget _cartItemDeleteContainer() {
  _animationController.duration = Duration(milliseconds: 500);
  _animationController.repeat(reverse: true);
  return FadeTransition(
    opacity: _animationController,
    child: Padding(
      padding: const EdgeInsets.all(5),
      child: Container(
        decoration: BoxDecoration(
          color: Colors.red,
          boxShadow: [
            BoxShadow(
              color: Colors.grey[500],
              blurRadius: 2,
              spreadRadius: 1,
            ),
          ],
          borderRadius: BorderRadius.all(Radius.circular(5)),
        ),
        child: Center(
            child: Texts(
          "Deleting...",
          fontWeight: FontWeight.normal,
          fontSize: 15,
          color: Colors.white,
        )),
      ),
    ),
  );
}
