import 'dart:ui';

import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:diplomaticquarterapp/core/viewModels/project_view_model.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/cart-page/payment_bottom_widget.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/cart-page/select_address_widget.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/cart-page/select_payment_option_widget.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/widgets/ProductOrderPreviewItem.dart';
import 'package:diplomaticquarterapp/theme/colors.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/in_app_browser/InAppBrowser.dart';
import 'package:diplomaticquarterapp/widgets/others/app_scaffold_widget.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class OrderPreviewPage extends StatefulWidget {
  final List<Addresses> addresses;
  final OrderPreviewViewModel model;

  OrderPreviewPage({this.addresses, this.model});

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

class _OrderPreviewPageState extends State<OrderPreviewPage> {
  MyInAppBrowser browser;
  bool isLoading = true;
  bool isChecked = false;

  @override
  void initState() {
    super.initState();
    getData();
  }

  void getData() async {
    if (isLoading) await Provider.of<OrderPreviewViewModel>(context, listen: false).getShoppingCart();
    setState(() {
      isLoading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    final mediaQuery = MediaQuery.of(context);
    final height = mediaQuery.size.height - 60 - mediaQuery.padding.top;
    ProjectViewModel projectViewModel = Provider.of(context);
    return AppScaffold(
      appBarTitle: "${TranslationBase.of(context).checkOut}",
      isShowAppBar: true,
      isPharmacy: true,
      isShowDecPage: false,
      isLoading: isLoading,
      isLocalLoader: true,
      backgroundColor: Colors.white,
      // baseViewModel: widget.model,
      body: Container(
        color: Color(0xFFF1F1F1),
        height: height * 0.90,
        child: SingleChildScrollView(
          child: Container(
            color: Color(0xFFF1F1F1),
            child: Column(
              children: [
                SelectAddressWidget(
                  widget.model,
                  widget.addresses,
                  changeMainState,
                  isUpdating: true,
                ),
                SizedBox(
                  height: 10,
                ),
                SelectPaymentOptionWidget(
                  widget.model,
                  changeMainState,
                  isUpdating: true,
                ),
                SizedBox(
                  height: 10,
                ),
                widget.model.paymentCheckoutData.lacumInformation != null
                    ? Stack(
                        children: [
                          Container(
                            child: Column(
                              children: [
                                Container(
                                  color: Colors.white,
                                  padding: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                                  child: Row(
                                    children: [
                                      Row(
                                        children: [
                                          SizedBox(
                                            height: 24.0,
                                            width: 24.0,
                                            child: Checkbox(
                                              activeColor: CustomColors.green,
                                              value: isChecked,
                                              onChanged: (bool value) {
                                                setState(() {
                                                  isChecked = value;
                                                  if (value) {
                                                    PaymentBottomWidget.isChecked = true;
                                                  } else {
                                                    PaymentBottomWidget.isChecked = false;
                                                  }
                                                  setState(() {});
                                                });
                                              },
                                            ),
                                          ),
                                          Padding(
                                            padding: const EdgeInsets.only(left: 8.0, right: 8.0),
                                            child: Text(
                                                TranslationBase.of(context).useLakumPoints +
                                                    " (${widget.model.paymentCheckoutData.lacumInformation.lakumInquiryInformationObjVersion.pointsBalance.toString() + " " + TranslationBase.of(context).points})",
                                                style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w600, letterSpacing: -0.56)),
                                          ),
                                        ],
                                      ),
                                      Expanded(
                                        child: Container(
                                          decoration: BoxDecoration(color: Color(0x99ffffff)),
                                          padding: const EdgeInsets.symmetric(horizontal: 8),
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.end,
                                            children: [
                                              Container(
                                                decoration: BoxDecoration(color: Color(0x99ffffff)),
                                                child: Column(
                                                  crossAxisAlignment: CrossAxisAlignment.start,
                                                  children: [
                                                    Texts(
                                                      "${TranslationBase.of(context).availableBalance}",
                                                      fontSize: 12,
                                                      fontWeight: FontWeight.bold,
                                                    ),
                                                    Text(
                                                        "${TranslationBase.of(context).sar + " " + widget.model.paymentCheckoutData.lacumInformation.lakumInquiryInformationObjVersion.pointsBalanceAmount.toString()}",
                                                        style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.w600, letterSpacing: -0.56)),
                                                  ],
                                                ),
                                              ),
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                SizedBox(
                                  height: 10,
                                ),
                              ],
                            ),
                          ),
                          projectViewModel.havePrivilege(83)
                              ? Container()
                              : Container(
                                  height: MediaQuery.of(context).size.height * .10,
                                  color: Colors.white.withOpacity(0.6),
                                )
                        ],
                      )
                    : Container(),
                Container(
                  color: Colors.white,
                  width: double.infinity,
                  padding: EdgeInsets.all(8),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Texts(
                        TranslationBase.of(context).reviewOrder,
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                      ...List.generate(
                        widget.model.cartResponse.shoppingCarts != null ? widget.model.cartResponse.shoppingCarts.length : 0,
                        (index) => ProductOrderPreviewItem(widget.model.cartResponse.shoppingCarts[index]),
                      ),
                    ],
                  ),
                ),
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.all(8),
                  child: widget.model.cartResponse.subtotal != null
                      ? Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Texts(
                              TranslationBase.of(context).orderSummary,
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                              color: Colors.black,
                            ),
                            SizedBox(
                              height: 20,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Texts(
                                  "${TranslationBase.of(context).subtotal}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                                Texts(
                                  "${TranslationBase.of(context).sar} ${(widget.model.cartResponse.subtotal).toStringAsFixed(2)}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                              ],
                            ),
                            const Divider(
                              color: Color(0xFFD6D6D6),
                              height: 20,
                              thickness: 1,
                              indent: 0,
                              endIndent: 0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Texts(
                                  "${TranslationBase.of(context).shipping}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                                Texts(
                                  "${TranslationBase.of(context).sar} ${(widget.model.totalAdditionalShippingCharge).toStringAsFixed(2)}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                              ],
                            ),
                            const Divider(
                              color: Color(0xFFD6D6D6),
                              height: 20,
                              thickness: 1,
                              indent: 0,
                              endIndent: 0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Texts(
                                  "${TranslationBase.of(context).vat}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                                Texts(
                                  "${TranslationBase.of(context).sar} ${(widget.model.cartResponse.subtotalVatAmount).toStringAsFixed(2)}",
                                  fontSize: 14,
                                  color: Colors.black,
                                  fontWeight: FontWeight.w500,
                                ),
                              ],
                            ),
                            const Divider(
                              color: Color(0xFFD6D6D6),
                              height: 20,
                              thickness: 1,
                              indent: 0,
                              endIndent: 0,
                            ),
                            isChecked
                                ? Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Texts(
                                        "${TranslationBase.of(context).lakum}",
                                        fontSize: 14,
                                        color: Colors.green,
                                        fontWeight: FontWeight.w500,
                                      ),
                                      Texts(
                                        "- ${TranslationBase.of(context).sar} ${(widget.model.paymentCheckoutData.lacumInformation.lakumInquiryInformationObjVersion.pointsBalanceAmount).toStringAsFixed(2)}",
                                        fontSize: 14,
                                        color: Colors.green,
                                        fontWeight: FontWeight.w500,
                                      ),
                                    ],
                                  )
                                : Container(),
                            isChecked
                                ? const Divider(
                                    color: Color(0xFFD6D6D6),
                                    height: 20,
                                    thickness: 1,
                                    indent: 0,
                                    endIndent: 0,
                                  )
                                : Container(),
                            isChecked
                                ? Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).total,
                                        fontSize: 14,
                                        color: Colors.black,
                                        fontWeight: FontWeight.bold,
                                      ),
                                      Texts(
                                        " ${TranslationBase.of(context).sar}"
                                        "${(widget.model.cartResponse.totalAmount - widget.model.paymentCheckoutData.lacumInformation.lakumInquiryInformationObjVersion.pointsBalanceAmount).toStringAsFixed(2)}",
                                        fontSize: 14,
                                        color: Colors.black,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ],
                                  )
                                : Row(
                                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).total,
                                        fontSize: 14,
                                        color: Colors.black,
                                        fontWeight: FontWeight.bold,
                                      ),
                                      Texts(
                                        " ${TranslationBase.of(context).sar} ${(widget.model.cartResponse.totalAmount).toStringAsFixed(2)}",
                                        fontSize: 14,
                                        color: Colors.black,
                                        fontWeight: FontWeight.bold,
                                      ),
                                    ],
                                  ),
                            SizedBox(
                              height: 10,
                            ),
                          ],
                        )
                      : Container(),
                ),
                SizedBox(
                  height: widget.model.cartResponse.shoppingCarts != null ? height * 0.10 : 0,
                )
              ],
            ),
          ),
        ),
      ),
      bottomSheet: Container(
        height: widget.model.cartResponse.shoppingCarts != null ? height * 0.10 : 0,
        color: Colors.white,
        child: PaymentBottomWidget(widget.model),
      ),
    );
  }

  changeMainState() {
    setState(() {});
  }
}
