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

/// 0=待处理 1=处理中 2=已处理 3=已关闭 4=误报关闭
enum AlarmStatus { all, pending, processing, processed, close, 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 alertIcon;

  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.alertIcon,
  });

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

  // 0=待处理 1=处理中 2=已处理 3=已关闭 4=误报关闭
  static AlarmStatus _mapAlertStatus(String alertStatus) {
    switch (alertStatus) {
      case '0':
        return AlarmStatus.pending;
      case '1':
        return AlarmStatus.processing;
      case '2':
        return AlarmStatus.processed;
      case '3':
        return AlarmStatus.close;
      case '4':
        return AlarmStatus.falseAlarm;
      default:
        return AlarmStatus.all;
    }
  }

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

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,
      ];
}