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

class SSPhotoOfficeScheduleModel {
  int? photoOfficeID;
  String? fromDate;
  String? toDate;
  int? photoOfficeAppointmentScheduleID;
  String? photoOfficeName;
  String? description;
  String? areaName;
  String? latitude;
  String? longitude;
  double? distanceKM;
  int? totalItemsCount;
  List<PhotoOfficeScheduleSlots>? photoOfficeScheduleSlots;
  List<CustomTimeDateSlotModel>? customTimeDateSlotList;
  CustomTimeDateSlotModel? selectedCustomTimeDateSlotModel;
  int? selectedDateIndex;

  SSPhotoOfficeScheduleModel({
    this.photoOfficeID,
    this.fromDate,
    this.toDate,
    this.photoOfficeAppointmentScheduleID,
    this.photoOfficeName,
    this.description,
    this.areaName,
    this.latitude,
    this.longitude,
    this.distanceKM,
    this.totalItemsCount,
    this.photoOfficeScheduleSlots,
    this.customTimeDateSlotList,
    this.selectedCustomTimeDateSlotModel,
    this.selectedDateIndex,
  });

  SSPhotoOfficeScheduleModel.fromJson(Map<String, dynamic> json) {
    photoOfficeID = json['photoOfficeID'];
    fromDate = json['fromDate'];
    toDate = json['toDate'];
    photoOfficeAppointmentScheduleID = json['photoOfficeAppointmentScheduleID'];
    photoOfficeName = json['photoOfficeName'];
    description = json['description'];
    areaName = json['areaName'];
    latitude = json['latitude'];
    longitude = json['longitude'];
    distanceKM = json['distanceKM'];
    totalItemsCount = json['totalItemsCount'];
    if (json['photoOfficeScheduleSlots'] != null) {
      photoOfficeScheduleSlots = <PhotoOfficeScheduleSlots>[];
      json['photoOfficeScheduleSlots'].forEach((v) {
        photoOfficeScheduleSlots!.add(PhotoOfficeScheduleSlots.fromJson(v));
      });
    }
    customTimeDateSlotList = getFormattedDateTimeSlotPackage();
    selectedDateIndex = null;
  }

  List<CustomTimeDateSlotModel> getFormattedDateTimeSlotPackage() {
    List<CustomTimeDateSlotModel> customTimeDateSlotList = [];
    for (var element in photoOfficeScheduleSlots!) {
      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 photoOfficeScheduleSlots!) {
      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 PhotoOfficeScheduleSlots {
  int? id;
  String? slotDate;
  String? startTime;
  String? endTime;
  int? bookAppointment;
  int? allowAppointment;
  int? slotDurationMinute;

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

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