import 'package:flutter/material.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/controller.dart';
import 'package:fis_measure/interfaces/process/items/item_feature.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/image_measure_overlay/widgets/rim_measure_result_table.dart';
import 'package:get/get.dart';
import 'package:fis_measure/values/colors.dart';
import 'package:fis_measure/view/result/converter.dart';
import 'package:fis_measure/view/result/results_panel.dart';
import 'package:fis_measure/interfaces/process/items/types.dart';
import 'package:fis_ui/index.dart';

class MeasureResultLayer extends GetView<ImageMeasureOverlayController> {
  MeasureResultLayer({
    Key? key,
    required this.tabHashCode,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final _scrollController = ScrollController();

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageMeasureOverlayController>(
      tag: tabHashCode,
      id: ImageMeasureOverlayController.measureResultLayerId,
      builder: (_) {
        if (controller.app == null) return const SizedBox();
        final List<ResultLine> lines = [];
        final List<IMeasureItemFeature> features = [];

        for (var item in controller.app!.measureItems) {
          if (item.measuredFeatures.isNotEmpty) {
            features.addAll(item.measuredFeatures);
          }
          if (item.feature != null) {
            features.add(item.feature!);
          }
        }

        final idLength = features.length.toString().length;
        for (var feature in features) {
          final strList = FeatureValueDescConverter(feature).generate(idLength);
          if (feature.refItem.meta.measureType ==
              MeasureTypes.URMShellDenMeasure) {
            strList.forEach((element) {
              if (element.label == "  Area" ||
                  element.label.contains("Density Analysis"))
                lines.add(element);
            });

            // controller.onPerfusionColorbarChanged!.call(true);
          } else if (feature.refItem.meta.measureType ==
              MeasureTypes.URMShellVelMeasure) {
            strList.forEach((element) {
              if (element.label == "  Area" ||
                  element.label.contains("Velocity Analysis"))
                lines.add(element);
            });

            // controller.onPerfusionColorbarChanged!.call(true);
          } else if (feature.refItem.meta.measureType ==
              MeasureTypes.URMShellDenVelMeasure) {
            strList.forEach((element) {
              if (element.label == "  Area" ||
                  element.label.contains("DV Analysis")) lines.add(element);
            });
          } else {
            lines.addAll(strList);
          }
        }

        if (!lines.isNotEmpty ||
            !controller
                .measureDataController!.measureSystemSetting.showResultWindow) {
          return const SizedBox();
        }

        final aliment = _getAlignmentFromLocation(controller
            .measureDataController!.measureSystemSetting.showResultLocation);
        final double fontSize = controller
            .measureDataController!.measureSystemSetting.fontSize
            .toDouble();
        final mainList = <FWidget>[
          _buildTitle(controller.app!.applicationName, fontSize),
          _buildTitleDivider(),
        ];
        for (var line in lines) {
          mainList.add(_buildLine(line, fontSize));
        }

        return Align(
          alignment: aliment,
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                margin: const EdgeInsets.only(
                    left: 10, top: 0, right: 10, bottom: 10),
                constraints: const BoxConstraints(
                  maxWidth: 600,
                  minWidth: 150,
                  maxHeight: 400,
                ),
                decoration: BoxDecoration(
                  border: Border.all(
                    color: MeasureColors.ResultBorder,
                    width: 2,
                  ),
                  borderRadius: BorderRadius.circular(4),
                  color: Colors.black.withOpacity(0.7),
                ),
                child: FSingleChildScrollView(
                  controller: _scrollController,
                  child: FRepaintBoundary(
                    child: FIntrinsicHeight(
                      child: FIntrinsicWidth(
                        child: FContainer(
                          margin: const EdgeInsets.all(8).copyWith(top: 2),
                          child: FColumn(
                            mainAxisSize: MainAxisSize.min,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: mainList,
                          ),
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              RimMeasureResultTable(
                tabHashCode: tabHashCode,
              )
            ],
          ),
        );
      },
    );
  }

  /// 面板对齐方式
  Alignment _getAlignmentFromLocation(int locationNum) {
    switch (locationNum) {
      case 0:
        return Alignment.topLeft;
      case 1:
        return Alignment.bottomLeft;
      case 2:
        return Alignment.topRight;
      case 3:
        return Alignment.bottomRight;
      default:
        return Alignment.topLeft;
    }
  }

  FWidget _buildLine(ResultLine result, double fontSize) {
    return FRow(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      FText(
        result.label,
        style: TextStyle(
          fontSize: fontSize,
          color: MeasureColors.Primary,
        ),
      ),
      const FSizedBox(width: 8),
      FText(
        result.value ?? '',
        style: TextStyle(
          fontSize: fontSize,
          color: MeasureColors.Primary,
        ),
      ),
    ]);
  }

  FWidget _buildTitle(String title, double fontSize) {
    return FText(
      title,
      style: TextStyle(
        fontSize: fontSize - 2,
        color: MeasureColors.Primary,
      ),
    );
  }

  FWidget _buildTitleDivider() {
    return const FDivider(
      color: Colors.white,
      height: 0.5,
      endIndent: 2,
    );
  }
}
