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

class DiagonalPainter extends CustomPainter {
  final Color color;
  DiagonalPainter(this.color);
  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint()
      ..color = color
      ..style = PaintingStyle.fill
      ..strokeWidth = 2.0;

    final path = Path()
      ..moveTo(0, 0)
      ..lineTo(64, 64)
      ..lineTo(64, 8)
      ..quadraticBezierTo(64, 0, 56, 0)
      ..lineTo(0, 0)
      ..close();

    canvas.drawPath(path, paint);
  }

  @override
  bool shouldRepaint(covariant DiagonalPainter oldDelegate) {
    ///此处可以设置颜色动态值
    return oldDelegate.color != color;
  }
}

class FDiagonalContainer extends FStatefulWidget
    implements FInteractiveContainer {
  final VoidCallback onPressed;

  final Color hoverColor;

  final Color defaultColor;
  final double? width;
  final double? height;
  final IconData? defaultIcon;
  final IconData? switchIcon;
  final double? iconSize;
  FDiagonalContainer(
    this.onPressed,
    this.defaultColor,
    this.hoverColor, {
    this.width = 64,
    this.height = 64,
    this.iconSize = 42,
    this.defaultIcon = Icons.qr_code,
    this.switchIcon = Icons.desktop_windows_rounded,
  });

  @override
  String get pageName => "FDiagonalContainer";

  @override
  FState<FStatefulWidget> createState() {
    return _FDiagonalContainerState();
  }
}

class _FDiagonalContainerState extends FState<FDiagonalContainer> {
  late Color _color;
  late IconData _iconData;
  bool _isSwitchIcon = false;

  @override
  FWidget build(BuildContext context) {
    return _buildQRCodeButtonContent();
  }

  void initState() {
    super.initState();
    _color = widget.defaultColor;
    _iconData = widget.defaultIcon!;
  }

  FWidget _buildQRCodeButtonContent() {
    return FContainer(
      width: widget.width,
      height: widget.height,
      child: FMouseRegion(
        cursor: SystemMouseCursors.click,
        onEnter: (_) => _changeColor(true),
        onExit: (_) => _changeColor(false),
        child: FGestureDetector(
          businessParent: widget,
          name: 'QRCodeButton',
          onTap: () {
            _changeIconData();
            widget.onPressed.call();
          },
          child: FAnimatedContainer(
            duration: const Duration(milliseconds: 200),
            child: FCustomPaint(
              painter: DiagonalPainter(_color),
              child: FAlign(
                alignment: Alignment.topRight,
                child: FIcon(
                  _iconData,
                  size: widget.iconSize,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }

  void _changeColor(bool isHover) {
    setState(() {
      _color = isHover ? widget.hoverColor : widget.defaultColor;
    });
  }

  void _changeIconData() {
    _isSwitchIcon = !_isSwitchIcon;
    setState(() {
      _iconData = _isSwitchIcon ? widget.switchIcon! : widget.defaultIcon!;
    });
  }
}
