import 'package:equatable/equatable.dart';

enum DeviceTypeEnum {
  all,
  smartBreaker,
  guestControl,
  network,
}

/// deviceTypeId 映射: all→null, smartBreaker→airSwitch, guestControl→guestControl, network→gateway
String? deviceTypeIdFromEnum(DeviceTypeEnum type) {
  switch (type) {
    case DeviceTypeEnum.all:
      return null;
    case DeviceTypeEnum.smartBreaker:
      return 'airSwitch';
    case DeviceTypeEnum.guestControl:
      return 'guestControl';
    case DeviceTypeEnum.network:
      return 'gateway';
  }
}

class DeviceInfo extends Equatable {
  final int deviceId;
  final String name;
  final String room;
  final DeviceTypeEnum type;
  final bool online;
  final String status;
  final String lastUpdate;
  final String color;
  final String deviceTypeIcon;
  final String subTitle;

  const DeviceInfo({
    this.deviceId = 0,
    this.name = '',
    this.room = '',
    required this.type,
    this.online = false,
    this.status = '',
    this.lastUpdate = '',
    this.color = '',
    this.deviceTypeIcon = '',
    this.subTitle = '',
  });

  @override
  List<Object?> get props => [deviceId, name, room, type, online,
    status, lastUpdate, color, deviceTypeIcon, subTitle];
}

class DeviceListState extends Equatable {
  final bool isLoading;
  final String? error;
  final int tabAll;
  final int tabAirSwitch;
  final int tabGuestControl;
  final int tabNetwork;
  final int totalCount;
  final DeviceTypeEnum selectedType;

  const DeviceListState({
    this.isLoading = false,
    this.error,
    this.tabAll = 0,
    this.tabAirSwitch = 0,
    this.tabGuestControl = 0,
    this.tabNetwork = 0,
    this.totalCount = 0,
    this.selectedType = DeviceTypeEnum.all,
  });

  DeviceListState copyWith({
    bool? isLoading,
    String? error,
    int? tabAll,
    int? tabAirSwitch,
    int? tabGuestControl,
    int? tabNetwork,
    int? totalCount,
    DeviceTypeEnum? selectedType,
    bool clearError = false,
  }) {
    return DeviceListState(
      isLoading: isLoading ?? this.isLoading,
      error: clearError ? null : (error ?? this.error),
      tabAll: tabAll ?? this.tabAll,
      tabAirSwitch: tabAirSwitch ?? this.tabAirSwitch,
      tabGuestControl: tabGuestControl ?? this.tabGuestControl,
      tabNetwork: tabNetwork ?? this.tabNetwork,
      totalCount: totalCount ?? this.totalCount,
      selectedType: selectedType ?? this.selectedType,
    );
  }

  @override
  List<Object?> get props => [
        isLoading,
        error,
        tabAll,
        tabAirSwitch,
        tabGuestControl,
        tabNetwork,
        totalCount,
        selectedType,
      ];
}
