import 'package:fis_common/logger/logger.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/process/workspace/rpc_bridge.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/rpc.dart';
import 'package:fis_measure/process/workspace/measure_handler.dart';
import 'package:image_picker/image_picker.dart';

class MeasureDataHelper {
  /// 这边还没做压缩 缩略图
  /// patientCode 好像没用到 传空也行
  static Future<bool?> saveImage(
      Uint8List imageBytes,
      String patientCode,
      String recordCode,
      String remedicalCode,
      String measuredData,
      VidImageSource source,
      [String? currentMode]) async {
    try {
      XFile xFile = XFile.fromData(
        imageBytes,
        name: remedicalCode + '.jpg',
      );

      final measuredFileToken = await rpc.storage.upload(xFile);

      final result = await rpc.recordInfo.addRemedicalMeasuredInfoAsync(
        AddRemedicalMeasuredInfoRequest(
          recordCode: recordCode,
          businessType: source == VidImageSource.Consultation
              ? BusinessTypeEnum.LiveConsultation
              : BusinessTypeEnum.RemoteDiagnosis,
          remedicalMeasuredInfos: [
            AddRemedicalMeasuredInfoDTO(
              remedicalCode: remedicalCode,
              measuredFileToken: measuredFileToken,
              previewFileToken: measuredFileToken,
              measuredData: measuredData,
            )
          ],
          model: currentMode,
          token: Store.user.token,
        ),
      );
      return result;
    } catch (e) {
      logger.e("addRemedicalMeasuredInfoAsync exception:", e);
    }
    return null;
  }

  /// 保存测量的方法
  static Future<bool> saveUserDefinedMeasureApplicationAsync(
      MeasureModeSubmit args) async {
    var result = false;
    try {
      result = await RPCBridge.ins.rpc.remedical
          .saveUserDefinedMeasureApplicationAsync(
        SaveUserDefinedMeasureApplicationRequest(
          token: Store.user.token,
          version: args.version,
          applicationName: args.applicationName,
          categoryName: args.categoryName,
          workingMode: args.workingMode,
        ),
      );
    } catch (e) {
      logger.e(
          'MeasureHomeController saveUserDefinedMeasureApplicationAsync ex: ',
          e);
    }
    return result;
  }

  /// 保存样式
  static Future<void> saveMeasureSystemSettingAsync(
    MeasureSystemSettingDTO measureSystemSetting,
  ) async {
    await RPCBridge.ins.rpc.remedical.saveMeasureSystemSettingAsync(
      SaveMeasureSystemSettingRequest(
        systemSetting: measureSystemSetting,
        token: Store.user.token,
      ),
    );
  }

  /// 获取样式
  static Future<MeasureSystemSettingDTO> getMeasureSystemSettingAsync() async {
    final result =
        await RPCBridge.ins.rpc.remedical.getMeasureSystemSettingAsync(
      GetMeasureSystemSettingRequest(
        token: Store.user.token,
      ),
    );
    return result;
  }
}
