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> powerSingleOn({required int deviceId}) {
    return DioRequest.instance.post<Map<String, dynamic>>(
      '/app/room/device/power/single/on',
      data: {'deviceId': deviceId},
    );
  }

  /// 单设备断电
  Future<ResponseModel> powerSingleOff({required int deviceId}) {
    return DioRequest.instance.post<Map<String, dynamic>>(
      '/app/room/device/power/single/off',
      data: {'deviceId': deviceId},
    );
  }

  /// 客房状态总览-客房概览统计
  Future<ResponseModel<RoomOverviewBO>> getOverview(int? floorId) {
    Map<String, dynamic> param = {};
    if (floorId != null && floorId != 0) {
      param['floorId'] = floorId;
    }
    return DioRequest.instance.get<RoomOverviewBO>(
      '/app/room/overview',
      queryParameters: param,
      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();
      },
    );
  }
}