import 'package:flutter/material.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/fcustom_flat_button.dart';
import 'package:fis_common/index.dart';
import 'package:fis_i18n/i18n.dart';
import 'package:flyinsono/pages/remedical/consultation/widgets/report/ai_icon_color.dart';
import 'package:get/get.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/interface/interactive_container.dart';

class ReportCardMobileLayout extends FStatelessWidget implements FWidget {
  final reportManager = Get.find<IReportManager>();
  final ReportDTO reportInfo;
  final void Function(String) onTapShare;
  final int index;
  final String patientName;
  final void Function()? onTapCheck;
  final void Function()? onTapEditReport;
  final void Function(int) onTapImage;

  ///父级节点
  final FInteractiveContainer businessParent;

  static const _textStyle = TextStyle(
    fontSize: 14,
  );
  ReportCardMobileLayout(
    this.reportInfo,
    this.index, {
    required this.patientName,
    required this.businessParent,
    required this.onTapShare,
    required this.onTapImage,
    this.onTapEditReport,
    this.onTapCheck,
  });

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      height: 183,
      decoration: BoxDecoration(
        boxShadow: [
          BoxShadow(
            color: Colors.grey[300]!,
            offset: Offset(2.0, 2.0), //阴影xy轴偏移量
            blurRadius: 5.0, //阴影模糊程度
            spreadRadius: 2.0, //阴影扩散程度
          )
        ],
        borderRadius: BorderRadius.all(
          Radius.circular(8),
        ),
        color: Colors.white,
      ),
      margin: EdgeInsets.only(left: 7, right: 7, top: 10),
      child: FColumn(
        children: [
          _buildReportInfoView(),
          const FSizedBox(height: 5),
          FDivider(
            height: 1,
            thickness: 1,
            color: Colors.grey[200],
          ),
          _buildReportOperateButtons(context, index, reportInfo),
        ],
      ),
    );
  }

  ///报告内容展示
  FWidget _buildReportInfoView() {
    bool isAIReport = false;
    final diagnosisLabels = reportInfo.diagnosisLabels ?? [];
    if (diagnosisLabels.contains('VAid')) {
      isAIReport = true;
    }
    final reportPreviewList = reportInfo.reportPreviewList ?? [];
    var reportPreview = '';
    if (reportPreviewList.isNotEmpty) {
      var token = reportPreviewList.first.fileToken;
      if (token.isNotNullOrEmpty) {
        var tokens = token!.split(';');
        if (tokens.isNotEmpty) {
          reportPreview = tokens.first;
        }
      }
    }

    String pName = reportInfo.patientName ?? '';
    if (pName.isEmpty) {
      pName = this.patientName;
    }
    String reportUserName = reportInfo.reportUserName ?? '';
    if (reportUserName.isEmpty && isAIReport) {
      final labels = reportInfo.diagnosisLabels ?? [];
      reportUserName = labels.isNotEmpty
          ? reportInfo.diagnosisLabels![0]
          : i18nBook.homePage.notYet.t;
    }
    DateTime reportTime = reportInfo.reportTime ?? DateTime.now();
    return FContainer(
      margin: EdgeInsets.symmetric(vertical: 15, horizontal: 15),
      child: FRow(children: [
        if (reportPreviewList.isNotEmpty && reportPreview.isNotEmpty) ...[
          FImage.network(
            reportPreview,
            width: 70.92,
            height: 100,
          ),
        ] else ...[
          FContainer(
            width: 70.92,
            height: 100,
            child: FIcon(
              FIcons.report_a4,
              size: 70,
            ),
          )
        ],
        const FSizedBox(width: 15),
        FColumn(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            FContainer(
              width: 160,
              child: FText(
                pName,
                overflow: TextOverflow.ellipsis,
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            FText(
              reportManager.getTime(reportTime.toString()),
              style: _textStyle,
            ),
            FText(
              isAIReport
                  ? i18nBook.remedical.aiReport.t
                  : i18nBook.remedical.cloudReport.t,
              style: _textStyle,
            ),
            FText(
              i18nBook.remedical.reporter.t + " " + reportUserName,
              style: _textStyle,
            ),
          ],
        ),
        FExpanded(child: FSizedBox()),
        _buildAIIcon(),
        FSizedBox(width: 15),
      ]),
    );
  }

  FWidget _buildAIIcon() {
    final reportOrgan = reportInfo.reportOrgan;
    Color aiColor = Colors.black;
    if (reportOrgan == DiagnosisOrganEnum.Null) {
      return FSizedBox();
    } else {
      /// 老数据没有当前AI的良恶性状态 兼容老数据
      /// 老数据： ["VAid"] / 新数据： ["VAid", "BenignAndMalignant"]
      if (reportInfo.diagnosisLabels?.length == 2) {
        aiColor = AIIcon.buildAIIconColor(
          reportInfo.diagnosisLabels![1],
        );
      }
      return FIcon(
        AIIcon.buildAIIcon(reportOrgan),
        size: 30,
        color: aiColor,
      );
    }
  }

  ///报告相关操作按钮
  FWidget _buildReportOperateButtons(
      BuildContext context, int index, ReportDTO reportInfo) {
    return FExpanded(
      child: FRow(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          FCustomFlatButton(
            i18nBook.remedical.share.t,
            () {
              onTapShare.call(reportInfo.reportCode ?? '');
              reportManager.showBottomSheet(reportInfo);
            },
            businessParent: this.businessParent,
          ),
          FVerticalDivider(
            width: 1,
            thickness: 1,
            color: Colors.grey[200],
          ),
          if (onTapCheck != null) ...[
            FCustomFlatButton(
              i18nBook.remedical.check.t,
              () async {
                onTapCheck!.call();
              },
              businessParent: this.businessParent,
            ),
            FVerticalDivider(
              width: 1,
              thickness: 1,
              color: Colors.grey[200],
            ),
          ],
          FCustomFlatButton(
            i18nBook.remedical.image.t,
            () async {
              onTapImage.call(index);
            },
            businessParent: this.businessParent,
          ),
          FVerticalDivider(
            width: 1,
            thickness: 1,
            color: Colors.grey[200],
          ),
          if (reportInfo.canEditReport)
            FCustomFlatButton(
              i18nBook.common.edit.t,
              () {
                onTapEditReport?.call();
              },
              businessParent: this.businessParent,
            ),
        ],
      ),
    );
  }
}
