import 'package:flutter/material.dart';
import 'package:fis_common/helpers/color.dart';

enum VButtonType {
  primary,
  error,
  cancel,
  text,
}

class VButton extends StatelessWidget {
  /// 类型
  final VButtonType type;

  /// 文字
  final String? label;

  /// 自定义子组件
  final Widget? child;

  /// 点击事件回调
  final VoidCallback? onTap;

  const VButton({
    super.key,
    this.type = VButtonType.primary,
    this.label,
    this.child,
    this.onTap,
  }) : assert(label != null || child != null);

  @override
  Widget build(BuildContext context) {
    final colors = _findBgColor(context, type);
    final bgColor = colors.bgColor;
    final textColor = colors.textColor;

    return LayoutBuilder(builder: (context, c) {
      double width = c.maxWidth;
      if (width > 1200) {
        width = 270;
      }
      double height = c.maxHeight;
      if (height > 720) {
        height = 60;
      }
      double radius = height / 4;
      List<Color> havaColors = [
        // Color.fromRGBO(59, 188, 255, 1),
        // Color.fromRGBO(44, 120, 229, 1),
        FColorHelper.mixColor(Colors.white, Theme.of(context).primaryColor, 40),
        FColorHelper.mixColor(Colors.black, Theme.of(context).primaryColor, 5),
      ];
      return Container(
        width: 270,
        height: height,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(radius)),
          gradient: LinearGradient(
            begin: Alignment.topRight,
            end: Alignment.bottomLeft,
            colors: havaColors,
          ),
        ),
        child: ElevatedButton(
          onPressed: () {
            onTap?.call();
          },
          style: ButtonStyle(
            foregroundColor: MaterialStatePropertyAll(textColor),
            backgroundColor: const MaterialStatePropertyAll(Colors.transparent),
            elevation: const MaterialStatePropertyAll(0),
            padding: MaterialStatePropertyAll(
              EdgeInsets.symmetric(horizontal: radius, vertical: 8),
            ),
            alignment: Alignment.center,
            shape: MaterialStatePropertyAll(
              RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(radius),
              ),
            ),
          ),
          child: child ??
              Text(
                label!,
                style: TextStyle(
                  color: textColor,
                  fontSize: 18,
                ),
              ),
        ),
      );
    });
  }

  _BgColorTuple _findBgColor(BuildContext context, VButtonType type) {
    switch (type) {
      case VButtonType.primary:
        return _BgColorTuple(Theme.of(context).primaryColor, Colors.white);
      case VButtonType.error:
        return _BgColorTuple(Theme.of(context).colorScheme.error, Colors.white);
      case VButtonType.text:
        return _BgColorTuple(Colors.white, Theme.of(context).primaryColor);
      case VButtonType.cancel:
        return _BgColorTuple(Colors.grey, Colors.white);
    }
  }
}

class _BgColorTuple {
  final Color bgColor;
  final Color textColor;

  _BgColorTuple(this.bgColor, this.textColor);
}
