/*
 * @Author: XianKaiQun
 * @Date: 2020-06-21 20:54:48
 * @LastEditors  : WuWei
 * @LastEditTime : 2022-01-19 16:14:12
 * @Description: 
 */
import 'package:flutter/material.dart';

import 'package:wisdom_cli/wisdom_cli.dart';

// 按钮形状
enum WisButtonShape {
  ///正常的（稍微有点圆弧）
  normal,

  ///椭圆的
  round,

  ///方形
  square,
}

class WisButton extends StatelessWidget {
  const WisButton({
    Key? key,
    this.onPressed,
    this.onLongPress,
    this.onHighlightChanged,
    this.textStyle,
    this.textColor,
    this.color,
    this.focusColor,
    this.hoverColor,
    this.highlightColor,
    this.splashColor,
    this.plain = false,
    this.buttonShape = WisButtonShape.normal,
    this.elevation = 0,
    this.focusElevation = 0,
    this.hoverElevation = 0,
    this.highlightElevation = 0,
    this.disabledElevation = 0,
    this.margin = EdgeInsets.zero,
    this.padding = EdgeInsets.zero,
    this.visualDensity = const VisualDensity(),
    this.constraints = const BoxConstraints(minWidth: 64.0, minHeight: 29.0),
    this.animationDuration = kThemeChangeDuration,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
    this.materialTapTargetSize,
    this.child,
    this.enableFeedback = true,
  }) : super(key: key);

  final VoidCallback? onPressed;

  final VoidCallback? onLongPress;

  final ValueChanged<bool>? onHighlightChanged;

  ///朴素按钮
  final bool plain;

  ///按钮形状
  final WisButtonShape buttonShape;

  final TextStyle? textStyle;

  final Color? textColor;

  final Color? color;

  final Color? focusColor;

  final Color? hoverColor;

  final Color? highlightColor;

  final Color? splashColor;

  final double elevation;

  final double hoverElevation;

  final double focusElevation;

  final double highlightElevation;

  final double disabledElevation;

  final EdgeInsetsGeometry margin;

  final EdgeInsetsGeometry padding;

  final VisualDensity visualDensity;

  final BoxConstraints constraints;

  final Duration animationDuration;

  final Widget? child;

  final MaterialTapTargetSize? materialTapTargetSize;

  final FocusNode? focusNode;

  final bool autofocus;

  final Clip clipBehavior;

  final bool enableFeedback;

  static Widget loading({
    Key? key,
    Future<void> Function()? onPressed,
    bool plain = false,
    WisButtonShape buttonShape = WisButtonShape.normal,

    ///加载的时候是否显示loading  true不显示
    bool remain = false,
    TextStyle? textStyle,
    Color? textColor,
    Color? color,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    double elevation = 0,
    double hoverElevation = 0,
    double focusElevation = 0,
    double highlightElevation = 0,
    double disabledElevation = 0,
    EdgeInsetsGeometry margin = EdgeInsets.zero,
    EdgeInsetsGeometry padding = EdgeInsets.zero,
    VisualDensity visualDensity = const VisualDensity(),
    BoxConstraints constraints =
        const BoxConstraints(minWidth: 64.0, minHeight: 29.0),
    Duration animationDuration = kThemeChangeDuration,
    MaterialTapTargetSize? materialTapTargetSize,
    FocusNode? focusNode,
    bool autofocus = false,
    Clip clipBehavior = Clip.none,
    bool enableFeedback = true,
    Color? loadingColor,
    TextStyle? loadingTextStyle,
    double loadingSize = 20.0,
    double loadingstrokeWidth = 2.0,
    Widget? text,
    Widget? child,
  }) {
    //有背景颜色就默认白色
    Color? _loadingColor =
        color != null && loadingColor == null && !plain ? Colors.white : null;
    return WLoadingWrapper(
      action: onPressed,
      remain: remain,
      color: _loadingColor,
      textStyle: loadingTextStyle,
      size: loadingSize,
      strokeWidth: loadingstrokeWidth,
      text: text,
      child: child,
      builder: (BuildContext context, Future<void> Function()? action,
          Widget? child) {
        return WisButton(
          key: key,
          onPressed: action,
          plain: plain,
          buttonShape: buttonShape,
          textStyle: textStyle,
          textColor: textColor,
          color: color,
          focusColor: focusColor,
          hoverColor: hoverColor,
          highlightColor: highlightColor,
          splashColor: splashColor,
          elevation: elevation,
          hoverElevation: hoverElevation,
          focusElevation: focusElevation,
          highlightElevation: highlightElevation,
          disabledElevation: disabledElevation,
          margin: margin,
          padding: padding,
          visualDensity: visualDensity,
          constraints: constraints,
          animationDuration: animationDuration,
          child: child,
          materialTapTargetSize: materialTapTargetSize,
          focusNode: focusNode,
          autofocus: autofocus,
          clipBehavior: clipBehavior,
          enableFeedback: enableFeedback,
        );
      },
    );
  }

  bool get enabled => onPressed != null || onLongPress != null;

  @override
  Widget build(BuildContext context) {
    Color? _color = enabled
        ? (color != null ? color : Theme.of(context).primaryColor)
        : (color != null ? color : Theme.of(context).primaryColor)
            ?.withOpacity(0.6);

    TextStyle _textStyle = TextStyle(
            color: (textColor == null && plain) ? _color : textColor,
            fontSize: 14)
        .merge(textStyle);

    ShapeBorder _shape() {
      BorderRadius borderRadius;
      switch (buttonShape) {
        case WisButtonShape.round:
          borderRadius = BorderRadius.circular(10000);
          break;
        case WisButtonShape.square:
          borderRadius = BorderRadius.zero;
          break;
        default:
          borderRadius = BorderRadius.circular(12.pt);
      }
      return RoundedRectangleBorder(
        side: BorderSide(
            color: plain ? _color! : Colors.transparent, width: 1.pt),
        borderRadius: borderRadius,
      );
    }

    Widget current = RawMaterialButton(
      onPressed: onPressed,
      onLongPress: onLongPress,
      onHighlightChanged: onHighlightChanged,
      textStyle: _textStyle,
      fillColor: plain ? null : _color,
      focusColor: focusColor,
      hoverColor: hoverColor,
      highlightColor: highlightColor,
      splashColor: splashColor,
      elevation: elevation,
      focusElevation: focusElevation,
      hoverElevation: hoverElevation,
      highlightElevation: highlightElevation,
      disabledElevation: disabledElevation,
      padding: padding,
      visualDensity: visualDensity,
      constraints: constraints,
      shape: _shape(),
      animationDuration: animationDuration,
      clipBehavior: clipBehavior,
      focusNode: focusNode,
      autofocus: autofocus,
      materialTapTargetSize: materialTapTargetSize,
      child: child,
      enableFeedback: enableFeedback,
    );
    current = Padding(
      padding: margin,
      child: current,
    );
    return current;
  }
}
