import 'package:fis_jsonrpc/rpc.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/router/desktop/names.dart';
import 'package:flyinsono/architecture/router/router.dart';
import 'package:flyinsono/pages/remedical/report/report_edit/index.dart';
import 'package:flyinsono/pages/remedical/translation_delegate_helper.dart';
import 'package:fis_measure/process/workspace/rpc_bridge.dart';

class ImageCorverMobile extends FStatefulWidget implements FPage {
  final bool isSelectImageMode;
  final TerminalImage image;
  final int index;
  final String remedicalCode;
  final RemedicalFileDataTypeEnum fileDataType;
  final RemedicalInfoDTO remedicalInfo;
  final String? application;
  final bool? isSelected;
  final bool isCanCreateReport;
  final void Function(String)? onImageTap;
  final String? patientCode;
  final int totalCount;
  ImageCorverMobile(
    this.isSelectImageMode,
    this.image,
    this.index,
    this.remedicalCode,
    this.fileDataType,
    this.remedicalInfo,
    this.application,
    this.isSelected, {
    this.pageName = 'ImageCorverMobile',
    this.onImageTap,
    this.isCanCreateReport = false,
    this.patientCode,
    this.totalCount = 0,
  });

  @override
  final String pageName;
  @override
  FState<FStatefulWidget> createState() {
    return _ImageCorverState();
  }
}

class _ImageCorverState extends FState<ImageCorverMobile> {
  bool _isSelected = false;
  late String _terminalImage;
  @override
  void initState() {
    _terminalImage = widget.image.coverImageUrl ?? '';
    _isSelected = widget.isSelected ?? false;
    super.initState();
  }

  @override
  FWidget build(BuildContext context) {
    final imageUrlMap = widget.image.imageUrl ?? '';

    return FGestureDetector(
      businessParent: widget,
      name: "selectImage",
      onTap: () {
        if (widget.isSelectImageMode) {
          _isSelected = !_isSelected;
          widget.onImageTap?.call(_terminalImage);
        } else {
          routerToVidPlayer(
            widget.index,
            widget.remedicalCode,
          );
        }
      },
      child: FStack(
        fit: StackFit.expand,
        children: [
          FContainer(
            color: Colors.black,
            child: FCenter(
              child: FFittedBox(
                fit: BoxFit.scaleDown,
                child: imageUrlMap.isEmpty
                    ? FSizedBox()
                    : FContentImage(
                        remedicalInfo: widget.remedicalInfo,
                        onDoubleTap: () => routerToVidPlayer(
                          widget.index,
                          widget.remedicalCode,
                        ),
                        onTap: () => routerToVidPlayer(
                          widget.index,
                          widget.remedicalCode,
                        ),
                        ifShowIndex: false,
                        locationDescription: translation_delegate_helper
                            .translateLocationDescription(
                          widget.remedicalInfo.imageLocation?.quadrant,
                          widget.remedicalInfo.imageLocation?.position,
                        ),
                        description:
                            translation_delegate_helper.translateDescription(
                                widget.remedicalInfo.application,
                                widget.remedicalInfo.applicationCategory),
                      ),
              ),
            ),
          ),
          if (widget.isSelectImageMode) ...[
            FPositioned(
              right: 0,
              child: FCheckbox<bool>(
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                  fillColor: MaterialStateProperty.all(Colors.white),
                  checkColor: FTheme.ins.colorScheme.primary,
                  value: _isSelected,
                  isCheck: _isSelected,
                  visualDensity: VisualDensity(horizontal: -4, vertical: -4),
                  onChange: _onChange),
            ),
          ]
        ],
      ),
    );
  }

  void _onChange(dynamic value, bool? isCheck) {
    widget.onImageTap?.call(_terminalImage);
    _isSelected = isCheck ?? false;
    //setState(() {});
  }

  void routerToVidPlayer(int index, String remedicalCode) {
    RPCBridge.ins.source = VidImageSource.Remedical;
    router.to(RouteNames.Remedical.Measure, parameters: {
      "patientCode": widget.patientCode ?? '',
      "remedicalCode": remedicalCode,
      "recordCode": widget.remedicalInfo.recordCode ?? '',
      "isCanWriteReport": widget.isCanCreateReport.toString(),
    });
  }
}
