import 'package:fis_common/index.dart';
import 'package:flyinsono/js_platform/methods.dart';
import 'package:flyinsono/managers/interfaces/entities/print_driver_info.dart';
import 'package:flyinsono/managers/interfaces/remote_maintenance.dart';
import 'package:get/get.dart';

import 'base.dart';

class CheckPrintDriverStateChangeHandler extends MethodHandlerBase {
  CheckPrintDriverStateChangeHandler()
      : super(TargetMethodName.OnCheckPrintDriverStateChange);

  @override
  void execute(List<String> args) {
    var manager = Get.find<IRemoteMaintenanceManager>();
    var stateStr = args[0];
    var stateIndex = int.parse(stateStr);
    CheckPrintDriverState state = CheckPrintDriverState.values
            .firstWhereOrNull((element) => element.index == stateIndex) ??
        CheckPrintDriverState.Success;
    String? name;
    String? printerBrands;
    List<String> printerModels = [];
    if (state == CheckPrintDriverState.Ready) {
      name = args[1];
    } else if (state == CheckPrintDriverState.Success) {
      printerBrands = args[1];
      var models = args[2].split('@');
      for (var m in models) {
        if (m.isNotNullOrEmpty) {
          printerModels.add(m);
        }
      }
    }
    manager.onCheckPrintDriverStateChange.emit(
        this,
        PrintDriverInfo(
          state: state,
          fileName: name,
          printerBrands: printerBrands,
          printerModels: printerModels,
        ));
  }
}
