import React, { useState, useContext } from "react"
import PropTypes from "prop-types"
import cx from "classnames"
import IconButton from "./icon-button"
import ButtonGroup from "./button-group"
import CKIcon from "../ck-icon"
import { ButtonGroupContext } from "./button-group"

const SIZES = {
  sm: "text-sm py-1 px-3",
  md: "text-sm py-2 px-4 h-10",
  lg: "text-sm py-3 px-5",
  icon: "p-2 h-10",
}

export const TOKENS = {
  primary: "text-white hover:text-white focus:text-white bg-red-500 hover:bg-red-600",
  secondary: "text-gray-800 bg-white hover:bg-gray-50 border-gray-200",
  tertiary: "text-gray-800 bg-transparent hover:bg-gray-50",
  plain: "",
}

const DISABLED_CLASSES = "text-gray-600 bg-gray-200"

const COLOR_FALLBACK = {
  red: TOKENS["primary"],
  white: TOKENS["secondary"],
  plain: TOKENS["tertiary"],
  gray: TOKENS["secondary"],
  yellow: "text-white bg-yellow-500 hover:bg-yellow-600",
  blue: "text-white bg-blue-500 hover:bg-blue-600",
  green: "text-white bg-green-500 hover:bg-green-600",
  "light-blue": "text-gray-900 bg-blue-200 hover:bg-blue-300",
  "light-green": "text-gray-900 bg-green-200 hover:bg-green-300 focus:bg-green-300",
  "light-yellow": "text-gray-900 bg-yellow-200 hover:bg-yellow-300",
  "light-red": "text-gray-900 bg-red-200 hover:bg-red-300",
}

/**
 * @param {Object} props
 * @param {"primary"|"secondary"|"tertiary"|"plain"} props.is
 * @param {"sm"|"md"|"lg"} props.size
 * @param {string} props.icon
 * @param {boolean} props.disabled
 * @param className any custom classes to be applied to button
 */
const Button = React.forwardRef(
  (
    {
      is,
      color,
      size = "md",
      icon,
      disabled = false,
      className,
      children,
      onMouseDown = () => {},
      onBlur = () => {},
      ...props
    },
    ref
  ) => {
    const groupContext = useContext(ButtonGroupContext)
    const [clicked, setClicked] = useState(false)
    const DEFAULT = `leading-snug border border-solid border-transparent no-underline font-sans font-semibold transition duration-200 hover:no-underline focus:no-underline focus:outline-none align-middle`
    const isDisabled = disabled && (Object.keys(TOKENS).includes(is) || color)
    const usesTokenStyles = Object.keys(TOKENS).includes(is) || isDisabled || color
    const classes = cx(
      className,
      disabled ? "cursor-default" : "cursor-pointer",
      {
        [DEFAULT]: usesTokenStyles,
        [SIZES[size]]: usesTokenStyles,
        [COLOR_FALLBACK[color]]: color && !isDisabled,
        [TOKENS[is]]: is && !isDisabled,
        "rounded": usesTokenStyles,
        [DISABLED_CLASSES]: isDisabled,
        "focus:outline-blue": !clicked,
        "inline-block": props.href,
        "inline-flex items-center justify-center": Boolean(icon) && usesTokenStyles,
        "rounded-none border-none": groupContext.present
      }
    )

    const handleMouseDown = (event) => {
      setClicked(true)
      onMouseDown(event)
    }

    const handleBlur = (event) => {
      setClicked(false)
      onBlur(event)
    }

    const Component = props.href ? "a" : "button"

    return (
      <Component
        {...props}
        ref={ref}
        className={classes}
        disabled={disabled}
        onMouseDown={handleMouseDown}
        onBlur={handleBlur}
      >
        {icon && (
          <CKIcon name={icon} className="mr-2 pointer-events-none" />
        )}
        {children}
      </Component>
    )
  }
)

Button.propTypes = {
  className: PropTypes.string,
  children: PropTypes.node,
  is: PropTypes.oneOf([
    "primary", // main color
    "secondary", // white
    "tertiary", // transparent with bg on hover
    "plain", // sets defaults without color for easy customization
  ]),
  /** Temporary fallback for "is" prop */
  color: PropTypes.oneOf([
    "red",
    "white",
    "plain",
  ]),
  disabled: PropTypes.bool,
  size: PropTypes.oneOf(Object.keys(SIZES)),
  icon: PropTypes.elementType,
  href: PropTypes.string,
  active: PropTypes.bool,
  onMouseDown: PropTypes.func,
  onBlur: PropTypes.func,
}

export { ButtonGroup, IconButton }

export default Button
