import 'dart:developer';

import 'package:flutter/material.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/service_model.dart';
import 'package:mc_common_app/theme/colors.dart';
import 'package:mc_common_app/utils/enums.dart';
import 'package:mc_common_app/view_models/appointments_view_model.dart';
import 'package:mc_common_app/views/appointments/widgets/custom_calender_widget.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/common_widgets/time_slots.dart';
import 'package:mc_common_app/widgets/extensions/extensions_widget.dart';
import 'package:provider/provider.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:sizer/sizer.dart';

class ScreenArgumentsForAppointmentDetailPage {
  final int routeFlag; // 1 = coming from create appointment || 2 = coming from reschedule appointment
  final int appointmentId; // 1 = coming from create appointment || 2 = coming from reschedule appointment

  ScreenArgumentsForAppointmentDetailPage({required this.routeFlag, required this.appointmentId});
}

class BookAppointmentSchedulesView extends StatelessWidget {
  final ScreenArgumentsForAppointmentDetailPage screenArgumentsForAppointmentDetailPage;

  const BookAppointmentSchedulesView({Key? key, required this.screenArgumentsForAppointmentDetailPage}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CustomAppBar(
          title: LocaleKeys.bookAppointment.tr(),
          isRemoveBackButton: false,
          isDrawerEnabled: false,
          onBackButtonTapped: () => Navigator.pop(context),
        ),
        body: Consumer(
          builder: (BuildContext context, AppointmentsVM appointmentsVM, Widget? child) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                21.height,
                ListView.builder(
                  shrinkWrap: true,
                  itemCount: appointmentsVM.serviceAppointmentScheduleList.length,
                  itemBuilder: (BuildContext context, int scheduleIndex) {
                    ServiceAppointmentScheduleModel scheduleData = appointmentsVM.serviceAppointmentScheduleList[scheduleIndex];
                    return ExpansionTile(
                      tilePadding: const EdgeInsets.symmetric(horizontal: 21, vertical: 10),
                      childrenPadding: const EdgeInsets.only(left: 16, bottom: 10, right: 16),
                      title: Column(
                        children: [
                          Row(
                            children: [
                              Expanded(
                                child: (scheduleData.scheduleName ?? "").toText(fontSize: 20, isBold: true),
                              ),
                            ],
                          ),
                          Row(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              (LocaleKeys.serviceLocation.tr()).toText(fontSize: 12, color: MyColors.lightTextColor, isBold: true),
                              2.width,
                              (scheduleData.appointmentTypeEnum == AppointmentTypeEnum.home ? LocaleKeys.customerLocation.tr() : LocaleKeys.companyLocation.tr())
                                  .toText(fontSize: 12, isBold: true)
                                  .expand(),
                            ],
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              5.height,
                              ListView.builder(
                                physics: const NeverScrollableScrollPhysics(),
                                shrinkWrap: true,
                                itemCount: appointmentsVM.serviceAppointmentScheduleList[scheduleIndex].servicesListInAppointment!.length,
                                itemBuilder: (BuildContext context, int serviceIndex) {
                                  ServiceModel selectedService = appointmentsVM.serviceAppointmentScheduleList[scheduleIndex].servicesListInAppointment![serviceIndex];
                                  return Row(
                                    children: [
                                      Expanded(
                                        child: ("${serviceIndex + 1}. ${selectedService.providerServiceDescription}").toText(fontSize: 15, isBold: true, color: MyColors.lightTextColor),
                                      ),
                                    ],
                                  );
                                },
                              ),
                            ],
                          ),
                        ],
                      ),
                      children: [
                        Column(
                          children: [
                            CustomCalenderAppointmentWidget(
                                customTimeDateSlotList: scheduleData.customTimeDateSlotList ?? [],
                                onDateSelected: (int dateIndex) {
                                  appointmentsVM.updateSelectedAppointmentDate(scheduleIndex: scheduleIndex, dateIndex: dateIndex);
                                },
                                selectedCustomTimeDateSlotModel: scheduleData.selectedCustomTimeDateSlotModel),
                            if (appointmentsVM.serviceAppointmentScheduleList[scheduleIndex].selectedDateIndex != null) ...[
                              5.height,
                              Row(
                                mainAxisAlignment: MainAxisAlignment.start,
                                children: [
                                  (LocaleKeys.availableSlots.tr()).toText(fontSize: 14, isBold: true),
                                ],
                              ),
                              5.height,
                              SizedBox(
                                width: double.infinity,
                                child: BuildTimeSlots(
                                  timeSlots: appointmentsVM.serviceAppointmentScheduleList[scheduleIndex]
                                          .customTimeDateSlotList![appointmentsVM.serviceAppointmentScheduleList[scheduleIndex].selectedDateIndex!].availableSlots ??
                                      [],
                                  onPressed: (slotIndex) {
                                    appointmentsVM.updateSelectedAppointmentSlotByDate(scheduleIndex: scheduleIndex, slotIndex: slotIndex);
                                  },
                                ),
                              ),
                            ],
                          ],
                        ),
                      ],
                    ).toWhiteContainer(width: double.infinity, margin: const EdgeInsets.symmetric(horizontal: 21, vertical: 10));
                  },
                ).expand(),
                Row(
                  children: [
                    Expanded(
                      child: ShowFillButton(
                        txtColor: MyColors.black,
                        maxHeight: 55,
                        title: LocaleKeys.cancel.tr(),
                        onPressed: () => Navigator.pop(context),
                        backgroundColor: MyColors.greyButtonColor,
                      ),
                    ),
                    12.width,
                    Expanded(
                      child: ShowFillButton(
                        maxHeight: 55,
                        title: screenArgumentsForAppointmentDetailPage.routeFlag == 1 ? LocaleKeys.review.tr() : LocaleKeys.confirm.tr(),
                        onPressed: () {
                          if (screenArgumentsForAppointmentDetailPage.routeFlag == 1) {
                            appointmentsVM.onReviewButtonPressed(context);
                          } else {
                            appointmentsVM.onRescheduleAppointmentConfirmPressed(
                              context: context,
                              appointmentId: screenArgumentsForAppointmentDetailPage.appointmentId,
                              selectedSlotId: appointmentsVM.serviceAppointmentScheduleList.first.selectedCustomTimeDateSlotModel!.date!.slotId,
                            );
                          }
                        },
                        backgroundColor: MyColors.darkPrimaryColor,
                      ),
                    )
                  ],
                ).paddingAll(21)
              ],
            );
          },
        ));
  }
}
