import 'package:vitalapp/pages/medical/controllers/base.dart';
import 'package:vnote_device_plugin/consts/status.dart';
import 'package:vnote_device_plugin/consts/types.dart';
import 'package:vnote_device_plugin/devices/base.dart';
import 'package:fis_common/logger/logger.dart';
import 'package:vnote_device_plugin/devices/heart.dart';
import 'package:vnote_device_plugin/devices/ic_reader.dart';
import 'package:vnote_device_plugin/devices/nibp.dart';
import 'package:vnote_device_plugin/devices/sugar.dart';
import 'package:vnote_device_plugin/devices/temp.dart';
import 'package:vnote_device_plugin/devices/urine.dart';
import 'package:vnote_device_plugin/devices/weight.dart';
import 'package:vnote_device_plugin/events/event_type.dart';
import 'package:vnote_device_plugin/models/exams/heart.dart';

class DeviceControllerManager extends BaseDeviceController<DeviceWorkerBase> {
  DeviceControllerManager(
    this.type,
    super.model,
    super.mac,
  ) : super();

  final String type;

  DeviceWorkerBase? deviceWorker;

  @override
  DeviceWorkerBase createWorker() {
    if (deviceWorker != null) {
      logger.i('DeviceControllerManager:$type disconnect start');
      deviceWorker?.disconnect().then((value) {
        logger.i('DeviceControllerManager:$type disconnect end');
        deviceWorker?.dispose();
        logger.i('DeviceControllerManager:$type dispose');
      });
    }
    logger.i('DeviceControllerManager:$type create');
    print('DeviceControllerManager:$type create');
    return _createWorker();
  }

  @override
  void dispose() {
    super.dispose();
    deviceWorker = null;
    logger.i('DeviceControllerManager:$type dispose');
    print('DeviceControllerManager:$type dispose');
  }

  DeviceWorkerBase _createWorker() {
    switch (type) {
      //体重
      case DeviceTypes.WEIGHT:
        deviceWorker = WeightDeviceWorker(mac: super.mac, model: model);
        break;
      //心率
      case DeviceTypes.HEART:
        deviceWorker = HeartDeviceWorker(mac: super.mac, model: model);
        break;
      //血压
      case DeviceTypes.NIBP:
        deviceWorker = NibpDeviceWorker(mac: super.mac, model: model);
        break;
      //血氧
      case DeviceTypes.SPO2:
        deviceWorker = NibpDeviceWorker(mac: super.mac, model: model);
        break;
      //血糖
      case DeviceTypes.SUGAR:
        deviceWorker = SugarDeviceWorker(mac: super.mac, model: model);
        break;
      //温度
      case DeviceTypes.TEMP:
        deviceWorker = TempDeviceWorker(mac: super.mac, model: model);
        break;
      //尿常规
      case DeviceTypes.URINE:
        deviceWorker = UrineDeviceWorker(mac: super.mac, model: model);
        break;
      case DeviceTypes.IC_READER:
        deviceWorker = ICReaderDeviceWorker(mac: super.mac, model: model);
        break;
    }
    return deviceWorker!;
  }
}
