import 'package:syncrow_web/pages/device_managment/all_devices/models/device_status.dart';
import 'package:syncrow_web/pages/device_managment/water_heater/models/schedule_model.dart';

class GarageDoorStatusModel {
  final String uuid;
  final bool switch1;
  final int countdown1;
  final bool doorContactState;
  final int trTimeCon;
  final int countdownAlarm;
  final String doorControl1;
  final bool voiceControl1;
  final String doorState1;
  // final bool isOpen;
  final Duration delay;
  final List<ScheduleModel>? schedules; // Add schedules field

  GarageDoorStatusModel({
    required this.uuid,
    required this.switch1,
    required this.countdown1,
    required this.doorContactState,
    required this.trTimeCon,
    required this.countdownAlarm,
    required this.doorControl1,
    required this.voiceControl1,
    required this.doorState1,
    //  required this.isOpen,
    required this.delay,
    required this.schedules, // Initialize schedules
  });

  factory GarageDoorStatusModel.fromJson(String id, List<Status> jsonList) {
    late bool switch1;
    late int countdown1;
    late bool doorContactState;
    late int trTimeCon;
    late int countdownAlarm;
    late String doorControl1;
    late bool voiceControl1;
    late String doorState1;
    List<ScheduleModel> schedules = []; // Initialize schedules

    for (var status in jsonList) {
      switch (status.code) {
        case 'switch_1':
          switch1 = status.value ?? false;
          break;
        case 'countdown_1':
          countdown1 = status.value ?? 0;
          break;
        case 'doorcontact_state':
          doorContactState = status.value ?? false;
          break;
        case 'tr_timecon':
          trTimeCon = status.value ?? 0;
          break;
        case 'countdown_alarm':
          countdownAlarm = status.value ?? 0;
          break;
        case 'door_control_1':
          doorControl1 = status.value ?? 'closed';
          break;
        case 'voice_control_1':
          voiceControl1 = status.value ?? false;
          break;
        case 'door_state_1':
          doorState1 = status.value ?? 'close_time_alarm';
          break;
      }
    }

    return GarageDoorStatusModel(
      uuid: id,
      switch1: switch1,
      countdown1: countdown1,
      doorContactState: doorContactState,
      trTimeCon: trTimeCon,
      countdownAlarm: countdownAlarm,
      doorControl1: doorControl1,
      voiceControl1: voiceControl1,
      doorState1: doorState1,
      //  isOpen: doorState1 == 'open' ? true : false,
      delay: Duration(seconds: countdown1),
      schedules: schedules, // Assign schedules
    );
  }

  GarageDoorStatusModel copyWith({
    String? uuid,
    bool? switch1,
    int? countdown1,
    bool? doorContactState,
    int? trTimeCon,
    int? countdownAlarm,
    String? doorControl1,
    bool? voiceControl1,
    String? doorState1,
    //   bool? isOpen,
    Duration? delay,
    List<ScheduleModel>? schedules, // Add schedules to copyWith
  }) {
    return GarageDoorStatusModel(
      uuid: uuid ?? this.uuid,
      switch1: switch1 ?? this.switch1,
      countdown1: countdown1 ?? this.countdown1,
      doorContactState: doorContactState ?? this.doorContactState,
      trTimeCon: trTimeCon ?? this.trTimeCon,
      countdownAlarm: countdownAlarm ?? this.countdownAlarm,
      doorControl1: doorControl1 ?? this.doorControl1,
      voiceControl1: voiceControl1 ?? this.voiceControl1,
      doorState1: doorState1 ?? this.doorState1,
      //  isOpen: isOpen ?? this.isOpen,
      delay: delay ?? this.delay,
      schedules: schedules ?? this.schedules, // Copy schedules
    );
  }

  @override
  String toString() {
    return 'GarageDoorStatusModel(uuid: $uuid, switch1: $switch1, countdown1: $countdown1, doorContactState: $doorContactState, trTimeCon: $trTimeCon, countdownAlarm: $countdownAlarm, doorControl1: $doorControl1, voiceControl1: $voiceControl1, doorState1: $doorState1, delay: $delay, schedules: $schedules)';
  }
}
