import 'dart:convert';

import 'package:fis_jsonrpc/rpc.dart';
import 'package:get/get.dart';
import 'package:vnote_device_plugin/devices/temp.dart';
import 'package:vnoteapp/architecture/defines.dart';
import 'package:vnoteapp/architecture/utils/prompt_box.dart';
import 'package:vnoteapp/managers/interfaces/cachedRecord.dart';
import 'package:vnoteapp/managers/interfaces/diagnosis.dart';
import 'package:vnoteapp/managers/interfaces/permission.dart';
import 'package:vnoteapp/pages/medical/models/item.dart';
import 'package:vnoteapp/pages/medical/state.dart';

class MedicalController extends FControllerBase {
  final state = MedicalState();
  final _diagnosisManager = Get.find<IDiagnosisManager>();
  final _cachedRecordManager = Get.find<ICachedRecordManager>();
  var permissionManager = Get.find<IPermissionManager>();
  final TempDeviceWorker tempDeviceWorker = TempDeviceWorker(
    mac: 'D0:05:00:00:00:67',
    model: 'YHW-6',
  );

  String patientCode = '';
  String appDataId = '1008611';
  List<MedicalItem> medicalMenuList = [
    MedicalItem(key: 'Temp', diagnosticItem: '体温'),
    MedicalItem(key: 'GLU', diagnosticItem: '血糖'),
    MedicalItem(key: 'NIBP', diagnosticItem: '血压'),
    MedicalItem(key: 'SpO2', diagnosticItem: '血氧'),
    MedicalItem(key: 'BMI', diagnosticItem: 'BMI'),
  ];
  Map<String, dynamic> diagnosisDataValue = {};

  @override
  Future<void> onLoad() {
    final p = Get.parameters;
    patientCode = p["patientCode"]!;
    getPermission();
    return super.onLoad();
  }

  Future<void> getPermission() async {
    await permissionManager.requestLocationPermission();
    await permissionManager.requestBluetoothConnectPermission();
    await permissionManager.requestBluetoothAdvertisePermission();
    await permissionManager.requestBluetoothScanPermission();
  }

  Future<void> readCachedRecord() async {
    String? value = await _cachedRecordManager.readCachedRecord(
      state.currentTab,
      patientCode,
      'ZLZS',
      jsonEncode(
        diagnosisDataValue[state.currentTab],
      ),
    );
    diagnosisDataValue[state.currentTab] = jsonDecode(value ?? '');
  }

  Future<void> saveCachedRecord() async {
    await _cachedRecordManager.saveCachedRecord(
      state.currentTab,
      patientCode,
      'ZLZS',
      jsonEncode(
        diagnosisDataValue[state.currentTab],
      ),
    );
    print(diagnosisDataValue['BMI']);
  }

  Future<void> createDiagnosis() async {
    CreateDiagnosisRequest diagnosisRequest = CreateDiagnosisRequest(
      patientCode: patientCode,
      appDataId: appDataId,
      diagnosisTime: DateTime.now(),
      diagnosisData: jsonEncode(diagnosisDataValue[state.currentTab]),
      key: state.currentTab,
    );
    saveCachedRecord();
    final result =
        await _diagnosisManager.createDiagnosisAsync(diagnosisRequest);
    if (result.isNotEmpty) {
      PromptBox.toast('保存成功');
      final index = state.medicalMenuList
          .indexWhere((element) => element.key == state.currentTab);
      if (index < 4) {
        state.currentTab = state.medicalMenuList[index + 1].key;
      } else {
        Get.back();
      }
    }
  }
}
