import { Component, ReactNode } from 'react';
import classNames from 'classnames';
import { withoutWeightColor } from './utils';

type FlatButtonProps = {
  color?: string;
  colorWeight?: number;
  textColor?: string;
  textColorWeight?: number;
  borderWeight?: number;
  type?: 'button' | 'submit' | 'reset';
  onClick?: () => void;
  children?: ReactNode;
  disabled?: boolean;
  className?: string;
};

class FlatButton extends Component<FlatButtonProps> {
  getColorClass = () => {
    const {
      color = 'blue',
      colorWeight = 500,
      textColor,
      textColorWeight,
      disabled = false
    } = this.props;

    return classNames(
      {
        [textColorWeight ? `text-${textColor}-${textColorWeight}` : `text-${textColor}`]: textColor
      },
      { [`text-${color}-${colorWeight}`]: !textColor },
      { 'opacity-40 cursor-not-allowed': disabled },
      { [`hover:bg-${color}-100`]: !withoutWeightColor.includes(color) && !disabled }
    );
  };

  render() {
    const { type = 'button', disabled = false } = this.props;

    return (
      <button
        type={type}
        disabled={disabled}
        onClick={this.props.onClick}
        className={classNames(
          'focus:outline-none',
          this.getColorClass(),
          { 'hover:font-semibold hover:shadow-lg': !this.props.disabled },
          this.props.className
        )}
      >
        {this.props.children}
      </button>
    );
  }
}

export default FlatButton;
