/*
 * @Author: XianKaiQun
 * @Date: 2020-09-28 11:13:31
 * @LastEditors  : WuWei
 * @LastEditTime : 2023-05-18 15:42:12
 * @Description: 
 */

import 'package:flutter/material.dart';

import 'loading.dart';

class WButton extends StatefulWidget {
  WButton({
    Key? key,
    required this.onPressed,
    this.onLongPress,
    this.onHighlightChanged,
    this.mouseCursor,
    this.textTheme,
    this.textColor,
    this.disabledTextColor,
    this.color,
    this.disabledColor,
    this.focusColor,
    this.hoverColor,
    this.highlightColor,
    this.splashColor,
    this.colorBrightness,
    this.elevation = 0,
    this.focusElevation = 0,
    this.hoverElevation = 0,
    this.highlightElevation = 0,
    this.disabledElevation = 0,
    this.padding,
    this.margin,
    this.visualDensity,
    this.shape,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus = false,
    this.materialTapTargetSize,
    this.animationDuration,
    this.minWidth,
    this.height,
    this.enableFeedback = true,
    this.child,
    this.focusOnGesture = true,
    this.boxShadow,
  }) : super(key: key);

  final VoidCallback onPressed;
  final VoidCallback? onLongPress;
  final ValueChanged<bool>? onHighlightChanged;
  final boxShadow;

  ///手势获取焦点
  final bool? focusOnGesture;
  final MouseCursor? mouseCursor;
  final ButtonTextTheme? textTheme;
  final Color? textColor;
  final Color? disabledTextColor;
  final Color? color;
  final Color? disabledColor;
  final Color? focusColor;
  final Color? hoverColor;
  final Color? highlightColor;
  final Color? splashColor;
  final Brightness? colorBrightness;
  final double elevation;
  final double focusElevation;
  final double hoverElevation;
  final double highlightElevation;
  final double disabledElevation;
  final EdgeInsetsGeometry? padding;
  final EdgeInsetsGeometry? margin;
  final VisualDensity? visualDensity;
  final ShapeBorder? shape;
  final Clip clipBehavior;
  final FocusNode? focusNode;
  final bool autofocus;
  final MaterialTapTargetSize? materialTapTargetSize;
  final Duration? animationDuration;
  final double? minWidth;
  final double? height;
  final bool enableFeedback;
  final Widget? child;

  @override
  _WButtonState createState() => _WButtonState();
}

class _WButtonState extends State<WButton> {
  FocusNode? focusNode;

  @override
  void initState() {
    focusNode = widget.focusNode ?? FocusNode();

    super.initState();
  }

  @override
  void dispose() {
    focusNode?.dispose();
    super.dispose();
  }

  VoidCallback get onPressed {
    return () {
      widget.onPressed();
      if (widget.focusOnGesture == true) {
        focusNode!.requestFocus();
      }
    };
  }

  VoidCallback? get onLongPress {
    if (widget.onLongPress != null) {
      return () {
        widget.onLongPress!();
        if (widget.focusOnGesture == true) {
          focusNode!.requestFocus();
        }
      };
    } else {
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget current = MaterialButton(
      onPressed: onPressed,
      onLongPress: onLongPress,
      onHighlightChanged: widget.onHighlightChanged,
      mouseCursor: widget.mouseCursor,
      textTheme: widget.textTheme,
      textColor: widget.textColor,
      disabledTextColor: widget.disabledTextColor,
      color: widget.color ?? Theme.of(context).primaryColor,
      disabledColor: widget.disabledColor ?? Theme.of(context).disabledColor,
      focusColor: widget.focusColor,
      hoverColor: widget.hoverColor,
      highlightColor: widget.highlightColor,
      splashColor: widget.splashColor,
      colorBrightness: widget.colorBrightness,
      elevation: widget.elevation,
      focusElevation: widget.focusElevation,
      hoverElevation: widget.hoverElevation,
      highlightElevation: widget.highlightElevation,
      disabledElevation: widget.disabledElevation,
      padding: widget.padding,
      visualDensity: widget.visualDensity,
      shape: widget.shape,
      clipBehavior: widget.clipBehavior,
      focusNode: focusNode,
      autofocus: widget.autofocus,
      materialTapTargetSize: widget.materialTapTargetSize,
      animationDuration: widget.animationDuration,
      minWidth: widget.minWidth,
      height: widget.height,
      enableFeedback: widget.enableFeedback,
      child: widget.child,
    );
    if (widget.margin != null && widget.margin != EdgeInsets.zero) {
      current = Padding(
        padding: widget.margin!,
        child: current,
      );
    }
    // if (widget.boxShadow) {

    // }
    return current;
  }
}

///传入future  手势触发展示loading图标
class WLoadingButton extends WLoadingWrapper {
  WLoadingButton({
    Key? key,
    Future<void> Function()? onPressed,
    ValueChanged<bool>? onHighlightChanged,

    ///手势获取焦点
    bool? focusOnGesture,
    MouseCursor? mouseCursor,
    ButtonTextTheme? textTheme,
    Color? textColor,
    Color? disabledTextColor,
    Color? color,
    Color? disabledColor,
    Color? focusColor,
    Color? hoverColor,
    Color? highlightColor,
    Color? splashColor,
    Brightness? colorBrightness,
    double elevation = 0,
    double focusElevation = 0,
    double hoverElevation = 0,
    double highlightElevation = 0,
    double disabledElevation = 0,
    EdgeInsetsGeometry? padding,
    EdgeInsetsGeometry? margin,
    VisualDensity? visualDensity,
    ShapeBorder? shape,
    Clip clipBehavior = Clip.none,
    FocusNode? focusNode,
    bool autofocus = false,
    MaterialTapTargetSize? materialTapTargetSize,
    Duration? animationDuration,
    double? minWidth,
    double? height,
    bool enableFeedback = true,
    Widget? child,
    bool remain = false,
    double? loadingSize,
    double? loadingStrokeWidth,
    Widget? loadingText,
  }) : super(
          key: key,
          remain: remain,
          action: onPressed,
          color: color,
          strokeWidth: loadingStrokeWidth,
          text: loadingText,
          size: loadingSize,
          child: child,
          builder: (context, action, child) {
            return WButton(
              onPressed: action ?? () => {},
              onHighlightChanged: onHighlightChanged,
              mouseCursor: mouseCursor,
              textTheme: textTheme,
              textColor: textColor,
              disabledTextColor: disabledTextColor,
              color: color,
              disabledColor: disabledColor,
              focusColor: focusColor,
              hoverColor: hoverColor,
              highlightColor: highlightColor,
              splashColor: splashColor,
              colorBrightness: colorBrightness,
              elevation: elevation,
              focusElevation: focusElevation,
              hoverElevation: hoverElevation,
              highlightElevation: highlightElevation,
              disabledElevation: disabledElevation,
              padding: padding,
              margin: margin,
              visualDensity: visualDensity,
              shape: shape,
              clipBehavior: clipBehavior,
              focusNode: focusNode,
              autofocus: autofocus,
              materialTapTargetSize: materialTapTargetSize,
              animationDuration: animationDuration,
              minWidth: minWidth,
              height: height,
              enableFeedback: enableFeedback,
              focusOnGesture: focusOnGesture,
              child: child,
            );
          },
        );
}
