import 'package:fis_ui/base_define/page.dart';
import 'package:fis_ui/index.dart';
import 'package:fis_ui/interface/interactive_container.dart';
import 'package:flutter/material.dart';

/// 上传示例类型
enum UploadSampleType {
  /// 身份证正面
  idCardFront,

  /// 身份证背面
  idCardBack,
}

/// 上传状态
enum UploadStatus {
  /// 待选择文件
  waitChoose,

  /// 已选择(上传前)
  ready,

  /// 上传中
  uploading,

  /// 上传成功
  success,

  /// 上传失败
  fail,
}

/// 上传卡片(容器)
class UploadCard extends FStatelessWidget implements FInteractiveContainer {
  final double? cardHeight; //120
  final double? cardWidth; // = 190;
  final bool? isCircleAvatar;

  /// 上传卡片(容器)
  ///
  /// [uploadCardPanel] 子组件
  ///
  /// [width] 宽度
  ///
  /// [height] 高度
  UploadCard({
    Key? key,
    this.uploadCardPanel,
    this.cardWidth,
    this.cardHeight,
    this.isCircleAvatar,
    this.pageName = 'LabeledCheckbox',
  }) : super(key: key);

  @override
  final String pageName;

  ///上传区域内容
  final FWidget? uploadCardPanel;

  factory UploadCard.square({
    Key? key,
    FWidget? child,
    double? cardHeight,
  }) {
    return UploadCard(
      key: key,
      uploadCardPanel: child,
      cardWidth: cardHeight,
      cardHeight: cardHeight,
    );
  }

  @override
  FWidget build(BuildContext context) {
    final isAvatar = isCircleAvatar ?? false;
    return FContainer(
      alignment: isAvatar ? Alignment.bottomCenter : Alignment.center,
      width: cardWidth,
      height: cardHeight,
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(4),
        border: Border.all(
          color: isAvatar ? Colors.transparent : Colors.grey,
          width: 0.5,
        ),
      ),
      child: uploadCardPanel,
    );
  }
}

class ImageUploadSuccessBox extends FStatefulWidget implements FPage {
  const ImageUploadSuccessBox(
    this.url,
    this.isLocal, {
    Key? key,
    required this.onDelete,
  }) : super(key: key);
  @override
  final String pageName = 'ImageUploadSuccessBox';
  final bool isLocal;
  final String url;
  final VoidCallback onDelete;
  @override
  FState<ImageUploadSuccessBox> createState() => _ImageUploadSuccessBoxState();
}

class _ImageUploadSuccessBoxState extends FState<ImageUploadSuccessBox> {
  bool loaded = false;

  @override
  FWidget build(BuildContext context) {
    FImage image;
    if (widget.isLocal) {
      image = FImage.asset(widget.url);
    } else {
      image = FImage.network(widget.url);
    }
    if (!loaded) {
      image.image.resolve(ImageConfiguration()).addListener(
        ImageStreamListener(
          (image, synchronousCall) {
            if (mounted) {
              setState(() {
                loaded = true;
              });
            }
          },
        ),
      );
    }
    final child = FStack(
      children: [
        FRepaintBoundary(
          child: FPadding(
            padding: EdgeInsets.only(left: 2, right: 2, top: 1, bottom: 1),
            child: image,
          ),
        ),
        if (loaded) buildCloseImageView(),
      ],
    );

    return child;
  }

  ///移除图像区域
  FWidget buildCloseImageView() {
    return FPositioned(
      top: 4,
      right: 4,
      child: FMouseRegion(
        cursor: SystemMouseCursors.click,
        child: FGestureDetector(
          businessParent: this.widget,
          name: "delete",
          onTap: widget.onDelete,
          child: FIcon(
            Icons.close_rounded,
            color: Colors.red.withOpacity(.8),
            size: 28,
          ),
        ),
      ),
    );
  }
}

class RowSeparator extends FStatelessWidget {
  final double? thickness;

  RowSeparator({
    this.thickness = 1,
  });

  @override
  FWidget build(BuildContext context) {
    return FDivider(
      thickness: thickness,
      color: Colors.grey[350],
    );
  }
}
