import 'dart:convert';

import '../repositories/inspection_device_repository.dart';
import '../models/bo/inspection_device_bo.dart';

class InspectionDeviceService {
  final InspectionDeviceRepository _repository;

  InspectionDeviceService({required InspectionDeviceRepository repository})
      : _repository = repository;

  Future<InspectionDeviceListBO> getList({
    required int pageSize,
    required int pageNum,
    int? roomId,
    String? runStatus,
  }) async {
    final result = await _repository.getList(
      pageSize: pageSize,
      pageNum: pageNum,
      roomId: roomId,
      runStatus: runStatus,
    );
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  Future<DeviceDetailBO> getDetail(int deviceId) async {
    final result = await _repository.getDetail(deviceId);
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }

  Future<InspectionStartResultBO> startInspection({
    required int deviceId,
    required int inspectorId,
  }) async {
    List<Map<String, dynamic>> list = [
      {
        'name': '检查输入电压是否正常',
        'value': '正常',
        'status': 2,
      },
      {
        'name': '确认设备是否正常运行',
        'value': '正常',
        'status': 2,
      },
      {
        'name': '若异常,请联系电工',
        'value': '正常',
        'status': 2,
      },
    ];
    final result = await _repository.startInspection({
      'deviceId': deviceId,
      'inspectorId': inspectorId,
      'remark': '巡检完成',//后端要先写死
      'items_detail': jsonEncode(list),//后端要先写死
    });
    if (result.success && result.data != null) {
      return result.data!;
    }
    throw Exception(result.msg);
  }
}