import React from 'react';
import styled from 'styled-components';

import v from '../../styles/Variables';

interface Props {
  /**
   * The hex background color of the button.
   */
  backgroundColor?: string;
  /**
   * A valid JSX set of elements.
   */
  children: React.ReactNode;
  /**
   * The callback when the button is clicked.
   */
  clickCb?: () => void;
  /**
   * TThe callback function when the mouse enters the button.
   */
  enterCb?: () => void;
  /**
   * Toggle to show the content inside the button.
   */
  fabOpen: boolean;
  /**
   * The callback function when the mouse leaves the button.
   */
  leaveCb?: () => void;
  /**
   * The color of the text of the button.
   */
  textColor?: string;
  /**
   * The classes applied to the button itself.
   */
  theme?: string;
  /**
   * The text shown next to the main button.
   */
  title?: string;
}

interface FabStyles {
  backgroundColor: string;
  textColor: string;
}

const Fab = ({ backgroundColor = v.colors.dark, children, clickCb, enterCb, fabOpen, leaveCb, textColor = v.colors.light, theme = '', title = '+' }: Props) => {
  return (
    <Wrapper
      data-test="component-fab"
      onMouseEnter={() => (enterCb ? enterCb() : null)}
      onMouseLeave={() => (leaveCb ? leaveCb() : null)}
      onClick={() => (clickCb ? clickCb() : null)}
      className={`nwc--fab-wrapper nwc--docked ${theme}`}
    >
      <span data-test="component-fab-open" className={fabOpen === true ? 'nwc--fab-mask' : 'nwc--fab-mask opacity--0'} />

      <FabMenu data-test="component-fab-wrapper" className={fabOpen === true ? 'nwc--fab-menu' : 'nwc--fab-menu opacity--0'}>
        <span className="right">{children}</span>
      </FabMenu>

      <FloatinActionButton data-test="component-fab-button" type="button" className="nwc--fab padding--none border--none" backgroundColor={backgroundColor} textColor={textColor}>
        <div>{title}</div>
      </FloatinActionButton>
    </Wrapper>
  );
};

export default Fab;

/* styles */
const Wrapper = styled.span`
  display: inline-block;
  position: relative;
  text-align: right;
  right: 25px;

  &.nwc--docked {
    position: fixed;
    bottom: 25px;
  }
`;

const FloatinActionButton = styled.button<FabStyles>`
  display: inline-block;
  min-width: 56px;
  height: 56px;
  text-align: center;
  line-height: 56px;
  padding: 0 20px;
  border-radius: 300px;
  transition: all 0.4s linear;
  box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12);
  background: ${({ backgroundColor }) => backgroundColor};
  color: ${({ textColor }) => textColor};
  z-index: 999;
  position: relative;
  font-size: 24px;

  &:hover {
    cursor: pointer;
    opacity: 0.7;
    transform: rotate(45deg);
    box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.1), 0px 6px 7px 0px rgba(0, 0, 0, 0.1), 0px 1px 10px 0px rgba(0, 0, 0, 0.1);
  }
`;

const FabMenu = styled.div`
  position: absolute;
  bottom: 65px;
  right: 10px;
  z-index: 99;
  transition: all 0.2s linear;
  opacity: 1;
  text-align: right;

  .nwc--fab-image:hover,
  .nwc--fab-item:hover {
    cursor: pointer;
    opacity: 0.7;
  }

  .nwc--fab-item {
    display: inline-block;
    padding: 0 10px;
    min-width: 40px;
    height: 40px;
    background: #fff;
    box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12);
    border-radius: 300px;
    text-align: center;
    line-height: 40px;
    margin-bottom: 15px;
    white-space: nowrap;
    transition: all 0.2s linear;
  }

  .nwc--fab-image {
    img {
      display: inline-block;
      padding: 0;
      width: 40px;
      height: 40px;
      background: #fff;
      box-shadow: 0px 3px 5px -1px rgba(0, 0, 0, 0.2), 0px 6px 10px 0px rgba(0, 0, 0, 0.14), 0px 1px 18px 0px rgba(0, 0, 0, 0.12);
      border-radius: 300px;
      margin-bottom: 15px;
      overflow: hidden;
      transition: all 0.2s linear;
    }

    span {
      position: absolute;
      right: 55px;
      padding-top: 5px;
      color: #fff;
    }
  }

  .nwc--fab-mask {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    transition: background 0.4s linear;

    &.opacity--0 {
      height: 0;
    }
  }
`;
