import React, { useCallback } from 'react';
import { useHistory } from 'react-router-dom';

export interface Props extends React.ButtonHTMLAttributes<HTMLButtonElement> {
  onClick?: () => void;
  route?: string;
  variant?: 'contained' | 'outlined';
  size?: 'sm' | 'md' | 'lg';
  children?: string;
}

const variants = {
  contained: 'bg-black text-white hover:underline ring-gray-400',
  outlined: 'hover:shadow border border-gray-300 ring-gray-200',
};

const sizes = {
  lg: 'p-5',
  md: 'p-4',
  sm: 'p-3',
};

const style = 'm-2 rounded-sm font-semibold focus:outline-none active:ring-2';

const Button: React.FC<Props> = ({ onClick, route, variant = 'contained', size = 'md', type = 'button', ...props }) => {
  const history = useHistory();
  const navigateRoute = useCallback(() => history.push(route || '/'), [route, history]);

  return (
    <button
      type={type}
      onClick={route ? navigateRoute : onClick}
      className={`${style} ${variants[variant]} ${sizes[size]}`}
      {...props}
    />
  );
};

export default Button;
