import 'package:equatable/equatable.dart';

import '../../../domain/entities/monitor_state.dart' as domain;

class MonitorBlocState extends Equatable {
  final int? selectedMonitorId;
  final Map<int, domain.MonitorState> monitorStates;
  final List<int> availableMonitors;
  final String? error;

  const MonitorBlocState({
    this.selectedMonitorId,
    this.monitorStates = const {},
    this.availableMonitors = const [],
    this.error,
  });

  bool get hasSelection => selectedMonitorId != null;

  /// Get the currently selected monitor's state
  domain.MonitorState? get selectedMonitorState {
    if (selectedMonitorId == null) return null;
    return monitorStates[selectedMonitorId];
  }

  /// Get the camera currently on the selected monitor
  int? get selectedMonitorCamera {
    return selectedMonitorState?.currentChannel;
  }

  MonitorBlocState copyWith({
    int? selectedMonitorId,
    Map<int, domain.MonitorState>? monitorStates,
    List<int>? availableMonitors,
    String? error,
    bool clearSelection = false,
  }) {
    return MonitorBlocState(
      selectedMonitorId:
          clearSelection ? null : (selectedMonitorId ?? this.selectedMonitorId),
      monitorStates: monitorStates ?? this.monitorStates,
      availableMonitors: availableMonitors ?? this.availableMonitors,
      error: error,
    );
  }

  @override
  List<Object?> get props =>
      [selectedMonitorId, monitorStates, availableMonitors, error];
}
