import React, {
  createRef,
  forwardRef,
  memo,
  ButtonHTMLAttributes,
  ReactElement,
} from 'react';
import styleProps from '@techytools/ui/types/styles';
import Block from '@techytools/ui/components/Block';
import variants from '@techytools/ui/components/Button/styles';
import IconLoading from '@ant-design/icons/LoadingOutlined';
import withCombinedProps from '@techytools/ui/hooks/withCombinedProps';
import withStyles from '@techytools/ui/hooks/withStyles';
import styled from '@emotion/styled';
import MuiButton, { ButtonProps as MuiButtonProps } from '@mui/material/Button';

export type Props = {
  /**
   * Used to set padding/fontSize/height/line-height.
   */
  size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
  /**
   * If true, will have very rounded corners like a "pill" or "circle".
   */
  round?: boolean;
  /**
   * Pass true to use default loading animation. Or pass a custom loading animation component.
   * If button has children, loading animation will play on top of the children.
   * This way, if loading prop is dynamic (after user clicked submit), button size will not change.
   */
  loading?: boolean | ReactElement;
  /**
   * React component to displayed to the left of the text.
   */
  icon?: ReactElement;
  /**
   * React component to displayed to the right of the text.
   */
  suffix?: ReactElement;
  /**
   * Alternative to `props.children`, will overwrite `props.children`.
   */
  value?: ReactElement | string | number;
} & styleProps &
  MuiButtonProps &
  ButtonHTMLAttributes<HTMLElement & HTMLButtonElement>;

/**
 * Button. Pass variant such as "primary", "outlined", "cancel", or "disabled"
 */
// @ts-ignore tsFix: currntly just shoving the props in, but need to reconcile this abstration component with what MUI.com button expects, to make sure it's ok. Right now, the obvious mismatch is props.variant, which is ok. This library takes care of variants. So MUI may get a variant that it does not recognize, but that's ok.
export const Component = withStyles(
  // @ts-ignore tsFix props.variant is ignored. This library takes care of variants and how each variant should look
  forwardRef(({ onClick, loading, variant, ...props }: Props, ref: any) => {
    /*
     * Props logic
     */
    if (loading) {
      if (loading === true) {
        loading = <IconLoading />;
      }
    }
    const Children: any = [];
    // icon on the left
    if (props.icon) {
      Children.push(
        <span key="iconLeft" className="Button--icon">
          {props.icon}
        </span>
      );
    }
    if (props.icon && props.children) {
      Children.push(
        <span key="iconLeftSpacer" className="Button--spacer">
          {' '}
        </span>
      );
    }
    // content
    if (props.value || props.children) {
      Children.push(
        <span key="children" className="Button--text">
          {props.value || props.children}
        </span>
      );
    }
    if (loading) {
      Children.push(
        <span key="loading" className="Button--loading Button--icon">
          {loading}
        </span>
      );
    }
    // icon on the right
    if (props.suffix && props.children) {
      Children.push(
        <span key="iconRightSpacer" className="Button--spacer">
          {' '}
        </span>
      );
    }
    if (props.suffix) {
      Children.push(
        <span key="iconRight" className="Button--icon">
          {props.suffix}
        </span>
      );
    }
    if (!ref) {
      ref = createRef();
    }
    // const ChildrenWithRipple = withRipple({ children: Children });
    return (
      <Styled
        tabIndex={0}
        {...props}
        ref={ref}
        onClick={(e) => {
          if (onClick) {
            onClick(e);
          }
          // remove the blue outline
          // ref?.current?.blur();
        }}
      >
        {props.textgradient ? (
          <Block textgradient={props.textgradient}>{Children}</Block>
        ) : (
          Children
        )}
      </Styled>
    );
  }),
  'Button',
  variants
);

/*
 * (1) default export is normal component ready to use (2) withButton is HOC used to predefine common props
 */

export default memo(Component);

export const withButton = (props: Props) =>
  memo(withCombinedProps(Component, props));

const Styled = styled(MuiButton)``;
