import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/extensions/int_extensions.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/generated/locale_keys.g.dart';
import 'package:mc_common_app/models/appointments_models/service_schedule_model.dart';
import 'package:mc_common_app/models/services_models/item_model.dart';
import 'package:mc_common_app/models/services_models/service_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/view_models/appointments_view_model.dart';
import 'package:mc_common_app/widgets/button/show_fill_button.dart';
import 'package:mc_common_app/widgets/common_widgets/app_bar.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';

class ReviewAppointment extends StatelessWidget {
  const ReviewAppointment({Key? key}) : super(key: key);

  Widget buildBranchInfoCard({required BuildContext context}) {
    AppointmentsVM appointmentsVM = context.read<AppointmentsVM>();
    return Padding(
        padding: const EdgeInsets.only(
          bottom: 10,
          left: 21,
          right: 21,
        ),
        child: Row(
          children: [
            Image.asset(
              MyAssets.bnCar,
              width: 80,
              height: 60,
              fit: BoxFit.cover,
            ),
            12.width,
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            (appointmentsVM.selectedBranchModel!.branchName ??
                                    "")
                                .toText(fontSize: 16, isBold: true),
                            Row(
                              children: [
                                LocaleKeys.location.tr().toText(
                                    color: MyColors.lightTextColor,
                                    fontSize: 12),
                                2.width,
                                ": ${appointmentsVM.selectedBranchModel!.branchDescription ?? ""}"
                                    .toText(fontSize: 12),
                              ],
                            ),
                          ],
                        ),
                      ),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          "4.9".toText(
                            isUnderLine: true,
                            isBold: true,
                            fontSize: 12,
                          ),
                          2.width,
                          MyAssets.starIcon.buildSvg(width: 12),
                        ],
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        )
            .onPress(() {})
            .toWhiteContainer(width: double.infinity, allPading: 12));
  }

  Widget buildServicesInfoCard({required BuildContext context}) {
    AppointmentsVM appointmentsVM = context.read<AppointmentsVM>();
    return ListView.builder(
      shrinkWrap: true,
      itemCount: appointmentsVM.serviceAppointmentScheduleList.length,
      itemBuilder: (BuildContext context, int scheduleIndex) {
        ServiceAppointmentScheduleModel scheduleData =
            appointmentsVM.serviceAppointmentScheduleList[scheduleIndex];
        return Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Expanded(
                  child: LocaleKeys.services.tr().toText(
                      fontSize: 14,
                      isBold: true,
                      color: MyColors.lightTextColor),
                ),
              ],
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ListView.separated(
                  physics: NeverScrollableScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: scheduleData.servicesListInAppointment!.length,
                  itemBuilder: (BuildContext context, int serviceIndex) {
                    String selectedTimeSlot = "";
                    if (scheduleData
                            .selectedCustomTimeDateSlotModel!.availableSlots !=
                        null) {
                      selectedTimeSlot = scheduleData
                          .selectedCustomTimeDateSlotModel!.availableSlots!
                          .firstWhere((element) => element.isSelected)
                          .slot;
                    }
                    return Column(
                      children: [
                        if (scheduleData
                            .servicesListInAppointment!.isNotEmpty) ...[
                          Column(
                            children: List.generate(
                                scheduleData.servicesListInAppointment!.length,
                                (serviceIndex) {
                              ServiceModel serviceData = scheduleData
                                  .servicesListInAppointment![serviceIndex];
                              return Column(
                                children: [
                                  Row(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.start,
                                    children: [
                                      "${serviceData.providerServiceDescription}"
                                          .toText(fontSize: 16, isBold: true),
                                    ],
                                  ),
                                  Column(
                                    children: List.generate(
                                        serviceData.serviceItems!.length,
                                        (itemIndex) {
                                      ItemData itemData =
                                          serviceData.serviceItems![itemIndex];
                                      return Column(
                                        children: [
                                          Row(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.start,
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: [
                                              ("${itemData.name}: ${itemData.price} " +
                                                      LocaleKeys.sar.tr())
                                                  .toText(
                                                      fontSize: 13,
                                                      isBold: true,
                                                      color: MyColors
                                                          .lightTextColor),
                                            ],
                                          ),
                                        ],
                                      );
                                    }),
                                  ),
                                ],
                              );
                            }),
                          ).paddingOnly(bottom: 10),
                        ],
                        5.height,
                        SizedBox(
                          width: double.infinity,
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              LocaleKeys.timeLocation
                                  .tr()
                                  .toText(fontSize: 16, isBold: true),
                              3.height,
                              Row(children: [
                                LocaleKeys.dateAndTime.tr().toText(
                                    fontSize: 12,
                                    color: MyColors.lightTextColor,
                                    isBold: true),
                                "${scheduleData.selectedCustomTimeDateSlotModel!.date!.date} at ${selectedTimeSlot}"
                                    .toText(fontSize: 12, isBold: true),
                              ]),
                              Row(children: [
                                (LocaleKeys.location.tr() + " ").toText(
                                    fontSize: 12,
                                    color: MyColors.lightTextColor,
                                    isBold: true),
                                (scheduleData.appointmentType == 2
                                        ? LocaleKeys.home.tr()
                                        : LocaleKeys.workshop.tr())
                                    .toText(fontSize: 12, isBold: true),
                              ]),
                            ],
                          ),
                        ),
                        10.height,
                        Divider(thickness: 0.7),
                        Builder(builder: (BuildContext context) {
                          double totalServicePrice = 0.0;
                          double totalKms = 15.3;
                          double rangePricePerKm = 5;
                          totalServicePrice = rangePricePerKm * totalKms;

                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              LocaleKeys.amountVAR
                                  .tr()
                                  .toText(fontSize: 16, isBold: true),
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: [
                                  LocaleKeys.serviceCharges.tr().toText(
                                      fontSize: 14,
                                      color: MyColors.lightTextColor,
                                      isBold: true),
                                  "${scheduleData.amountTotal.toString()} SAR"
                                      .toText(fontSize: 16, isBold: true),
                                ],
                              ),
                              if (scheduleData.appointmentType == 1) ...[
                                Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    (LocaleKeys.locationCharges.tr() +
                                            " ($rangePricePerKm x $totalKms )")
                                        .toText(
                                            fontSize: 14,
                                            color: MyColors.lightTextColor,
                                            isBold: true),
                                    ("${totalServicePrice.toString()} " +
                                            LocaleKeys.sar.tr())
                                        .toText(fontSize: 16, isBold: true),
                                  ],
                                ),
                              ],
                              10.height,
                            ],
                          );
                        }),
                      ],
                    );
                  },
                  separatorBuilder: (BuildContext context, int index) =>
                      Divider(thickness: 2),
                ).paddingOnly(bottom: 10),
              ],
            ),
          ],
        ).toWhiteContainer(
            width: double.infinity,
            allPading: 12,
            margin: const EdgeInsets.symmetric(horizontal: 21, vertical: 0));
      },
    );
  }

  Widget buildNextButtonFooter({required BuildContext context}) {
    AppointmentsVM appointmentsVM = context.read<AppointmentsVM>();
    return Container(
      color: MyColors.white,
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Divider(thickness: 0.7, height: 3),
          8.height,
          if (appointmentsVM.amountToPayForAppointment > 0) ...[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                LocaleKeys.payableNow.tr().toText(fontSize: 14, isBold: true),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    appointmentsVM.amountToPayForAppointment
                        .toString()
                        .toText(fontSize: 16, isBold: true),
                    2.width,
                    LocaleKeys.sar
                        .tr()
                        .toText(
                            color: MyColors.lightTextColor,
                            fontSize: 12,
                            isBold: true)
                        .paddingOnly(bottom: 2),
                  ],
                )
              ],
            ).paddingOnly(left: 21, right: 21),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                LocaleKeys.remainingAmount
                    .tr()
                    .toText(fontSize: 14, isBold: true),
                Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    (appointmentsVM.totalAmount -
                            appointmentsVM.amountToPayForAppointment)
                        .toString()
                        .toText(fontSize: 16, isBold: true),
                    2.width,
                    LocaleKeys.sar
                        .tr()
                        .toText(
                            color: MyColors.lightTextColor,
                            fontSize: 12,
                            isBold: true)
                        .paddingOnly(bottom: 2),
                  ],
                )
              ],
            ).paddingOnly(left: 21, right: 21),
          ],
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              LocaleKeys.totalAmount.tr().toText(fontSize: 18, isBold: true),
              Row(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  appointmentsVM.totalAmount
                      .toString()
                      .toText(fontSize: 29, isBold: true),
                  2.width,
                  LocaleKeys.sar
                      .tr()
                      .toText(
                          color: MyColors.lightTextColor,
                          fontSize: 16,
                          isBold: true)
                      .paddingOnly(bottom: 5),
                ],
              )
            ],
          ).paddingOnly(left: 21, right: 21),
          5.height,
          SizedBox(
            width: double.infinity,
            child: ShowFillButton(
              maxHeight: 55,
              backgroundColor: MyColors.darkPrimaryColor,
              title: LocaleKeys.bookAppointment.tr(),
              onPressed: () {
                appointmentsVM.onBookAppointmentPressed(context);
              },
            ).paddingOnly(bottom: 12, left: 21, right: 21),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppBar(
        title: LocaleKeys.reviewAppointment.tr(),
        isRemoveBackButton: false,
        isDrawerEnabled: false,
        onBackButtonTapped: () => Navigator.pop(context),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ListView(
            children: [
              buildBranchInfoCard(context: context),
              buildServicesInfoCard(context: context),
            ],
          ).expand(),
          buildNextButtonFooter(context: context),
        ],
      ),
    );
  }
}
