import 'package:fis_i18n/i18n.dart';
import 'package:fis_jsonrpc/rpc.dart';
import 'package:fis_jsonrpc/services/liveConsultation.m.dart';
import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/values/font_sizes.dart';
import 'package:fis_ui/values/spacings.dart';
import 'package:flutter/material.dart';
import 'package:flyinsono/architecture/define.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/managers/interfaces/report.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/widgets/cloud_image_item.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/widgets/quick_preview_plugin.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/controller/real_time_consultation_controller.dart';
import 'package:flyinsono/views/desktop/real_time_consultation/widgets/screenshot_imags.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

///会诊图像
class ConsultationImageView extends FisView<RealTimeConsultationController>
    implements FPage {
  final double _leftMargin = 20;

  final double _imageWidth = 190;
  final ScrollController _scrollController = ScrollController();
  int _index = 0;
  ConsultationImageView({Key? key}) : super(key: key);

  @override
  String get pageName => 'FAppointmentApplication';

  @override
  FWidget build(BuildContext context) {
    controller.imageController
        .getConsultationImages(controller.state.currentConsultationCode);
    return _buildImages(context);
  }

  FWidget _buildCloudImages(List<RemedicalInfoDTO> images) {
    return FContainer(
      margin: EdgeInsets.only(
        left: 10,
        top: 5,
      ),
      child: FWrap(
        spacing: 8.0, // 主轴(水平)方向间距
        runSpacing: 8.0, // 纵轴（垂直）方向间距
        direction: Axis.horizontal,
        alignment: WrapAlignment.start,
        children: List<FWidget>.generate(
          images.length,
          (i) {
            var imageInfo = images[i];
            final image = CloudImageItem(
              _index + 1,
              imageInfo,
              onDoubleTap: () {
                router.back();
                Get.find<IReportManager>().enterVidMeasurePage(
                  imageUrl: imageInfo.terminalImages?.imageUrl ?? '',
                  imageindex: i,
                  remedicalCode: imageInfo.remedicalCode ?? '',
                  patientCode:
                      controller.state.consultationsRecordInfos.patientCode ??
                          '',
                  recordCode: controller.state.currentConsultationCode,
                );
              },
              imageWidth: _imageWidth,
            );
            _index++;
            return image;
          },
        ),
      ),
    );
  }

  ///图像展示
  FWidget _buildImages(BuildContext context) {
    return FObx(
      () {
        if (controller.state.realTimeConsultationImageList.isEmpty) {
          return _mockEmptyView();
        }
        return FSingleChildScrollView(
          child: FColumn(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              FAlwaysScrollListView(
                scrollController: _scrollController,
                child: QuickFWidget(ScrollConfiguration(
                  behavior: ScrollConfiguration.of(context)
                      .copyWith(scrollbars: false),
                  child: FListView.builder(
                    shrinkWrap: true,
                    controller: _scrollController,
                    itemCount:
                        controller.state.realTimeConsultationImageList.length,
                    itemBuilder: (BuildContext context, int index) {
                      return _buildItemImages(index);
                    },
                  ),
                )),
              ),
            ],
          ),
        );
      },
    );
  }

  FWidget _buildItemImages(int index) {
    _index = 0;
    var imageInfos = controller.state.realTimeConsultationImageList[index];
    var consultationImages = imageInfos.screenshotList ?? [];
    var coloudImags = imageInfos.ultrasoundImageList ?? [];
    var measureImags = imageInfos.measurementImageList ?? [];

    return FColumn(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        FContainer(
          margin: EdgeInsets.only(
            left: _leftMargin,
            top: 15,
          ),
          child: FText(
            DateFormat("yyyy-MM-dd").format(controller
                    .state.realTimeConsultationImageList[index].imageDate ??
                DateTime.now()),
            style: TextStyle(
              fontSize: FFontSizes.H5,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        if (consultationImages.isNotEmpty) ...[
          FContainer(
            margin: EdgeInsets.only(left: _leftMargin, top: FSpacings.M),
            child:
                FText(i18nBook.realTimeConsultation.consultationScreenshot.t),
          ),
          ScreenshotImages(consultationImages),
        ],
        if (coloudImags.isNotEmpty) ...[
          FContainer(
            margin: EdgeInsets.only(left: _leftMargin, top: FSpacings.M),
            child: FText(i18nBook.remedical.cloudImage.t),
          ),
          _buildCloudImages(coloudImags),
        ],
        if (measureImags.isNotEmpty) ...[
          FContainer(
            margin: EdgeInsets.only(left: _leftMargin, top: FSpacings.M),
            child: FText(i18nBook.measure.measureImage.t),
          ),
          _buildMeasureImages(measureImags),
        ],
      ],
    );
  }

  FWidget _buildMeasureImages(List<RemedicalMeasuredInfoDTO> measureImags) {
    return FContainer(
      color: Colors.black,
      margin: EdgeInsets.only(
        left: 20,
        top: 5,
      ),
      child: FWrap(
        spacing: 8.0, // 主轴(水平)方向间距
        runSpacing: 8.0, // 纵轴（垂直）方向间距
        direction: Axis.horizontal,
        alignment: WrapAlignment.start,
        children: List<FWidget>.generate(
          measureImags.length,
          (i) {
            var imageInfo = measureImags[i];
            final image = FStack(
              children: [
                FImage.network(
                  imageInfo.previewFileToken ?? '',
                  width: _imageWidth,
                  height: 160,
                ),
                FPositioned(
                  left: 5,
                  top: 5,
                  child: FText(
                    (_index + 1).toString(),
                    style: TextStyle(color: Colors.white),
                  ),
                ),
                FPositioned(
                  right: 10,
                  top: 5,
                  child: FQuickPreviewPlugin(
                    imageUrl: imageInfo.previewFileToken ?? '',
                    vidUrl: imageInfo.measuredFileToken ?? '',
                    child: FIcon(
                      Icons.zoom_in,
                      color: Colors.white,
                      size: 32,
                    ),
                  ),
                ),
              ],
            );
            _index++;
            return image;
          },
        ),
      ),
    );
  }

  ///缺省显示
  FWidget _mockEmptyView() {
    return FContainer(
      margin: EdgeInsets.only(top: 80),
      child: FColumn(
        children: [
          FCenter(
            child: FImage.asset(
              "assets/images/no_data.png",
              width: 300,
              height: 300,
              fit: BoxFit.cover,
            ),
          ),
          FText(i18nBook.common.noneDataTip.t),
        ],
      ),
    );
  }
}
