import React from 'react';
import { useTranslation } from 'react-i18next';

import Button from '../components/Button';
import ExpansionPanel from '../components/ExpansionPanel';
import Icon from '../components/Icon';
import HighlightTools from './HighlightTools';
import FreehandTool from './FreehandTool';
import TextTool from './FreeTextTool';
import StickyNoteTool from './StickyNoteTool';
import ShapeTool from './ShapeTool';

import useActions from '../actions';
import useStore from '../store';

type Props = {
  sidebarState: string;
  onClickSidebar: (state: string) => void;
};

const MarkupTools: React.FC<Props> = ({
  sidebarState,
  onClickSidebar,
}: Props) => {
  const { t } = useTranslation('sidebar');
  const [{ toolState }, dispatch] = useStore();
  const { setTool } = useActions(dispatch);

  const onClickTool = (state: string): void => {
    if (state === toolState) {
      setTool('');
    } else {
      setTool(state);
    }
  };

  const isActive = sidebarState === 'markup-tools';

  const Label = (
    <Button
      shouldFitContainer
      align="left"
      onClick={(): void => {
        onClickSidebar('markup-tools');
      }}
    >
      <Icon glyph="markup-tools" style={{ marginRight: '10px' }} />
      {t('markupTool')}
      <Icon
        glyph="dropdown-arrow"
        style={{
          transform: isActive ? 'rotate(180deg)' : 'rotate(0deg)',
          marginLeft: '10px',
        }}
      />
    </Button>
  );

  return (
    <ExpansionPanel label={Label} isActive={isActive}>
      <HighlightTools
        title={t('annotate')}
        isActive={toolState === 'highlight'}
        onClick={(): void => {
          onClickTool('highlight');
        }}
      />
      <FreehandTool
        title={t('freehand')}
        isActive={toolState === 'freehand'}
        onClick={(): void => {
          onClickTool('freehand');
        }}
      />
      <TextTool
        title={t('textBox')}
        isActive={toolState === 'text'}
        onClick={(): void => {
          onClickTool('text');
        }}
      />
      <StickyNoteTool
        title={t('stickyNote')}
        isActive={toolState === 'sticky'}
        onClick={(): void => {
          onClickTool('sticky');
        }}
      />
      <ShapeTool
        title={t('shape')}
        isActive={toolState === 'shape'}
        onClick={(): void => {
          onClickTool('shape');
        }}
      />
    </ExpansionPanel>
  );
};

export default MarkupTools;
