import '../models/bo/room_bo.dart';
import '../repositories/room_repository.dart';

class RoomService {
  final RoomRepository _repository;

  RoomService({required RoomRepository repository}) : _repository = repository;

  /// 获取楼层及区域信息
  Future<List<FloorAreaBO>> getFloorAreas() async {
    final result = await _repository.getFloorAreas();
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  /// 获取房间状态列表
  Future<List<RoomStatusBO>> getRoomStatus({
    String? roomId,
    int? floorId,
    String? areaType,
  }) async {
    final result = await _repository.getRoomStatus(
      roomId: roomId,
      floorId: floorId,
      areaType: areaType,
    );
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  /// 送电
  Future<void> powerOn({required int roomId}) async {
    final result = await _repository.powerOn(roomId: roomId);
    if (!result.success) {
      throw Exception(result.msg);
    }
  }

  /// 断电
  Future<void> powerOff({required int roomId}) async {
    final result = await _repository.powerOff(roomId: roomId);
    if (!result.success) {
      throw Exception(result.msg);
    }
  }

  /// 客房详情
  Future<RoomDetailBO> getRoomDetail({required int roomId}) async {
    final result = await _repository.getRoomDetail(roomId: roomId);
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  /// 客房状态总览-客房概览统计
  Future<RoomOverviewBO> getOverview(int? floorId) async {
    final result = await _repository.getOverview(floorId);
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  /// 非客房-相关楼层区域设备
  Future<List<AreaDeviceBO>> getAreaDevices({
    required int floorId,
    required String areaType,
  }) async {
    final result = await _repository.getAreaDevices(
      floorId: floorId,
      areaType: areaType,
    );
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  /// 单设备送电
  Future<void> powerSingleOn({required int deviceId}) async {
    final result = await _repository.powerSingleOn(deviceId: deviceId);
    if (!result.success) {
      throw Exception(result.msg);
    }
  }

  /// 单设备断电
  Future<void> powerSingleOff({required int deviceId}) async {
    final result = await _repository.powerSingleOff(deviceId: deviceId);
    if (!result.success) {
      throw Exception(result.msg);
    }
  }
}