import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/exam_record_util.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class ReportPreview extends StatefulWidget implements FPage {
  final ReportDTO reportInfo;
  final List<ReportDTO> reportList;
  @override
  final String pageName;

  ReportPreview({
    this.pageName = 'ReportPreview',
    required this.reportInfo,
    required this.reportList,
  });
  @override
  State<StatefulWidget> createState() {
    return _ReportPreviewState();
  }
}

class _ReportPreviewState extends State<ReportPreview> {
  int _index = 0;
  ReportDTO _reportInfoDetail = ReportDTO();
  final reportManager = Get.find<IReportManager>();
  @override
  void initState() {
    super.initState();
    _reportInfoDetail = widget.reportInfo;
  }

  @override
  FWidget build(BuildContext context) {
    return FScaffold(
      appBar: ExamRecordsMobileAppbar(
        title: FText(
          i18nBook.remedical.viewReport.t,
          style: TextStyle(
            fontSize: 16,
            color: Colors.white,
          ),
        ),
        // isSingle: true,
      ),
      body: _buildReportPreview(),
      backgroundColor: Colors.grey[200],
    );
  }

  ///报告预览
  FWidget _buildReportPreview() {
    final reportPreviewList = widget.reportInfo.reportPreviewList ?? [];
    if (reportPreviewList.isEmpty) {
      return FSizedBox();
    }
    var body = PdfRender(_reportInfoDetail, widget.reportList);
    return body;
  }
}

class PdfRender extends StatefulWidget implements FWidget {
  final ReportDTO reportInfo;
  final List<ReportDTO> reportList;

  PdfRender(this.reportInfo, this.reportList);

  @override
  State<StatefulWidget> createState() {
    return PdfRenderState();
  }
}

class PdfRenderState extends State<PdfRender> {
  final _reportManager = Get.find<IReportManager>();
  static const String C_REPORTFORMAT = ".pdf";
  static const String C_DEFAULTFILENAME = "report";
  late FWidget _body;

  @override
  void initState() {
    super.initState();
    _body = _getPdfRender();
  }

  @override
  Widget build(BuildContext context) {
    return _body;
  }

  @override
  void didUpdateWidget(covariant PdfRender old) {
    _body = _getPdfRender();
    super.didUpdateWidget(old);
  }

  ///获取报告名称
  String _generateReaportFileName(String reportCode) {
    var patientName = "";
    var dateStr = "";
    for (var r in widget.reportList) {
      if (r.reportCode == reportCode) {
        patientName = r.patientName!.replaceAll("*", "x");
        DateFormat format = DateFormat("yyyMMddHHmmss");
        dateStr = format.format(r.reportTime!.toLocal());
      }
    }
    var fileName = "[$patientName]-[$dateStr]-[$reportCode]$C_REPORTFORMAT";
    return fileName;
  }

  FWidget _getPdfRender() {
    final reportInfo = widget.reportInfo;
    final reportPreviewList = reportInfo.reportPreviewList ?? [];
    if (reportPreviewList.isEmpty) {
      return FSizedBox();
    }
    final fileToken = reportPreviewList.first.fileToken ?? '';
    List<String> previewImages = [];
    if (fileToken.isNotEmpty) {
      previewImages = fileToken.split(';');
    }
    List<FWidget> children = previewImages
        .map(
          (e) => FContainer(
            child: FImage.network(
              e,
              //   width: MediaQuery.of(context).size.width - 80,
              fit: BoxFit.fitHeight,
            ),
          ),
        )
        .toList();

    return QuickFWidget(
      LayoutBuilder(
        builder: (BuildContext context, BoxConstraints constraints) {
          print(
              '${MediaQuery.of(context).size.height} ,, ${constraints.maxHeight}');
          return InteractiveViewer(
            constrained: false,
            minScale: 0.5,
            child: FContainer(
              width: constraints.maxWidth,
              height: children.length > 1 ? null : constraints.maxHeight,
              child: FColumn(
                mainAxisAlignment: MainAxisAlignment.center,
                children: children,
              ),
            ),
          );
        },
      ),
    );
  }
}
