import 'package:fis_lib_pdf/pdf_render_container.dart';
import 'package:fis_lib_pdf/ultils/pdf_access_type.dart';
import 'package:fis_lib_pdf/ultils/pdf_type_enum.dart';
import 'package:fis_lib_print/src/callback.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/index.dart';
import 'package:flyinsono/architecture/states/states.dart';
import 'package:flyinsono/architecture/storage/file_storage.dart';
import 'package:fis_lib_pdf/ultils/pdf381_wrapper/pdf381_preview_ext.dart';
import 'package:flyinsono/architecture/values/features.dart';
import 'package:flyinsono/managers/interfaces/quality_control.dart';
import 'package:get/get.dart';
import 'package:pdf/src/pdf/page_format.dart';
import 'index.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_i18n/i18n.dart';
import 'widgets/quality_consultation_control.dart';
import 'widgets/quality_control.dart';

///报告预览页面
class ReportPreviewPage extends FisView<ReportPreviewController> {
  const ReportPreviewPage({Key? key}) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return context.fResponsiveVoid(
      mobile: () => _MobileLayout(),
      desktop: () => _DesktopLayout(),
    );
  }
}

///桌面端布局
class _DesktopLayout extends FisView<ReportPreviewController>
    implements FWidget {
  final qualityControlManager = Get.find<IQualityControlManager>();

  @override
  FWidget build(BuildContext context) {
    return FObx(() {
      if (controller.state.isLoaded) {
        String qualityTitle;
        if (controller.reportDTO?.reportType ==
            ReportTypeEnum.LiveConsultation) {
          qualityTitle = qualityControlManager
              .getQualityTitle(controller.state.qualityType);
        } else {
          qualityTitle = i18nBook.common.qualityScore.t;
        }
        return FStack(
          children: [
            FPDFRenderContainer(
              pdfTypeEnum: ReportPreviewController.pdfType,
              demoMode: false,
              pdfAccessTypeEnum: PDFAccessTypeEnum.preview,
              fileName: _generateReaportFileName(),
              pdfExporter: TextMediaFileExporterForShell.exportFile,
              cacheGet: controller.fileStorage.getFile,
              cacheSave: controller.fileStorage.setFile,
              fromEditor: ReportPreviewController.isFromEditor,
              webSideShare: controller.shareReport,
              imageOrFileUrls: ReportPreviewController.imageOrFileUrls,
              onClose: controller.onPDFClose,
              customAction: [
                if (Store.user.hasFeature(FeatureKeys.QualityControl) &&
                    !(controller.reportDTO?.diagnosisLabels ?? [])
                        .contains('VAid')) ...[
                  SelfPdfPreviewAction(
                    content: qualityTitle,
                    icon: FIcon(Icons.gpp_good_outlined),
                    onPressed: _onQualityControl,
                  ),
                ],
              ],
            ),
          ],
        );
      } else {
        return FSizedBox();
      }
    });
  }

  ///获取报告名称
  String _generateReaportFileName() {
    return controller.state.reportName;
  }

  Future<void> _onQualityControl(BuildContext context, LayoutCallback build,
      PdfPageFormat pageFormat) async {
    if (controller.reportDTO?.reportType == ReportTypeEnum.LiveConsultation) {
      await router.dialog(
        () => ConsultationQualityControl(
          consultationRecordCode: controller.recordCode ?? '',
          reportCode: controller.state.reportCode,
          qualityType: controller.state.qualityType,
          onSelectedValue: (v) {
            if (controller.reportDTO != null) {
              controller.reportDTO!.qualityType = v;
            }
            controller.state.qualityType = v;
          },
        ),
      );
    } else {
      await router.dialog(
        () => QualityControl(
          recordCode: controller.recordCode ?? '',
          reportCode: controller.state.reportCode,
          qualityControlDatas: controller.reportDTO?.qualityControlDatas,
          onSubmit: (v) {
            controller.reportDTO?.qualityControlDatas = v;
          },
        ),
      );
    }
  }
}

///手机布局
class _MobileLayout extends FisView<ReportPreviewController>
    implements FWidget {
  @override
  FWidget build(BuildContext context) {
    throw UnimplementedError();
  }
}
