import React, { useEffect } 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 FreehandTools from './FreehandTools';
import TextTools from './FreeTextTools';
import StickyNoteTools from './StickyNoteTools';
import ShapeTools from './ShapeTools';

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

const MarkupTools: React.FC = () => {
  const { t } = useTranslation('sidebar');
  const [{ sidebarState, markupToolState }, dispatch] = useStore();
  const { setSidebar, setMarkupTool } = useActions(dispatch);

  const onClickSidebar = (state: string): void => {
    if (state === sidebarState) {
      setSidebar('');
    } else {
      setSidebar(state);
    }
  };

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

  useEffect(() => {
    if (sidebarState !== 'markup-tools') {
      setMarkupTool('');
    }
  }, []);

  return (
    <ExpansionPanel
      label={(
        <Button
          shouldFitContainer
          align="left"
          onClick={(): void => { onClickSidebar('markup-tools'); }}
        >
          <Icon glyph="markup-tools" style={{ marginRight: '10px' }} />
          {t('markupTool')}
        </Button>
      )}
      isActive={sidebarState === 'markup-tools'}
    >
      <HighlightTools
        title={t('annotate')}
        isActive={markupToolState === 'highlight'}
        onClick={(): void => { onClickTool('highlight'); }}
      />
      <FreehandTools
        title={t('freehand')}
        isActive={markupToolState === 'freehand'}
        onClick={(): void => { onClickTool('freehand'); }}
      />
      <TextTools
        title={t('textBox')}
        isActive={markupToolState === 'text'}
        onClick={(): void => { onClickTool('text'); }}
      />
      <StickyNoteTools
        title={t('stickyNote')}
        isActive={markupToolState === 'sticky'}
        onClick={(): void => { onClickTool('sticky'); }}
      />
      <ShapeTools
        title={t('shape')}
        isActive={markupToolState === 'shape'}
        onClick={(): void => { onClickTool('shape'); }}
      />
    </ExpansionPanel>
  );
};

export default MarkupTools;
