import 'package:diplomaticquarterapp/core/model/pharmacies/Addresses.dart';
import 'package:diplomaticquarterapp/core/service/AlHabibMedicalService/customer_addresses_service.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:diplomaticquarterapp/pages/pharmacy/pharmacyAddresses/PharmacyAddresses.dart';
import 'package:diplomaticquarterapp/uitl/gif_loader_dialog_utils.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class SelectAddressWidget extends StatefulWidget {
  final OrderPreviewViewModel model;
  final List<Addresses> addresses;
  final Function changeMainState;
  final bool isUpdating;

  SelectAddressWidget(this.model, this.addresses, this.changeMainState,
      {this.isUpdating = false});

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

class _SelectAddressWidgetState extends State<SelectAddressWidget> {
  AddressInfo address;

  _navigateToAddressPage(String identificationNo) {
    Navigator.push(context, FadePage(page: PharmacyAddressesPage(orderPreviewViewModel: widget.model,isUpdate: widget.isUpdating,changeMainState: widget.changeMainState,))).then((result) async {
      if (result != null) {
        GifLoaderDialogUtils.showMyDialog(context);
        address = result;
        widget.model.paymentCheckoutData.address = Addresses.fromJson(address.toJson());
        await widget.model.getInformationsByAddress(identificationNo);
        await widget.model.getShoppingCart();
        widget.changeMainState();
        GifLoaderDialogUtils.hideDialog(context);
      }
    });
  }

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

  @override
  Widget build(BuildContext context) {
    OrderPreviewViewModel model = Provider.of(context);
    if (widget.model.paymentCheckoutData.address != null) {
      address = AddressInfo.fromJson(widget.model.paymentCheckoutData.address.toJson());
    }
    return Container(
      color: Colors.white,
      child: address == null
          ? InkWell(
              onTap: () => {_navigateToAddressPage(model.user.patientIdentificationNo)},
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/pharmacy_module/ic_shipping_address.png",
                      width: 30.0,
                      height: 30.0,
                      fit: BoxFit.scaleDown,
                    ),
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 0, horizontal: 6),
                        child: Texts(
                          TranslationBase.of(context).selectAddress,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Color(0xff0000ff),
                        ),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 20,
                      color: Colors.grey.shade400,
                    ),
                  ],
                ),
              ),
            )
          : Container(
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Image.asset(
                          "assets/images/pharmacy_module/ic_shipping_mark.png",
                          width: 30.0,
                          height: 30.0,
                          fit: BoxFit.scaleDown,
                        ),
                        Expanded(
                          child: Container(
                            padding: EdgeInsets.symmetric(vertical: 0, horizontal: 6),
                            child: Texts(
                              TranslationBase.of(context).shippingAddress,
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                              color: Colors.black,
                            ),
                          ),
                        ),
                        InkWell(
                          onTap: () => {_navigateToAddressPage(model.authenticatedUserObject.user.patientIdentificationNo)},
                          child: Texts(
                            TranslationBase.of(context).changeAddress,
                            fontSize: 12,
                            fontWeight: FontWeight.normal,
                            color: Color(0xff0000ff),
                          ),
                        ),
                      ],
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Texts(
                        "${address.firstName} ${address.lastName}",
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(vertical: 8),
                      child: Texts(
                        "${address.address1} ${address.address2} ${address.address2},, ${address.city}, ${address.country} ${address.zipPostalCode}",
                        fontSize: 12,
                        fontWeight: FontWeight.normal,
                        color: Colors.grey.shade500,
                      ),
                    ),
                    Row(
                      children: [
                        Container(
                          margin: const EdgeInsets.only(right: 8),
                          child: Icon(
                            Icons.phone,
                            size: 20,
                            color: Colors.black,
                          ),
                        ),
                        Texts(
                          "${address.phoneNumber}",
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Colors.grey,
                        ),
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.symmetric(vertical: 8),
                      child: SizedBox(
                        height: 2,
                        width: double.infinity,
                        child: Container(
                          color: Color(0xffefefef),
                        ),
                      ),
                    ),
                    Row(
                      children: [
                        Image.asset(
                          "assets/images/pharmacy_module/ic_shipping_truck.png",
                          width: 30.0,
                          height: 30.0,
                          fit: BoxFit.scaleDown,
                        ),
                        Container(
                          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 6),
                          child: Texts(
                            "${TranslationBase.of(context).shipBy}",
                            fontSize: 12,
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                          ),
                        ),
                        Container(
                          child: Image.asset(
                            model.paymentCheckoutData.shippingOption == null
                                ? ""
                                : model.paymentCheckoutData.shippingOption.shippingRateComputationMethodSystemName == "Shipping.FixedOrByWeight"
                                    ? "assets/images/pharmacy_module/payment/LogoParmacyGreen.png"
                                    : "assets/images/pharmacy_module/payment/aramex_shipping_logo.png",
                            fit: BoxFit.contain,
                            width: 100,
                          ),
                          margin: EdgeInsets.symmetric(horizontal: 8),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ), // ic_shipping_mark.png
    );
  }
}
