import 'package:mc_common_app/models/appointments_models/service_schedule_model.dart';
import 'package:mc_common_app/models/general_models/widgets_models.dart';

class SSCarCheckScheduleModel {
  int? serviceProviderID;
  int? serviceProviderBranchID;
  int? branchAppointmentScheduleID;
  String? branchName;
  String? address;
  String? latitude;
  String? longitude;
  List<ScheduleServices>? scheduleServices;
  List<CarCheckOfficeScheduleSlots>? branchScheduleSlots;
  int? totalItemsCount;
  int? distanceKM;
  List<CustomTimeDateSlotModel>? customTimeDateSlotList;
  CustomTimeDateSlotModel? selectedCustomTimeDateSlotModel;

  SSCarCheckScheduleModel({
    this.serviceProviderID,
    this.serviceProviderBranchID,
    this.branchAppointmentScheduleID,
    this.branchName,
    this.address,
    this.latitude,
    this.longitude,
    this.scheduleServices,
    this.branchScheduleSlots,
    this.distanceKM,
    this.totalItemsCount,
    this.selectedCustomTimeDateSlotModel,
    this.customTimeDateSlotList,
  });

  SSCarCheckScheduleModel.fromJson(Map<String, dynamic> json) {
    serviceProviderID = json['serviceProviderID'];
    serviceProviderBranchID = json['serviceProviderBranchID'];
    branchAppointmentScheduleID = json['branchAppointmentScheduleID'];
    branchName = json['branchName'];
    address = json['address'];
    latitude = json['latitude'];
    longitude = json['longitude'];
    if (json['scheduleServices'] != null) {
      scheduleServices = <ScheduleServices>[];
      json['scheduleServices'].forEach((v) {
        scheduleServices!.add(ScheduleServices.fromJson(v));
      });
    }
    if (json['branchScheduleSlots'] != null) {
      branchScheduleSlots = <CarCheckOfficeScheduleSlots>[];
      json['branchScheduleSlots'].forEach((v) {
        branchScheduleSlots!.add(CarCheckOfficeScheduleSlots.fromJson(v));
      });
    }
    totalItemsCount = json['totalItemsCount'];

    //TODO: We will update this when Backend team will add this value
    distanceKM = 0;
    customTimeDateSlotList = getFormattedDateTimeSlotPackage();
  }

  List<CustomTimeDateSlotModel> getFormattedDateTimeSlotPackage() {
    List<CustomTimeDateSlotModel> customTimeDateSlotList = [];
    for (var element in branchScheduleSlots!) {
      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 branchScheduleSlots!) {
      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;
  }

  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;
    }
  }
}

class ScheduleServices {
  int? providerServiceID;
  String? providerServiceName;

  ScheduleServices({this.providerServiceID, this.providerServiceName});

  ScheduleServices.fromJson(Map<String, dynamic> json) {
    providerServiceID = json['providerServiceID'];
    providerServiceName = json['providerServiceName'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['providerServiceID'] = providerServiceID;
    data['providerServiceName'] = providerServiceName;
    return data;
  }
}

class CarCheckOfficeScheduleSlots {
  int? id;
  String? slotDate;
  String? startTime;
  String? endTime;
  int? bookAppointment;
  int? allowAppointment;
  int? slotDurationMinute;

  CarCheckOfficeScheduleSlots({this.id, this.slotDate, this.startTime, this.endTime, this.bookAppointment, this.allowAppointment, this.slotDurationMinute});

  CarCheckOfficeScheduleSlots.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    slotDate = json['slotDate'];
    startTime = json['startTime'];
    endTime = json['endTime'];
    bookAppointment = json['bookAppointment'];
    allowAppointment = json['allowAppointment'];
    slotDurationMinute = json['slotDurationMinute'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['id'] = id;
    data['slotDate'] = slotDate;
    data['startTime'] = startTime;
    data['endTime'] = endTime;
    data['bookAppointment'] = bookAppointment;
    data['allowAppointment'] = allowAppointment;
    data['slotDurationMinute'] = slotDurationMinute;
    return data;
  }
}
