import 'package:flutter/material.dart';
import 'package:flyinsono/lab/color/lab_colors.dart';
import 'package:flyinsono/lab/components/lab_loading_wrapper.dart';
import 'package:flyinsono/lab/mock_data/mock_rpc.dart';
import 'package:flyinsono/lab/pages/lab_data/controller.dart';
import 'package:flyinsono/lab/pages/lab_data/widgets/us_image_thumbnail.dart';
import 'package:flyinsono/lab/style/lab_box_decoration.dart';
import 'package:flyinsono/utils/datetime.dart';
import 'package:get/get.dart';
import 'package:fis_jsonrpc/rpc.dart';

class USImagePool extends GetView<LabDataController> {
  const USImagePool({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<LabDataController>(
      id: LabDataController.imageListId,
      builder: (context) {
        return Container(
          decoration: LabBoxDecoration.base,
          child: LabLoadingWrapper(
            decoration: LabBoxDecoration.base,
            loading: controller.isImageListLoading,
            child: Padding(
              padding: const EdgeInsets.all(5),
              child: GridView.builder(
                padding: const EdgeInsets.all(5),
                gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 6,
                  crossAxisSpacing: 5,
                  mainAxisSpacing: 5,
                  childAspectRatio: 3 / 2,
                ),
                itemCount: controller.imageList.length,
                itemBuilder: (context, index) {
                  var imageType =
                      controller.imageList[index].vidDataInfo.imageType;
                  var image = controller.imageList[index];
                  bool isVideo = [
                        SampleRemedicalFileDataTypeEnum.AnalysisVideo,
                        SampleRemedicalFileDataTypeEnum.VinnoVidMovie,
                        SampleRemedicalFileDataTypeEnum.ThirdVidMovie,
                        SampleRemedicalFileDataTypeEnum.AnalysisVideo,
                      ].contains(image.fileDataType) &&
                      imageType != ImageType.URM_Result;
                  return MouseRegion(
                    cursor: SystemMouseCursors.click,
                    child: GestureDetector(
                      onTap: () {
                        controller.clickImage(index);
                      },
                      child: USImageThumbnail(
                        imageUrl:
                            controller.imageList[index].vidDataInfo.coverUrl,
                        description: ((index + 1).toString() +
                            " " +
                            controller.imageList[index].fileTypeName +
                            " ${DataTimeUtils.convertToLocalString(controller.imageList[index].createTime)}"),
                        labelBuilder: _buildLabel(
                          controller.imageList[index].vidDataInfo.imageType,
                        ),
                        isVideo: isVideo,
                      ),
                    ),
                  );
                },
              ),
            ),
          ),
        );
      },
    );
  }

  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(
          '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;
    }
  }
}
