import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/models/services_models/service_model.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';
import 'package:mc_common_app/utils/enums.dart';

class CustomTimeDateSlotModel {
  TimeSlotModel? date;
  List<TimeSlotModel>? availableSlots;

  CustomTimeDateSlotModel({this.date, this.availableSlots});
}

class ServiceAppointmentScheduleModel {
  String? scheduleName;
  List<ServiceSlotList>? serviceSlotList;
  List<ServiceModel>? servicesListInAppointment;
  int? selectedDateIndex;
  List<CustomTimeDateSlotModel>? customTimeDateSlotList;
  CustomTimeDateSlotModel? selectedCustomTimeDateSlotModel;
  double? amountToPay;
  double? amountTotal;
  double? amountRem;
  int? appointmentType;
  double? totalLocationCharges;
  CurrentLocationInfoModel? locationInfoModel;
  AppointmentTypeEnum? appointmentTypeEnum;

  ServiceAppointmentScheduleModel({
    this.scheduleName,
    this.serviceSlotList,
    this.servicesListInAppointment,
    this.selectedDateIndex,
    this.customTimeDateSlotList,
    this.selectedCustomTimeDateSlotModel,
    this.amountToPay,
    this.amountTotal,
    this.amountRem,
    this.appointmentType,
    this.totalLocationCharges,
    this.locationInfoModel,
    this.appointmentTypeEnum,
  });

  List<CustomTimeDateSlotModel> getFormattedDateTimeSlotPackage() {
    List<CustomTimeDateSlotModel> customTimeDateSlotList = [];
    for (var element in serviceSlotList!) {
      if (!isAlreadyThere(element.slotDate!, customTimeDateSlotList)) {
        customTimeDateSlotList.add(CustomTimeDateSlotModel(
          date: TimeSlotModel(slotId: element.id!, isSelected: false, date: element.slotDate!, allowAppointment: (element.allowAppointment ?? 0) > (element.bookAppointment ?? 0)),
          availableSlots: getAvailableSlotsByDate(element.slotDate!),
        ));
      }
    }

    return customTimeDateSlotList;
  }

  List<TimeSlotModel>? getAvailableSlotsByDate(String date) {
    List<TimeSlotModel> timeslots = [];
    for (var element in serviceSlotList!) {
      if (element.slotDate == date) {
        timeslots.add(TimeSlotModel(slotId: element.id!, slot: element.startTime!, date: element.slotDate!, isSelected: false, allowAppointment: element.bookAppointment! < element.allowAppointment!));
      }
    }
    return timeslots;
  }

  List<TimeSlotModel> getFormattedSlotTimes() {
    var seenSlots = <TimeSlotModel>{};

    var slotTimeData = serviceSlotList!
        .where((slot) => seenSlots.add(TimeSlotModel(
              slot: slot.startTime!,
              slotId: slot.id!,
              isSelected: false,
              date: slot.slotDate!.toFormattedDateWithoutTime(),
            )))
        .toList();
    List<TimeSlotModel> slotTime = [];
    for (var element in slotTimeData) {
      slotTime.add(TimeSlotModel(isSelected: false, slotId: element.id!, slot: element.startTime ?? "", date: element.slotDate!.toFormattedDateWithoutTime()));
    }

    return slotTime;
  }

  bool isAlreadyThere(String dateToFind, List<CustomTimeDateSlotModel> slots) {
    int index = slots.indexWhere((element) {
      return element.date!.date == dateToFind;
    });
    if (index == -1) {
      return false;
    } else {
      return true;
    }
  }

  ServiceAppointmentScheduleModel.fromJson(Map<String, dynamic> json, {bool isForAppointment = false}) {
    scheduleName = json['scheduleName'];

    if (json['serviceSlotList'] != null) {
      serviceSlotList = <ServiceSlotList>[];
      json['serviceSlotList'].forEach((v) {
        serviceSlotList!.add(ServiceSlotList.fromJson(v));
      });
    }
    if (json['serviceList'] != null) {
      servicesListInAppointment = <ServiceModel>[];
      json['serviceList'].forEach((v) {
        servicesListInAppointment!.add(ServiceModel.fromJson(v, isForAppointment: isForAppointment));
      });
    }
    customTimeDateSlotList = getFormattedDateTimeSlotPackage();
    amountToPay = json['amountToPay'];
    selectedDateIndex = null;
    amountTotal = json['amountTotal'];
    amountRem = json['amountRem'];
    appointmentType = json['appointmentType'];
    totalLocationCharges = 0.0;
    locationInfoModel = CurrentLocationInfoModel(address: "", latitude: 0.0, longitude: 0.0, distanceToBranch: 0.0, homeChargesInCurrentService: 0.0);
    appointmentTypeEnum = json['appointmentType'] != null ? (json['appointmentType'] as int).toAppointmentTypeEnum() : AppointmentTypeEnum.workshop;
  }
}

class ServiceSlotList {
  int? id;
  int? branchAppointmentScheduleID;
  String? branchAppointmentSchedule;
  int? serviceProviderID;
  String? slotDate;
  String? startTime;
  String? endTime;
  int? bookAppointment;
  int? allowAppointment;
  int? slotDurationMinute;
  int? appointmentType;
  bool? isActive;
  int? createdBy;
  String? createdOn;
  int? modifiedBy;
  String? modifiedOn;

  ServiceSlotList(
      {this.id,
      this.branchAppointmentScheduleID,
      this.branchAppointmentSchedule,
      this.serviceProviderID,
      this.slotDate,
      this.startTime,
      this.endTime,
      this.bookAppointment,
      this.allowAppointment,
      this.slotDurationMinute,
      this.appointmentType,
      this.isActive,
      this.createdBy,
      this.createdOn,
      this.modifiedBy,
      this.modifiedOn});

  ServiceSlotList.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    branchAppointmentScheduleID = json['branchAppointmentScheduleID'];
    branchAppointmentSchedule = json['branchAppointmentSchedule'];
    serviceProviderID = json['serviceProviderID'];
    slotDate = (json['slotDate'] as String).toFormattedDateWithoutTime();
    startTime = json['startTime'];
    endTime = json['endTime'];
    bookAppointment = json['bookAppointment'];
    allowAppointment = json['allowAppointment'];
    slotDurationMinute = json['slotDurationMinute'];
    appointmentType = json['appointmentType'];
    isActive = json['isActive'];
    createdBy = json['createdBy'];
    createdOn = json['createdOn'];
    modifiedBy = json['modifiedBy'];
    modifiedOn = json['modifiedOn'];
  }

  @override
  String toString() {
    return 'ServiceSlotList{id: $id, branchAppointmentScheduleID: $branchAppointmentScheduleID, branchAppointmentSchedule: $branchAppointmentSchedule, serviceProviderID: $serviceProviderID, slotDate: $slotDate, startTime: $startTime, endTime: $endTime, bookAppointment: $bookAppointment, allowAppointment: $allowAppointment, slotDurationMinute: $slotDurationMinute, appointmentType: $appointmentType, isActive: $isActive, createdBy: $createdBy, createdOn: $createdOn, modifiedBy: $modifiedBy, modifiedOn: $modifiedOn}';
  }
}
