import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:smart_hotel_app/models/bo/inspection_device_bo.dart';
import 'package:smart_hotel_app/models/bo/inspection_room_bo.dart';
import 'package:smart_hotel_app/utils/deal_utils.dart';

enum DeviceStatusTab { total, on, /*off, */offline, alarm, fault }

extension DeviceStatusTabRunStatus on DeviceStatusTab {
  /// 映射为接口 runStatus 入参：
  /// online=开启(绿) offline=关闭/离线(灰) warning=告警(黄) fault=故障(红)
  String? get runStatus {
    switch (this) {
      case DeviceStatusTab.total:
        return null;
      case DeviceStatusTab.on:
        return 'online';
      // case DeviceStatusTab.off:
      //   return 'offline';
      case DeviceStatusTab.offline:
        return 'offline';
      case DeviceStatusTab.alarm:
        return 'warning';
      case DeviceStatusTab.fault:
        return 'fault';
    }
  }
}

class InspectionDevice {
  final String id;
  final IconData icon;
  final String name;
  final String type;
  final String location;
  final String status;
  final String time;
  final Color statusColor;

  const InspectionDevice({
    required this.id,
    required this.icon,
    required this.name,
    required this.type,
    required this.location,
    required this.status,
    required this.time,
    required this.statusColor,
  });

  factory InspectionDevice.fromInspectionDeviceBO(InspectionDeviceBO bo) {
    return InspectionDevice(
      id: bo.deviceId.toString(),
      icon: DealUtils.deviceIcon(bo.deviceTypeIcon),
      name: bo.deviceName,
      type: bo.deviceTypeName,
      location: bo.roomName,
      status: bo.runStatusText,
      time: bo.lastHeartbeatDisplay,
      statusColor: _mapStatusColor(bo.runStatus),
    );
  }

  static Color _mapStatusColor(String runStatus) {
    switch (runStatus) {
      case 'normal':
      case 'online':
        return const Color.fromRGBO(26, 188, 156, 1.0);
      case 'warning':
      case 'alarm':
        return const Color.fromRGBO(255, 77, 79, 1.0);
      case 'offline':
        return const Color.fromRGBO(102, 102, 102, 1.0);
      case 'fault':
        return const Color.fromRGBO(239, 68, 68, 1.0);
      case 'off':
        return const Color.fromRGBO(100, 116, 139, 1.0);
      default:
        return const Color.fromRGBO(100, 116, 139, 1.0);
    }
  }
}

class InspectionState extends Equatable {
  final bool isLoading;
  final String? error;
  final DeviceStatusTab selectedTab;
  final int selectedAreaIndex;
  final int? selectedRoomId;
  final List<InspectionRoomGroupsBO> roomGroups;
  final Map<DeviceStatusTab, int> tabCounts;
  final int totalCount;

  const InspectionState({
    this.isLoading = false,
    this.error,
    this.selectedTab = DeviceStatusTab.total,
    this.selectedAreaIndex = 0,
    this.selectedRoomId,
    this.roomGroups = const [],
    this.tabCounts = const {
      DeviceStatusTab.total: 0,
      DeviceStatusTab.on: 0,
      // DeviceStatusTab.off: 0,
      DeviceStatusTab.offline: 0,
      DeviceStatusTab.alarm: 0,
      DeviceStatusTab.fault: 0,
    },
    this.totalCount = 0,
  });

  InspectionState copyWith({
    bool? isLoading,
    String? error,
    DeviceStatusTab? selectedTab,
    int? selectedAreaIndex,
    int? selectedRoomId,
    List<InspectionRoomGroupsBO>? roomGroups,
    Map<DeviceStatusTab, int>? tabCounts,
    int? totalCount,
  }) {
    return InspectionState(
      isLoading: isLoading ?? this.isLoading,
      error: error,
      selectedTab: selectedTab ?? this.selectedTab,
      selectedAreaIndex: selectedAreaIndex ?? this.selectedAreaIndex,
      selectedRoomId: selectedRoomId ?? this.selectedRoomId,
      roomGroups: roomGroups ?? this.roomGroups,
      tabCounts: tabCounts ?? this.tabCounts,
      totalCount: totalCount ?? this.totalCount,
    );
  }

  @override
  List<Object?> get props => [
        isLoading,
        error,
        selectedTab,
        selectedAreaIndex,
        selectedRoomId,
        roomGroups,
        tabCounts,
        totalCount,
      ];
}