import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_measure/view/measure/measure_images_bar.dart';
import 'package:fis_theme/theme.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/pages/remedical/translation_delegate_helper.dart';
import 'package:flyinsono/pages/remedical/consultation/controller.dart';

///扫描图片对话框
class ScannedImageDialog extends FisView<ConsultationController>
    implements FPage {
  /// 检查编码
  final String recordCode;

  @override
  final String pageName;

  /// 图片扫查列表
  final GetRecordsPageDTO? checkLists;

  /// 病人信息
  final Map<String, dynamic>? patientInfo;

  ScannedImageDialog({
    Key? key,
    required this.recordCode,
    this.checkLists,
    this.patientInfo,
    this.pageName = 'ScannedImageDialog',
  }) : super(key: key);

  @override
  FWidget build(BuildContext context) {
    return FSimpleDialog(
      title: FText(
        i18nBook.remedical.scanImage.t,
        style: TextStyle(
          color: Colors.white,
          fontSize: 18,
        ),
      ),
      okString: i18nBook.common.confirm.t,
      cancelString: i18nBook.common.cancel.t,
      insetPadding: const EdgeInsets.symmetric(
        vertical: 100,
      ),
      children: [
        FContainer(
          width: 730,
          height: 500,
          child: _buildScannedImageDialog(),
        ),
      ],
    );
  }

  FWidget _buildScannedImageDialog() {
    return FContainer(
      child: FRow(
        children: [
          FContainer(
            margin: EdgeInsets.only(top: 30, left: 15),
            width: 100,
            child: FObx(
              () => FColumn(
                children: [
                  _buildTabList(
                    0,
                    i18nBook.remedical.cloudImage.t,
                  ),
                  _buildTabList(
                    1,
                    i18nBook.measure.measureImage.t,
                  ),
                ],
              ),
            ),
          ),
          FContainer(
            width: 1,
            color: FTheme.ins.colorScheme.line,
          ),
          _buildScanImageBody(),
        ],
      ),
    );
  }

  FWidget _buildTabList(int tabIndex, String title) {
    return FContainer(
      width: double.infinity,
      height: 50,
      decoration: BoxDecoration(
        color: controller.state.cloudImageIndex == tabIndex
            ? FTheme.ins.colorScheme.primary
            : Colors.white,
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(4),
          bottomLeft: Radius.circular(4),
        ),
      ),
      child: FCenter(
        child: FTextButton(
          businessParent: this,
          name: "setindex:$tabIndex",
          style: ButtonStyle(
            padding: MaterialStateProperty.all(
              EdgeInsets.symmetric(vertical: 15, horizontal: 15),
            ),
          ),
          onPressed: () => controller.state.cloudImageIndex = tabIndex,
          child: FText(
            title,
            style: TextStyle(
              color: controller.state.cloudImageIndex == tabIndex
                  ? Colors.white
                  : Colors.black,
            ),
          ),
        ),
      ),
    );
  }

  /// 扫查图片主体
  FWidget _buildScanImageBody() {
    return FObx(
      () => controller.state.cloudImageIndex == 0
          ? FExpanded(
              child: FColumn(
                children: [
                  FContainer(
                    margin: EdgeInsets.only(
                      top: 15,
                      left: 15,
                      right: 15,
                    ),
                    padding: EdgeInsets.all(10),
                    width: double.infinity,
                    color: FTheme.ins.colorScheme.secondary,
                    child: FText(
                      i18nBook.remedical.scanDate.translate(
                          [controller.state.getScannedImage.scanDate]),
                    ),
                  ),
                  FExpanded(
                    child: FSingleChildScrollView(
                      child: FColumn(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.start,
                        children: [
                          FContainer(
                            child: _buildContainer(
                              controller.state.getScannedImage,
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            )
          : FExpanded(
              child: FContainer(
                alignment: Alignment.topLeft,
                padding: EdgeInsets.only(left: 10),
                child: FSingleChildScrollView(
                  child: FColumn(
                    mainAxisAlignment: MainAxisAlignment.start,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      FContainer(
                        margin: EdgeInsets.only(right: 15),
                        child: FWrap(
                          alignment: WrapAlignment.start,
                          children: List<FWidget>.generate(
                            controller.state.remedicalMeasured.length,
                            (index) {
                              return FContainer(
                                margin: EdgeInsets.symmetric(
                                  horizontal: 5,
                                  vertical: 5,
                                ),
                                color: Colors.black,
                                child: FImage.network(
                                  controller.state.remedicalMeasured[index]
                                          .previewFileToken ??
                                      '',
                                  width: 200,
                                  height: 160,
                                ),
                              );
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
    );
  }

  /// 层级较深 检查图片的参数
  FWidget _buildContainer(RemedicalListResult? remedicalItem) {
    return FContainer(
      margin: EdgeInsets.symmetric(
        vertical: 5,
        horizontal: 15,
      ),
      child: FColumn(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          FListView.builder(
            shrinkWrap: true,
            itemCount: remedicalItem?.remedicalItemList?.length,
            itemBuilder: (BuildContext context, int remedicalItemListIndex) {
              return FColumn(
                children: [
                  FContainer(
                    height: 25,
                    margin: EdgeInsets.only(left: 15),
                    alignment: Alignment.centerLeft,
                    child: FText(
                      remedicalItem?.remedicalItemList?[remedicalItemListIndex]
                              .patientScanTypeDesc ??
                          '',
                    ),
                  ),
                  FContainer(
                    margin: EdgeInsets.only(left: 15),
                    alignment: Alignment.centerLeft,
                    child: FWrap(
                      spacing: 16.0, // 主轴(水平)方向间距
                      runSpacing: 8.0, // 纵轴（垂直）方向间距
                      direction: Axis.horizontal,
                      alignment: WrapAlignment.start,
                      children: List<FWidget>.generate(
                        remedicalItem!
                            .remedicalItemList![remedicalItemListIndex]
                            .remedicalList!
                            .length,
                        (index) {
                          final currentRemedicalItem = remedicalItem
                              .remedicalItemList![remedicalItemListIndex]
                              .remedicalList![index];
                          return FContentImage(
                            remedicalInfo: currentRemedicalItem,
                            onDoubleTap: () => controller.openVidMovieDialog(
                              currentRemedicalItem.terminalImages!.imageUrl!,
                              index,
                              currentRemedicalItem.remedicalCode!,
                              currentRemedicalItem.recordCode!,
                            ),
                            onTap: () => controller.openVidMovieDialog(
                              currentRemedicalItem.terminalImages!.imageUrl!,
                              index,
                              currentRemedicalItem.remedicalCode!,
                              currentRemedicalItem.recordCode!,
                            ),
                            locationDescription: translation_delegate_helper
                                .translateLocationDescription(
                              currentRemedicalItem.imageLocation?.quadrant,
                              currentRemedicalItem.imageLocation?.position,
                            ),
                            serialNo: index + 1,
                            description: translation_delegate_helper
                                .translateDescription(
                                    currentRemedicalItem.application,
                                    currentRemedicalItem.applicationCategory),
                          );
                        },
                      ),
                    ),
                  ),
                ],
              );
            },
          ),
        ],
      ),
    );
  }
}
