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 days; final TimeOfDay? timeOfDay; final List? selectedDays; ScheduleModel({ required this.category, required this.time, required this.function, required this.days, this.timeOfDay, this.selectedDays, this.scheduleId = '', }); Map toMap() { return { 'category': category, 'time': time, 'function': function.toMap(), 'days': days, }; } factory ScheduleModel.fromMap(Map map) { return ScheduleModel( scheduleId: map['scheduleId'] ?? '', category: map['category'] ?? '', time: map['time'] ?? '', function: Status.fromMap(map['function']), days: List.from(map['days']), timeOfDay: parseTimeOfDay(map['time']), selectedDays: parseSelectedDays(map['days']), ); } String toJson() => json.encode(toMap()); factory ScheduleModel.fromJson(String source) => ScheduleModel.fromMap(json.decode(source)); ScheduleModel copyWith({ String? category, String? time, Status? function, List? days, TimeOfDay? timeOfDay, List? selectedDays, String? scheduleId, }) { return ScheduleModel( category: category ?? this.category, time: time ?? this.time, function: function ?? this.function, days: days ?? this.days, timeOfDay: timeOfDay ?? this.timeOfDay, selectedDays: selectedDays ?? this.selectedDays, scheduleId: scheduleId ?? this.scheduleId, ); } 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 parseSelectedDays(List days) { const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; return allDays.map((day) => days.contains(day)).toList(); } static List convertSelectedDaysToStrings(List selectedDays) { const allDays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; List 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, timeOfDay: $timeOfDay, 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; } }