import 'package:flyinsono/lab/components/image_list_item.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'package:fis_jsonrpc/rpc.dart';

class ImageDataToAnalysis extends GetView<ImageDataToAnalysisController> {
  const ImageDataToAnalysis({
    Key? key,
    required this.initController,
    required this.tabHashCode,
  }) : super(key: key);

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final ImageDataToAnalysisController initController;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageDataToAnalysisController>(
      init: initController,
      tag: tabHashCode,
      id: ImageDataToAnalysisController.imageDataToAnalysisId,
      builder: (_) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            _buildRecordInfo(),
            _buildURMFilterSwitch(),
            Expanded(
              child: _buildImageList(),
            ),
          ],
        );
      },
    );
  }

  // 实验样本/病人名 + 检查名
  Widget _buildRecordInfo() {
    return GetBuilder<ImageDataToAnalysisController>(
      tag: tabHashCode,
      id: ImageDataToAnalysisController.recordInfo,
      builder: (context) {
        return Container(
          padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 5),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                controller.patientName,
                style: TextStyle(
                  fontSize: 14,
                  color: LabColors.text900,
                ),
              ),
              Expanded(
                child: SizedBox(
                  width: 10,
                ),
              ),
              Text(
                controller.recordName,
                style: TextStyle(
                  fontSize: 14,
                  color: LabColors.text900,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  // 仅显示 URM 数据开关
  Widget _buildURMFilterSwitch() {
    return Container(
      child: Row(
        children: [
          SizedBox(
            width: 5,
          ),
          Text(
            '仅显示 URM 数据',
            style: TextStyle(
              fontSize: 12,
              color: LabColors.text600,
            ),
          ),
          Expanded(child: SizedBox()),
          Switch(
            activeColor: LabColors.base600,
            inactiveThumbColor: LabColors.base100,
            inactiveTrackColor: LabColors.base400,
            value: controller.onlyURMData,
            onChanged: (value) {
              print('onlyURMData: $value');
              controller.setOnlyURMData(value);
            },
          ),
        ],
      ),
    );
  }

  Widget _buildImageList() {
    return GetBuilder<ImageDataToAnalysisController>(
      tag: tabHashCode,
      id: ImageDataToAnalysisController.imageListId,
      builder: (context) {
        return Container(
          decoration: LabBoxDecoration.base,
          padding: const EdgeInsets.only(left: 5, top: 5, right: 5),
          child: ListView(
            controller: controller.imageListScrollController,
            prototypeItem: ImageListItemPrototypeItem(
              aspectRatio: 4 / 3,
              key: controller.imageItemPrototypeItemKey,
            ),
            children:
                controller.getFilteredImageList().asMap().entries.map((e) {
              var type = e.value.vidDataInfo.sampleRemedicalFileDataTypeEnum;
              var imageType = e.value.vidDataInfo.imageType;
              bool isVideo = [
                    SampleRemedicalFileDataTypeEnum.AnalysisVideo,
                    SampleRemedicalFileDataTypeEnum.VinnoVidMovie,
                    SampleRemedicalFileDataTypeEnum.ThirdVidMovie,
                    SampleRemedicalFileDataTypeEnum.AnalysisVideo,
                  ].contains(type) &&
                  imageType != ImageType.URM_Result;
              return ImageListItem(
                imageType: imageType,
                imagePreviewUrl: e.value.vidDataInfo.coverUrl,
                description: e.value.description,
                isSelected: controller.selectedImageId == e.value.id,
                onSelected: () {
                  controller.selectImage(e.value);
                },
                labelBuilder: _buildLabel(e.value),
                isVideo: isVideo,
              );
            }).toList(),
          ),
        );
      },
    );
  }

  Widget? _buildLabel(ImageModel imageModel) {
    if (imageModel.vidDataInfo.imageType == ImageType.URM) {
      return controller.roiSelectedImageIds.contains(imageModel.id)
          ? Text(
              '已框选 ROI',
              style: TextStyle(
                color: LabColors.green400,
                // fontWeight: FontWeight.bold,
                fontSize: 12,
              ),
            )
          : Text(
              '未框选 ROI',
              style: TextStyle(
                color: Color.fromARGB(255, 224, 221, 78),
                // fontWeight: FontWeight.bold,
                fontSize: 12,
              ),
            );
    }
    return Text(
      '不支持 URM',
      style: TextStyle(
        color: LabColors.text400,
        // fontWeight: FontWeight.bold,
        fontSize: 12,
      ),
    );
  }
}
