import 'package:flutter/material.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:get/get.dart';

class ImageDataToAnalysisController extends GetxController {
  ImageDataToAnalysisController({
    this.onImageSelected,
  });

  static const imageDataToAnalysisId = "image_data_to_analysis"; // 左侧面板
  static const imageListId = "image_list"; // 图像列表
  static const recordInfo = "record_info"; // 实验样本/病人名 + 检查名

  final ValueChanged<VidDataInfo>? onImageSelected;

  List<ImageModel> originImageDataList = [];

  // 是否只显示 URM 数据
  bool onlyURMData = true;

  /// 已完成roi框选的图像id列表
  List<String> roiSelectedImageIds = [];

  String patientName = "";
  String recordName = "";

  final ScrollController imageListScrollController = ScrollController();

  final GlobalKey imageItemPrototypeItemKey = GlobalKey();

  /// 选中的图片的 Id
  String? selectedImageId;

  // 选择图片（左侧图像选择器）
  void selectImage(ImageModel image) {
    if (selectedImageId == image.id) {
      return;
    }
    selectedImageId = image.id;
    onImageSelected?.call(image.vidDataInfo);
    update([imageListId]);
  }

  // 选择图片无校验（左侧图像选择器）
  void selectImageNoCheck(ImageModel image) {
    selectedImageId = image.id;
    onImageSelected?.call(image.vidDataInfo);
    update([imageListId]);
  }

  // 滚动列表直至图像进入可视区域
  void scrollToImage(int index) {
    try {
      final RenderBox renderBox = imageItemPrototypeItemKey.currentContext!
          .findRenderObject() as RenderBox;
      final double itemHeight = renderBox.size.height;
      final double offset = itemHeight * index;
      imageListScrollController.animateTo(
        offset,
        duration: const Duration(milliseconds: 200),
        curve: Curves.easeInOut,
      );
    } catch (e) {
      print(e);
    }
  }

  // 根据实验样本（病人）、检查、图像 来配置合适的数据
  void updateImageList(
    PatientModel patient,
    RecordModel record,
    List<ImageModel> images,
  ) async {
    if (images.isEmpty) {
      selectedImageId = null;
      return;
    }
    patientName = patient.name;
    recordName = record.description;
    update([recordInfo]);
    originImageDataList = images;
    // 选中一个 urm 数据
    final firstURMImage = images.firstWhere(
      (element) => element.vidDataInfo.imageType == ImageType.URM,
      orElse: () => images.first,
    );
    selectImage(firstURMImage);
  }

  // 更新已框选 ROI 的图像 id 列表
  void updateRoiSelectedImageIds(List<String> ids) {
    roiSelectedImageIds = ids;
    update([imageListId]);
  }

  /// 获取过滤后的图像列表
  List<ImageModel> getFilteredImageList() {
    if (onlyURMData) {
      return originImageDataList
          .where((element) => element.vidDataInfo.imageType == ImageType.URM)
          .toList();
    }
    return originImageDataList;
  }

  // 设置是否开启过滤
  void setOnlyURMData(bool value) {
    onlyURMData = value;
    // 如果当前选中的图像不是 URM 类型，则重新选择第一个 URM 类型的图像
    if (onlyURMData &&
        selectedImageId != null &&
        originImageDataList
                .firstWhere((element) => element.id == selectedImageId)
                .vidDataInfo
                .imageType !=
            ImageType.URM) {
      final firstURMImage = originImageDataList.firstWhere(
        (element) => element.vidDataInfo.imageType == ImageType.URM,
        orElse: () => originImageDataList.first,
      );
      selectImage(firstURMImage);
    }
    update([imageDataToAnalysisId]);
  }

  // 初始化图像列表
  // void

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
