import 'package:equatable/equatable.dart';
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';

enum ScheduleModes { countdown, schedule, circulate, inching }

class WaterHeaterStatusModel extends Equatable {
  final String uuid;
  final bool heaterSwitch;
  final int countdownHours;
  final int countdownMinutes;
  final int inchingHours;
  final int inchingMinutes;
  final ScheduleModes scheduleMode;
  final String relayStatus;
  final String cycleTiming;
  final List<ScheduleModel> schedules;

  const WaterHeaterStatusModel({
    required this.uuid,
    required this.heaterSwitch,
    required this.countdownHours,
    required this.countdownMinutes,
    required this.relayStatus,
    required this.cycleTiming,
    required this.scheduleMode,
    required this.schedules,
    this.inchingHours = 0,
    this.inchingMinutes = 0,
  });

  factory WaterHeaterStatusModel.fromJson(String id, List<Status> jsonList) {
    late bool heaterSwitch = false;
    late int countdownInSeconds = 0;
    late String relayStatus = '';
    late String cycleTiming = '';
    late ScheduleModes scheduleMode = ScheduleModes.countdown;

    for (var status in jsonList) {
      switch (status.code) {
        case 'switch_1':
          heaterSwitch = status.value ?? false;
          break;
        case 'countdown_1':
          countdownInSeconds = status.value ?? 0;
          break;
        case 'relay_status':
          relayStatus = status.value ?? 'memory';
          break;
        case 'cycle_timing':
          cycleTiming = status.value ?? '';
          break;
        case 'switch_inching':
          scheduleMode = getScheduleMode(status.value ?? 'countdown');
          break;
      }
    }

    final countdownHours = countdownInSeconds ~/ 3600;
    final countdownMinutes = (countdownInSeconds % 3600) ~/ 60;

    return WaterHeaterStatusModel(
      uuid: id,
      heaterSwitch: heaterSwitch,
      countdownHours: countdownHours,
      countdownMinutes: countdownMinutes,
      relayStatus: relayStatus,
      cycleTiming: cycleTiming,
      scheduleMode: scheduleMode,
      schedules: const [],
    );
  }

  WaterHeaterStatusModel copyWith({
    String? uuid,
    bool? heaterSwitch,
    int? countdownHours,
    int? countdownMinutes,
    String? relayStatus,
    String? cycleTiming,
    ScheduleModes? scheduleMode,
    List<ScheduleModel>? schedules,
  }) {
    return WaterHeaterStatusModel(
      uuid: uuid ?? this.uuid,
      heaterSwitch: heaterSwitch ?? this.heaterSwitch,
      countdownHours: countdownHours ?? this.countdownHours,
      countdownMinutes: countdownMinutes ?? this.countdownMinutes,
      relayStatus: relayStatus ?? this.relayStatus,
      cycleTiming: cycleTiming ?? this.cycleTiming,
      scheduleMode: scheduleMode ?? this.scheduleMode,
      schedules: schedules ?? this.schedules,
    );
  }

  static ScheduleModes getScheduleMode(String value) {
    switch (value) {
      case 'countdown':
        return ScheduleModes.countdown;
      case 'schedule':
        return ScheduleModes.schedule;
      case 'circulate':
        return ScheduleModes.circulate;
      case 'inching':
        return ScheduleModes.inching;
      default:
        return ScheduleModes.countdown;
    }
  }

  @override
  List<Object?> get props => [
        uuid,
        heaterSwitch,
        countdownHours,
        countdownMinutes,
        scheduleMode,
        relayStatus,
        cycleTiming,
      ];
}
