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/components/rect_icon_button.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/utils/datetime.dart';
import 'package:get/get.dart';
import 'index.dart';
import 'package:fis_jsonrpc/rpc.dart';

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

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final ImageDataSourcePanelController initController;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ImageDataSourcePanelController>(
      init: initController,
      tag: tabHashCode,
      id: ImageDataSourcePanelController.imageDataSourcePanelId,
      builder: (_) {
        return Column(
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            // _buildRecordInfo(),
            // _buildImageSourceSelector(),
            // SizedBox(
            //   height: 10,
            // ),
            Expanded(
              child: _buildImageList(),
            ),
          ],
        );
      },
    );
  }

  // 实验样本/病人名 + 检查名
  Widget _buildRecordInfo() {
    return GetBuilder<ImageDataSourcePanelController>(
      tag: tabHashCode,
      id: ImageDataSourcePanelController.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,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget _buildImageList() {
    return GetBuilder<ImageDataSourcePanelController>(
      tag: tabHashCode,
      id: ImageDataSourcePanelController.imageListId,
      builder: (context) {
        return Container(
          decoration: LabBoxDecoration.base,
          // padding: const EdgeInsets.only(left: 5, top: 5, right: 5),
          child: Column(
            children: [
              // if (controller.isExistUrmData) _buildURMSwitch(),
              Expanded(
                child: ListView(
                  controller: controller.imageListScrollController,
                  // key: controller.imageItemPrototypeItemKey,
                  prototypeItem: ImageListItemPrototypeItem(
                    aspectRatio: 200 / 133,
                    key: controller.imageItemPrototypeItemKey,
                  ),
                  children: controller.imageDataList.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 Container(
                      margin: EdgeInsets.all(1),
                      child: ImageListItem(
                        imageType: e.value.vidDataInfo.imageType,
                        imagePreviewUrl: e.value.vidDataInfo.coverUrl,
                        description:
                            " ${DataTimeUtils.convertToLocalString(e.value.createTime)}",
                        isSelected: controller.selectedImageIndex == e.key,
                        isSelectUrmData: controller.isSelectUrmData,
                        onSelected: () {
                          controller.selectImage(e.key);
                        },
                        labelBuilder:
                            _buildLabel(e.value.vidDataInfo.imageType),
                        isVideo: isVideo,
                      ),
                    );
                  }).toList(),
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 5, bottom: 5),
                height: 30,
                alignment: Alignment.centerRight,
                width: double.infinity,
                child: RectIconButton(
                  icon: Icons.refresh_rounded,
                  tooltipMessage: "刷新",
                  onPressed: () {
                    controller.onRefersh?.call("");
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Widget? _buildLabel(ImageType imageType) {
    switch (imageType) {
      case ImageType.URM:
        return Text(
          'URM',
          style: TextStyle(
            color: LabColors.green400,
            fontWeight: FontWeight.bold,
            fontSize: 10,
          ),
        );
      case ImageType.URM_Result:
        return Text(
          'URM 分析结果',
          style: TextStyle(
            color: LabColors.green400,
            fontWeight: FontWeight.bold,
            fontSize: 12,
          ),
        );
      case ImageType.Measure_Result:
        return Text(
          '图像',
          style: TextStyle(
            color: LabColors.green400,
            fontWeight: FontWeight.bold,
            fontSize: 12,
          ),
        );
      case ImageType.Video:
        return Text(
          '视频',
          style: TextStyle(
            color: LabColors.green400,
            fontWeight: FontWeight.bold,
            fontSize: 12,
          ),
        );
      default:
        return null;
    }
  }
}
