import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:smart_hotel_app/models/bo/inspection_device_bo.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: _mapIcon(bo.deviceTypeIcon),
      name: bo.deviceName,
      type: bo.deviceTypeName,
      location: bo.roomName,
      status: bo.runStatusText,
      time: bo.lastHeartbeatDisplay,
      statusColor: _mapStatusColor(bo.runStatus),
    );
  }

  static IconData _mapIcon(String iconType) {
    switch (iconType) {
      case 'gateway':
        return Icons.wifi;
      case 'sensor':
        return Icons.thermostat;
      case 'camera':
        return Icons.videocam;
      case 'light':
        return Icons.light;
      case 'air_conditioner':
      case 'ac':
        return Icons.ac_unit;
      case 'socket':
        return Icons.kitchen;
      case 'alarm':
        return Icons.smoke_free;
      case 'door_sensor':
        return Icons.sensor_door;
      default:
        return Icons.devices_other;
    }
  }

  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 List<int> selectedRoomIds;
  final Map<int, String> allRooms;
  final Map<DeviceStatusTab, int> tabCounts;
  final int totalCount;

  const InspectionState({
    this.isLoading = false,
    this.error,
    this.selectedTab = DeviceStatusTab.total,
    this.selectedRoomIds = const [],
    this.allRooms = 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,
    List<int>? selectedRoomIds,
    Map<int, String>? allRooms,
    Map<DeviceStatusTab, int>? tabCounts,
    int? totalCount,
  }) {
    return InspectionState(
      isLoading: isLoading ?? this.isLoading,
      error: error,
      selectedTab: selectedTab ?? this.selectedTab,
      selectedRoomIds: selectedRoomIds ?? this.selectedRoomIds,
      allRooms: allRooms ?? this.allRooms,
      tabCounts: tabCounts ?? this.tabCounts,
      totalCount: totalCount ?? this.totalCount,
    );
  }

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