import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:fis_lib_business_components/index.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/translation_delegate_helper.dart';
import 'package:flyinsono/views/desktop/laboratory/controller.dart';
import 'package:get/get.dart';

class ImageAnalysis extends FStatefulWidget {
  const ImageAnalysis({super.key});

  @override
  FState<ImageAnalysis> createState() => _ImageAnalysisState();
}

class _ImageAnalysisState extends FState<ImageAnalysis> {
  final controller = Get.find<LaboratoryController>();

  List<RemedicalInfoDTO>? imageList = [];

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

  @override
  FWidget build(BuildContext context) {
    return FContainer(
      padding: EdgeInsets.all(
        5,
      ),
      child: FObx(
        () => _buildStreamingImage(controller.state.imageList),
      ),
    );
  }

  FWidget _imageWrap(List<FWidget> wrapList, [String? title]) {
    if (wrapList.length == 0)
      return FCenter(
        child: FContainer(
          child: FText(title ?? ''),
        ),
      );
    return FWrap(
      spacing: 8.0, // 主轴(水平)方向间距
      runSpacing: 8.0, // 纵轴（垂直）方向间距
      direction: Axis.horizontal,
      alignment: WrapAlignment.start,
      children: wrapList,
    );
  }

  /// 三种图片列表封装
  FWidget _buildStreamingImage(
    List<RemedicalInfoDTO>? imageList,
  ) {
    int _index = 0;
    void selectImage(int imageIndex) {
      controller.openVidMovieDialog(
        imageList![imageIndex].terminalImages!.imageUrl!,
        imageIndex,
        imageList[imageIndex].remedicalCode!,
      );
    }

    /// 设置图片翻译
    String setDescription(int imageIndex) {
      return translation_delegate_helper.translateDescription(
        imageList![imageIndex].application ?? '',
        imageList[imageIndex].applicationCategory ?? '',
      );
    }

    /// 设置图像定位信息
    String setLocationDescription(int imageIndex) {
      return translation_delegate_helper.translateLocationDescription(
        imageList![imageIndex].imageLocation?.quadrant,
        imageList[imageIndex].imageLocation?.position,
      );
    }

    bool hasImageList = imageList != null && imageList.isNotEmpty;

    if (hasImageList) {
      /// 图片布局
      return _imageWrap(
        List<FWidget>.generate(
          imageList.length,
          (index) {
            final image = FContentImage(
              remedicalInfo: imageList[index],
              onDoubleTap: () => selectImage(index),
              onTap: () => selectImage(index),
              serialNo: _index + 1,
              description: setDescription(index),
              locationDescription: setLocationDescription(index),
            );
            _index++;
            return image;
          },
        ),
      );
    } else {
      return NoDataWidget();
    }
  }
}
