import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/image_list_item.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_image/widgets/urm_child_data_panel/controller.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:get/get.dart';
import 'package:fis_jsonrpc/rpc.dart';

class UrmChildDataPanel extends GetView<UrmChildDataPanelController> {
  UrmChildDataPanel({
    Key? key,
    required this.tabHashCode,
    required this.initController,
  });

  @override
  String? get tag => tabHashCode;

  final String tabHashCode;

  final UrmChildDataPanelController initController;

  @override
  Widget build(BuildContext context) {
    return GetBuilder<UrmChildDataPanelController>(
        init: initController,
        tag: tabHashCode,
        id: UrmChildDataPanelController.urmAnalysisPanelId,
        builder: (_) {
          return Container(
            child: Column(
              children: [
                Expanded(
                  child: _buildImageList(),
                ),
              ],
            ),
          );
        });
  }

  Widget _buildImageList() {
    return GetBuilder<UrmChildDataPanelController>(
      tag: tabHashCode,
      id: UrmChildDataPanelController.imageListId,
      init: initController,
      builder: (context) {
        return QuickFWidget(
          Listener(
            onPointerSignal: (event) {
              if (event is PointerScrollEvent) {
                const double overScroll = 120;
                final offset = event.scrollDelta.dy / 2;
                double targetOffset =
                    initController.imageListScrollController.offset + offset;
                if (targetOffset > -overScroll &&
                    targetOffset <
                        initController.imageListScrollController.position
                                .maxScrollExtent +
                            overScroll) {
                  initController.imageListScrollController.jumpTo(targetOffset);
                }
              }
            },
            child: Container(
              child: Row(
                children: [
                  Expanded(
                    child: ScrollbarTheme(
                        data: ScrollbarThemeData(
                          radius: Radius.circular(2),
                          thickness: MaterialStateProperty.all(2),
                          thumbColor: MaterialStateProperty.all(
                            LabColors.text400,
                          ), // 这里改变滚动条颜色
                        ),
                        child: Container(
                          decoration: LabBoxDecoration.base,
                          child: Scrollbar(
                            thickness: 2,
                            controller:
                                initController.imageListScrollController,
                            scrollbarOrientation: ScrollbarOrientation.bottom,
                            child: SingleChildScrollView(
                              scrollDirection: Axis.horizontal,
                              controller:
                                  initController.imageListScrollController,
                              key: initController.imageItemPrototypeItemKey,
                              child: Row(children: [
                                ...initController.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(
                                    height: 133,
                                    width: 200,
                                    padding: EdgeInsets.only(bottom: 4, top: 4),
                                    child: Container(
                                      margin: EdgeInsets.all(2),
                                      child: ImageListItem(
                                        imagePreviewUrl:
                                            e.value.vidDataInfo.coverUrl,
                                        imageType:
                                            e.value.vidDataInfo.imageType,
                                        description: e.value.description,
                                        isSelected: e.key ==
                                            initController.selectedImageIndex,
                                        labelBuilder: _buildLabel(
                                            e.value.vidDataInfo.imageType),
                                        onSelected: () {
                                          initController.selectAnalysis(e.key);
                                        },
                                        isVideo: isVideo,
                                      ),
                                    ),
                                  );
                                }).toList(),
                              ]),
                            ),
                          ),
                        )),
                  ),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  Widget? _buildLabel(ImageType imageType) {
    switch (imageType) {
      case ImageType.URM:
        return Text(
          'URM',
          style: TextStyle(
            color: LabColors.green400,
            fontWeight: FontWeight.bold,
            fontSize: 12,
          ),
        );
      case ImageType.URM_Result:
        return Text(
          '分析结果',
          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;
    }
  }
}
