import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/services/notification.m.dart';
import 'package:fis_measure/utils/prompt_box.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/managers/interfaces/remote_controller.dart';
import 'package:flyinsono/views/common/device_live/device_live_list/controller.dart';
import 'package:flyinsono/views/common/device_live/device_live_list/widgets/cloud_drive_card.dart';
import 'package:get/get.dart';

class UsingCloudDriven extends FStatefulWidget implements FPage {
  @override
  String get pageName => 'UsingCloudDriven';
  final String deviceCode;
  final DevicePrinterParameterDTO? currentPrinter;
  final String ipAddress;
  final String portNumber;
  final VoidCallback successCallback;
  UsingCloudDriven({
    required this.deviceCode,
    required this.currentPrinter,
    required this.ipAddress,
    required this.portNumber,
    required this.successCallback,
  });

  @override
  FState<FStatefulWidget> createState() {
    return UsingCloudDrivenState();
  }
}

class UsingCloudDrivenState extends FState<UsingCloudDriven> {
  final controller = Get.find<DeviceLiveListController>();
  final remoteControllerManager = Get.find<IRemoteControllerManager>();

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
        appBar: FAppBar.simple(
          title: i18nBook.remoteControl.useCloudDriver.t,
          onBackClicked: () {
            router.back();
            controller.state.deviceSelectPrinterCode = '';
          },
        ),
        body: FColumn(
          children: [
            FExpanded(
                child: FListView.builder(
              itemBuilder: ((context, index) {
                var devicePrinter =
                    controller.state.printDriverTableDatas[index];
                return FCloudDriveCard(
                  devicePrinter: devicePrinter,
                  onTap: (v) {
                    if (v) {
                      controller.state.deviceSelectPrinterCode =
                          devicePrinter.code ?? '';
                    }
                  },
                );
              }),
              itemCount: controller.state.printDriverTableDatas.length,
            )),
            FElevatedButton(
              businessParent: widget,
              name: 'Use CloudDriver',
              onPressed: () async {
                if (controller.state.deviceSelectPrinterCode.isEmpty) {
                  PromptBox.toast(i18nBook.remoteControl.selectPatchPackage.t);
                  return;
                }
                _onInstallPrinterDrive(
                    controller.state.deviceSelectPrinterCode);
              },
              child: FText(i18nBook.remoteControl.useCloudDriver.t),
              style: ButtonStyle(
                padding: MaterialStateProperty.all(
                  EdgeInsets.symmetric(vertical: kIsMobile ? 0 : 15),
                ),
                fixedSize: MaterialStateProperty.all(
                    Size(MediaQuery.of(context).size.width, 50)),
              ),
            ),
          ],
        ));
  }

  /// 安装打印机
  Future<void> _onInstallPrinterDrive(String devicePrinterPatchCode) async {
    controller.setDevicePrinterAsync(
      deviceCode: widget.deviceCode,
      setPrinterEnum: DevicePrinterEnum.InstallPrinterDrive,
      parameters: [
        DevicePrinterParameter(
          setPrinterEnum: DevicePrinterParameterEnum.PrinterName,
          parameterValue: widget.currentPrinter?.name,
        ),
        DevicePrinterParameter(
          setPrinterEnum: DevicePrinterParameterEnum.PrinterModel,
          parameterValue: widget.currentPrinter?.brand,
        ),
        DevicePrinterParameter(
          setPrinterEnum: DevicePrinterParameterEnum.DevicePrinterPatchCode,
          parameterValue: devicePrinterPatchCode,
        ),
        DevicePrinterParameter(
          setPrinterEnum: DevicePrinterParameterEnum.IPAddress,
          parameterValue: widget.ipAddress,
        ),
        DevicePrinterParameter(
          setPrinterEnum: DevicePrinterParameterEnum.Port,
          parameterValue: widget.portNumber,
        ),
      ],
    );
    router.back();
    widget.successCallback.call();
  }
}
