import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/pages/remedical/translation_delegate_helper.dart';


class ImageDescribeView extends FStatelessWidget {
  final DiagnosisConclusionEnum? diagnosisConclusion;
  final List<DiagnosisOrganEnum>? diagnosisOrgans;
  final String? application;
  final String? applicationCategory;

  /// 判断是否有AI器官
  bool get hasDiagnosisOrgans => diagnosisOrgans?.length != 0;
  bool get isShowDiagnosisOrgans => [
        DiagnosisConclusionEnum.NoObviousLesion,
        DiagnosisConclusionEnum.Benign,
        DiagnosisConclusionEnum.Malignant,
        DiagnosisConclusionEnum.BenignAndMalignant
      ].contains(diagnosisConclusion);
  const ImageDescribeView({
    this.diagnosisConclusion = DiagnosisConclusionEnum.Unrecognized,
    this.diagnosisOrgans,
    this.application,
    this.applicationCategory,
  });
  dynamic _buildAITextColor(DiagnosisConclusionEnum diagnosisConclusion) {
    switch (diagnosisConclusion) {
      case DiagnosisConclusionEnum.NoObviousLesion:
        return Colors.lightBlue;
      case DiagnosisConclusionEnum.Benign:
        return Colors.greenAccent;
      case DiagnosisConclusionEnum.Malignant:
        return Colors.orangeAccent;
      case DiagnosisConclusionEnum.BenignAndMalignant:
        return Colors.orangeAccent;
      default:
        return Colors.transparent;
    }
  }

  ///判断是否是肝脏、胆囊、肾脏、脾脏、腹部器官
  bool _ifAbdominalOrgans(DiagnosisOrganEnum diagnosisOrgan) {
    final abdominalOrgansList = [
      DiagnosisOrganEnum.Liver,
      DiagnosisOrganEnum.Cholecyst,
      DiagnosisOrganEnum.Kidney,
      DiagnosisOrganEnum.Spleen,
      DiagnosisOrganEnum.Abdomen
    ];
    return abdominalOrgansList.contains(diagnosisOrgan);
  }

  FWidget _buildAIDiagnosticOrgans(DiagnosisOrganEnum? diagnosisOrganEnum) {
    switch (diagnosisOrganEnum) {
      case DiagnosisOrganEnum.Breast:
        return FIcon(
          FIcons.fis_breast,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Abdomen:
        return FIcon(
          FIcons.fis_abdomen,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );

      case DiagnosisOrganEnum.Liver:
        return FIcon(
          FIcons.fis_liver,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Cholecyst:
        return FIcon(
          FIcons.fis_gallbladder,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Kidney:
        return FIcon(
          FIcons.fis_kidney,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Spleen:
        return FIcon(
          FIcons.fis_spleen,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.CarotidArtery:
        return FIcon(
          FIcons.arteria_carotis,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );

      case DiagnosisOrganEnum.Thyroid:
        return FIcon(
          FIcons.fis_thyroid,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Neck:
        return FIcon(
          //图标待定
          FIcons.fis_thyroid,
          color: _buildAITextColor(
            diagnosisConclusion!,
          ),
        );
      case DiagnosisOrganEnum.Null:
        if (diagnosisOrgans?.length == 1) {
          return _buildDescribeApplication();
          //如果diagnosisOrgans只有一个值且该值为null值，就返回文字，否则返回空
        } else {
          return FText('');
        }

      default:
        return _buildDescribeApplication();
    }
  }

  FWidget _buildLeftAIOrCarotidLogo() {
    if (hasDiagnosisOrgans) {
      int count = 0;
      final List<FWidget> aIDiagnosticOrgansIconList = [];
      if (diagnosisOrgans != null) {
        for (var i = 0; i < diagnosisOrgans!.length; i++) {
          if (_ifAbdominalOrgans(diagnosisOrgans![i])) {
            //当diagnosisOrgans返回值为肝脏、胆囊、肾脏、脾脏,都只显示一个“腹部”图标
            if (count <= 0) {
              aIDiagnosticOrgansIconList.add(
                _buildAIDiagnosticOrgans(DiagnosisOrganEnum.Abdomen),
              );
            }
            count++;
          } else {
            aIDiagnosticOrgansIconList.add(
              _buildAIDiagnosticOrgans(
                diagnosisOrgans?[i],
              ),
            );
          }
        }
      }

      return FPositioned(
        bottom: 15,
        left: 15,
        child: FRow(
          children: aIDiagnosticOrgansIconList,
        ),
      );
    }
    return _buildDescribeApplication();
  }

  FWidget _buildDescribeApplication() {
    return FPositioned(
      left: 15,
      bottom: 15,
      child: FText(
        translation_delegate_helper.translateDescription(
          application,
          applicationCategory,
        ),
        style: TextStyle(
          color: Colors.white,
          overflow: TextOverflow.ellipsis,
        ),
      ),
    );
  }

  FWidget _buildRigthAIText() {
    if (isShowDiagnosisOrgans) {
      return FPositioned(
        right: 15,
        bottom: 12,
        child: FMaterialTooltip(
          textStyle: TextStyle(
            fontSize: 16,
            color: Colors.white,
          ),
          message: _buildAIText(diagnosisConclusion!),
          child: FText(
            'AI',
            style: TextStyle(
              color: _buildAITextColor(
                diagnosisConclusion!,
              ),
              fontSize: 20,
            ),
          ),
        ),
      );
    }

    return FSizedBox();
  }

  String _buildAIText(DiagnosisConclusionEnum diagnosisConclusion) {
    switch (diagnosisConclusion) {
      case DiagnosisConclusionEnum.NotRequired:
        return i18nBook.remedical.noNeedAIYet.t;
      case DiagnosisConclusionEnum.InProcess:
        return i18nBook.remedical.didNotHandle.t;
      case DiagnosisConclusionEnum.Unrecognized:
        return i18nBook.remedical.didNotIdentify.t;
      case DiagnosisConclusionEnum.NoObviousLesion:
        return i18nBook.remedical.noAbnormal.t;
      case DiagnosisConclusionEnum.Benign:
        return i18nBook.remedical.benign.t;
      case DiagnosisConclusionEnum.Malignant:
        return i18nBook.remedical.benignWithMalignant.t;
      case DiagnosisConclusionEnum.BenignAndMalignant:
        return i18nBook.remedical.benignWithMalignant.t;
      default:
        return i18nBook.remedical.didNotIdentify.t;
    }
  }

  @override
  FWidget build(BuildContext context) {
    return FStack(
      children: [
        _buildRigthAIText(),
        _buildLeftAIOrCarotidLogo(),
      ],
    );
  }
}
