import 'dart:convert';

import 'package:fis_common/event/event_type.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:flyinsono/managers/base_manager.dart';
import 'package:flyinsono/rpc.dart';

import 'interfaces/entities/install_printer_driver_state.dart';
import 'interfaces/entities/print_driver_info.dart';
import 'interfaces/entities/printer_drive.dart';
import 'interfaces/remote_maintenance.dart';

class RemoteMaintenanceManager extends BaseManager
    implements IRemoteMaintenanceManager {
  @override
  FEventHandler<PrintDriverInfo> onCheckPrintDriverStateChange =
      FEventHandler<PrintDriverInfo>();

  @override
  final onInstallPrinterDriverChange =
      FEventHandler<InstallPrinterDriverState>();

  ///保存并退出成功通知
  final onSystemSaveAndExit = FEventHandler<bool>();
  @override
  final FEventHandler<bool> onCancelExtendButtons = FEventHandler<bool>();
  @override
  Future<bool> deletePrinterByCodeAsync(String code) async {
    return await rpc.device
        .deletePrinterByCodeAsync(DeleteDevicePrinterByCodeRequest(
      token: token,
      code: code,
    ));
  }

  @override
  Future<PageResult<DevicePrinterDTO>> findDevicePrinterPagesAsync({
    String? keyword,
    int pageIndex = 1,
    int pageSize = 20,
  }) {
    return rpc.device.findDevicePrinterPagesAsync(FindDevicePrinterPageRequest(
      token: token,
      keyword: keyword,
      pageIndex: pageIndex,
      pageSize: pageSize,
    ));
  }

  @override
  Future<List<String>> getUninstalledPrinters({String? ip}) async {
    return rpc.platform.getUninstalledPrinters(ip: ip);
  }

  void closePrinterDriverManager() {
    rpc.platform.closePrinterDriverManager();
  }

  @override
  void installPrinterDriver(PrinterDrive printerDrive) {
    var printerDriveStr = jsonEncode(printerDrive.toJson());
    rpc.platform.installPrinterDriver(printerDriveStr);
  }

  @override
  Future<bool> printTestPage(String name) {
    return rpc.platform.printTestPage(name);
  }

  @override
  Future<bool> chooseLocalDrive() {
    return rpc.platform.chooseLocalDrive();
  }
}
