import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:flutter/foundation.dart';

class ScheduleModel {
  final String scheduleId;
  final String category;
  final String time;
  final Status function;
  final List<String> days;
  final List<bool>? selectedDays;
  final String timezoneId;
  final bool enable;

  ScheduleModel({
    required this.scheduleId,
    required this.category,
    required this.time,
    required this.function,
    required this.days,
    this.selectedDays,
    required this.timezoneId,
    required this.enable,
  });

  Map<String, dynamic> toMap() {
    return {
      'scheduleId': scheduleId,
      'category': category,
      'time': time,
      'function': function.toMap(),
      'days': days,
      'timezoneId': timezoneId,
      'enable': enable,
    };
  }

  factory ScheduleModel.fromMap(Map<String, dynamic> map) {
    return ScheduleModel(
      scheduleId: map['scheduleId'] ?? '',
      category: map['category'] ?? '',
      time: map['time'] ?? '',
      function: Status.fromMap(map['function']),
      days: List<String>.from(map['days'].map((e) => e.toString())),
      timezoneId: map['timezoneId'] ?? '',
      enable: map['enable'] ?? false,
      selectedDays: parseSelectedDays(
          List<String>.from(map['days'].map((e) => e.toString()))),
    );
  }

  String toJson() => json.encode(toMap());

  factory ScheduleModel.fromJson(String source) =>
      ScheduleModel.fromMap(json.decode(source));

  ScheduleModel copyWith({
    String? scheduleId,
    String? category,
    String? time,
    Status? function,
    List<String>? days,
    List<bool>? selectedDays,
    String? timezoneId,
    bool? enable,
  }) {
    return ScheduleModel(
      scheduleId: scheduleId ?? this.scheduleId,
      category: category ?? this.category,
      time: time ?? this.time,
      function: function ?? this.function,
      days: days ?? this.days,
      selectedDays: selectedDays ?? this.selectedDays,
      timezoneId: timezoneId ?? this.timezoneId,
      enable: enable ?? this.enable,
    );
  }

  static TimeOfDay? parseTimeOfDay(String isoTime) {
    try {
      final dateTime = DateTime.parse(isoTime);
      return TimeOfDay(hour: dateTime.hour, minute: dateTime.minute);
    } catch (e) {
      return null;
    }
  }

  static List<bool> parseSelectedDays(List<String> days) {
    const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    return allDays.map((day) => days.contains(day)).toList();
  }

  static List<String> convertSelectedDaysToStrings(List<bool> selectedDays) {
    const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
    List<String> result = [];
    for (int i = 0; i < selectedDays.length; i++) {
      if (selectedDays[i]) {
        result.add(allDays[i]);
      }
    }
    return result;
  }

  @override
  String toString() {
    return 'ScheduleModel(category: $category, time: $time, function: $function, days: $days, selectedDays: $selectedDays)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is ScheduleModel &&
        other.category == category &&
        other.time == time &&
        other.function == function &&
        listEquals(other.days, days) &&
        //  timeOfDay == other.timeOfDay &&
        listEquals(other.selectedDays, selectedDays);
  }

  @override
  int get hashCode {
    return category.hashCode ^
        time.hashCode ^
        function.hashCode ^
        days.hashCode ^
        //   timeOfDay.hashCode ^
        selectedDays.hashCode;
  }
}
