import 'package:equatable/equatable.dart';
import 'package:smart_hotel_app/models/bo/alert_list_bo.dart';

enum AlarmStatus { all, pending, processed, falseAlarm }

class AlarmItem extends Equatable {
  final String id;
  final String title;
  final String deviceInfo;
  final String alarmTime;
  final String processTime;
  final AlarmStatus status;
  final String level;
  final String urgency;

  const AlarmItem({
    required this.id,
    required this.title,
    required this.deviceInfo,
    required this.alarmTime,
    required this.processTime,
    required this.status,
    required this.level,
    required this.urgency,
  });

  factory AlarmItem.fromAlertListItemBO(AlertListItemBO item) {
    return AlarmItem(
      id: item.alertId.toString(),
      title: item.alertTitle,
      deviceInfo: item.deviceLocationText,
      alarmTime: item.alertTime,
      processTime: item.handleTime.isEmpty ? '--' : item.handleTime,
      status: _mapAlertStatus(item.alertStatus),
      level: item.alertLevel,
      urgency: '',
    );
  }

  static AlarmStatus _mapAlertStatus(String status) {
    switch (status) {
      case '0':
      case '1':
        return AlarmStatus.pending;
      case '2':
      case '3':
        return AlarmStatus.processed;
      case '4':
        return AlarmStatus.falseAlarm;
      default:
        return AlarmStatus.all;
    }
  }

  @override
  List<Object?> get props =>
      [id, title, deviceInfo, alarmTime, processTime, status, level, urgency];
}

class AbnormalListState extends Equatable {
  final bool isLoading;
  final String? error;
  final AlarmStatus currentFilter;
  final int totalCount;
  final int allCount;
  final int pendingCount;
  final int processedCount;
  final int falseAlarmCount;

  const AbnormalListState({
    this.isLoading = false,
    this.error,
    this.currentFilter = AlarmStatus.all,
    this.totalCount = 0,
    this.allCount = 0,
    this.pendingCount = 0,
    this.processedCount = 0,
    this.falseAlarmCount = 0,
  });

  AbnormalListState copyWith({
    bool? isLoading,
    String? error,
    AlarmStatus? currentFilter,
    int? totalCount,
    int? allCount,
    int? pendingCount,
    int? processedCount,
    int? falseAlarmCount,
    bool clearError = false,
  }) {
    return AbnormalListState(
      isLoading: isLoading ?? this.isLoading,
      error: clearError ? null : (error ?? this.error),
      currentFilter: currentFilter ?? this.currentFilter,
      totalCount: totalCount ?? this.totalCount,
      allCount: allCount ?? this.allCount,
      pendingCount: pendingCount ?? this.pendingCount,
      processedCount: processedCount ?? this.processedCount,
      falseAlarmCount: falseAlarmCount ?? this.falseAlarmCount,
    );
  }

  @override
  List<Object?> get props => [
        isLoading,
        error,
        currentFilter,
        totalCount,
        allCount,
        pendingCount,
        processedCount,
        falseAlarmCount,
      ];
}