import React, { useState } from 'react';

import { useTranslation } from 'next-i18next';
import {
  Button,
  Collapse,
} from 'reactstrap';

import MarkdownTable from '~/client/models/MarkdownTable';

type MarkdownTableDataImportFormProps = {
  onCancel: () => void,
  onImport: (table: MarkdownTable) => void,
}

export const MarkdownTableDataImportForm = (props: MarkdownTableDataImportFormProps): JSX.Element => {

  const { onCancel, onImport } = props;

  const { t } = useTranslation('commons', { keyPrefix: 'handsontable_modal.data_import_form' });

  const [dataFormat, setDataFormat] = useState<string>('csv');
  const [data, setData] = useState<string>('');
  const [parserErrorMessage, setParserErrorMessage] = useState(null);

  const convertFormDataToMarkdownTable = () => {
    let result;
    switch (dataFormat) {
      case 'csv':
        result = MarkdownTable.fromDSV(data, ',');
        break;
      case 'tsv':
        result = MarkdownTable.fromDSV(data, '\t');
        break;
      case 'html':
        result = MarkdownTable.fromHTMLTableTag(data);
        break;
    }
    return result.normalizeCells();
  };

  const importButtonHandler = () => {
    try {
      const markdownTable = convertFormDataToMarkdownTable();
      onImport(markdownTable);
      setParserErrorMessage(null);
    }
    catch (e) {
      setParserErrorMessage(e.message);
    }
  };

  return (
    <form className="data-import-form">
      <div className="form-group">
        <label htmlFor="data-import-form-type-select">{t('select_data_format')}</label>
        <select
          id="data-import-form-type-select"
          className="form-control"
          placeholder="select"
          value={dataFormat}
          onChange={(e) => { return setDataFormat(e.target.value) }}
        >
          <option value="csv">CSV</option>
          <option value="tsv">TSV</option>
          <option value="html">HTML</option>
        </select>
      </div>
      <div className="form-group">
        <label htmlFor="data-import-form-type-textarea">{t('import_data')}</label>
        <textarea
          id="data-import-form-type-textarea"
          className="form-control"
          placeholder={t('paste_table_data')}
          rows={8}
          onChange={(e) => { return setData(e.target.value) }}
        />
      </div>
      <Collapse isOpen={parserErrorMessage != null}>
        <div className="form-group">
          <label htmlFor="data-import-form-type-textarea-alert">{t('parse_error')}</label>
          <textarea
            id="data-import-form-type-textarea-alert"
            className="form-control"
            rows={4}
            value={parserErrorMessage || ''}
            readOnly
          />
        </div>
      </Collapse>
      <div className="d-flex justify-content-end">
        <Button color="secondary mr-2" onClick={onCancel}>{t('cancel')}</Button>
        <Button color="primary" onClick={importButtonHandler}>{t('import')}</Button>
      </div>
    </form>
  );

};
