import React from 'react';
import { Badge, Panel, Tab, Tabs } from 'react-bootstrap';
import TECHNIQUES from '../../config/techniques/techniques';
import { FILE_COUNT } from '../../constants/parameterTypes';
import { getDatasetParameterByPrefixName } from '../../helpers';
import DatasetFileTree from '../File/DatasetFileTree';
import InstrumentWidget from '../Instrument/InstrumentWidget';
import DatasetFooter from './DatasetFooter';
import DatasetMetadataTab from './DatasetMetadataTab';
import DatasetSummary from './DatasetSummary';
import DatasetTechniqueTab from './DatasetTechniqueTab';
import PublicationSummary from './PublicationSummary';

class DatasetWidget extends React.Component {
  getDefinition(dataset) {
    return dataset.parameters.filter(
      (parameter) => parameter.name === 'definition'
    );
  }

  render() {
    const dataset = this.props.dataset;
    const definition = this.getDefinition(dataset);

    function getFilesTabTitle(dataset) {
      const fileCount = dataset.parameters.find((o) => o.name === FILE_COUNT);
      if (fileCount && fileCount.value) {
        return (
          <div>
            Files{' '}
            <Badge style={{ backgroundColor: '#bfbfbf' }}>
              {fileCount.value}
            </Badge>
          </div>
        );
      }
      return 'Files';
    }

    /** Case of publication */

    if (definition) {
      if (definition.length > 0) {
        if (definition[0].value === 'Publication') {
          return (
            <PublicationSummary
              dataset={dataset}
              sessionId={this.props.sessionId}
            />
          );
        }
      }
    }

    return (
      <Panel>
        <Panel.Body>
          <Tabs id="tabs">
            <Tab eventKey={1} title="Summary">
              <DatasetSummary
                dataset={dataset}
                sessionId={this.props.sessionId}
              />
            </Tab>
            {TECHNIQUES.map((technique, i) => {
              const params = dataset.parameters.filter((parameter) =>
                parameter.name.startsWith(technique.shortname)
              );
              if (params.length > 0) {
                return (
                  <Tab
                    key={technique.shortname}
                    eventKey={i + 2}
                    title={technique.name}
                  >
                    <DatasetTechniqueTab dataset={dataset} params={params} />
                  </Tab>
                );
              }
              return null;
            })}
            {!getDatasetParameterByPrefixName(this.props.dataset, 'Instrument')
              .length === 0 && (
              <Tab eventKey={9} title="Instrument">
                <InstrumentWidget dataset={dataset} />
              </Tab>
            )}

            <Tab eventKey={11} title={getFilesTabTitle(dataset)} mountOnEnter>
              <DatasetFileTree
                dataset={dataset}
                sessionId={this.props.sessionId}
              >
                {' '}
                &gt;
              </DatasetFileTree>
            </Tab>
            <Tab eventKey={10} title="Metadata List" className="pull-right">
              <DatasetMetadataTab dataset={dataset} />
            </Tab>
          </Tabs>
        </Panel.Body>
        <Panel.Footer>
          <DatasetFooter
            location={this.props.dataset.location}
            sessionId={this.props.sessionId}
            dataset={this.props.dataset}
          />
        </Panel.Footer>
      </Panel>
    );
  }
}

export default DatasetWidget;
