import '../utils/http/response_model.dart';
import '../utils/http/dio_request.dart';
import '../models/bo/room_bo.dart';

class RoomRepository {
  /// 客房服务看板-客房状态总览-楼层
  Future<ResponseModel<List<FloorAreaBO>>> getFloorAreas() {
    return DioRequest.instance.get<List<FloorAreaBO>>(
      '/app/room/floor/areas',
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map((e) => FloorAreaBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }

  /// 客房服务看板-客房状态总览-房间list
  Future<ResponseModel<List<RoomStatusBO>>> getRoomStatus({
    String? roomId,
    int? floorId,
    String? areaType,
  }) {
    final queryParameters = <String, dynamic>{};
    if (roomId != null) queryParameters['roomId'] = roomId;
    if (floorId != null) queryParameters['floorId'] = floorId.toString();
    if (areaType != null) queryParameters['areaType'] = areaType;

    return DioRequest.instance.get<List<RoomStatusBO>>(
      '/app/room/status',
      queryParameters: queryParameters,
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map((e) => RoomStatusBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }

  /// 客房服务看板-送电
  Future<ResponseModel> powerOn({required int roomId}) {
    return DioRequest.instance.post(
      '/app/room/power/on',
      data: {'roomId': roomId},
    );
  }

  /// 客房服务看板-断电
  Future<ResponseModel> powerOff({required int roomId}) {
    return DioRequest.instance.post(
      '/app/room/power/off',
      data: {'roomId': roomId},
    );
  }

  /// 客房详情
  Future<ResponseModel<RoomDetailBO>> getRoomDetail({required int roomId}) {
    return DioRequest.instance.get<RoomDetailBO>(
      '/app/room/detail',
      queryParameters: {'roomId': roomId},
      fromJsonT: (data) =>
          RoomDetailBO.fromJson(data as Map<String, dynamic>),
    );
  }

  /// 客房详情-房间总电源-开
  Future<ResponseModel> roomPowerOn({required int roomId}) {
    return DioRequest.instance.post(
      '/app/room/power/on',
      data: {'roomId': roomId},
    );
  }

  /// 客房详情-房间总电源-关
  Future<ResponseModel> roomPowerOff({required int roomId}) {
    return DioRequest.instance.post(
      '/app/room/power/off',
      data: {'roomId': roomId},
    );
  }

  /// 客房状态总览-客房概览统计
  Future<ResponseModel<RoomOverviewBO>> getOverview(int? floorId) {
    return DioRequest.instance.get<RoomOverviewBO>(
      '/app/room/overview',
      queryParameters: {'floorId': floorId == 0 ? null : floorId},
      fromJsonT: (data) => RoomOverviewBO.fromJson(data as Map<String, dynamic>),
    );
  }

  /// 非客房-相关楼层区域设备
  Future<ResponseModel<List<AreaDeviceBO>>> getAreaDevices({
    required int floorId,
    required String areaType,
  }) {
    return DioRequest.instance.get<List<AreaDeviceBO>>(
      '/app/room/area/devices',
      queryParameters: {
        'floorId': floorId.toString(),
        'areaType': areaType,
      },
      fromJsonT: (data) {
        final list = data as List<dynamic>;
        return list
            .map((e) => AreaDeviceBO.fromJson(e as Map<String, dynamic>))
            .toList();
      },
    );
  }
}