import React from 'react'
import { addIndex, isNil, map, omit } from 'ramda'
import cx from 'classnames'
import PropTypes from 'prop-types'
import MTTableRowMenu from './mt-table-row-menu'
import MTIcon from './mt-icon'

const mapIndexed = addIndex(map)

function MTTable (props) {
  const {
    headers,
    rows,
    onSort,
    sortColumn,
    isAscending,
    footer,
    highlightFirstColumn,
    highlightLastColumn,
    expandLastColumn,
    zebraTable,
    handleRowClick,
    rowMenu,
    className,
    data,
    ...rest
  } = props
  const otherProps = omit([ 'filterInput' ], rest)
  function createCell (item, i) {
    const cellStyle = !isNil(item) && item.toString().length > 50 ? { whiteSpace: 'normal' } : {}
    return <td style={ cellStyle } key={ `cell-${i}` }>{ item }</td>
  }

  const renderRowMenu = (info) => (
    <td className="table__row-menu-cell" style={ { whiteSpace: 'normal' } }>
      <MTTableRowMenu rowMenu={ rowMenu } data={ info } />
    </td>
  )

  const createClickableRow = function (handleFunction) {
    const onClick = (d) => () => handleFunction(d)
    // eslint-disable-next-line react/no-unstable-nested-components
    return (item, i) => (
      <tr className="mt-table__clickable-row" onClick={ onClick(data[i]) } key={ `row-${i}` }>
        { item.map(createCell) }
        { rowMenu && renderRowMenu(data[i]) }
      </tr>
    )
  }

  function createRow (item, i) {
    return (
      <tr key={ `row-${i}` }>
        { item.map(createCell) }
      </tr>
    )
  }
  const sortFunction = onSort == null ? () => {} : onSort
  const sortableClass = onSort == null ? '' : 'sortable'
  const headerElements = mapIndexed((item, i) => (
    <th
      key={ i }
      onClick={ () => sortFunction(i) }
      className={ `${sortableClass}` }
    >
      <div className="table-cell-wrapper">
        <span className="table-cell-wrapper__item">
          { item }
        </span>
        <div className="table-cell-wrapper__caret">
          { sortColumn === i && (
            <MTIcon type={ isAscending ? 'caret-down' : 'caret-up' } />
          ) }
        </div>
      </div>
    </th>
  ), headers)

  const footerElementForMenu = rowMenu && (
    <td className="table__row-menu-footer-cell" />
  )

  const headerElementForMenu = rowMenu && (
    <th className="table__row-menu-header-cell" />
  )

  const rowElements = handleRowClick
    ? rows.map(createClickableRow(handleRowClick))
    : rows.map(createRow)
  const footerElements = mapIndexed((item, i) => <td key={ i }>{ item }</td>, footer)
  const footerElement = footerElements.length > 0
    ? (
      <tfoot>
        <tr>{ footerElements }{ footerElementForMenu }</tr>
      </tfoot>
    )
    : null

  const tableClasses = cx('table', {
    'highlight-first': highlightFirstColumn,
    'highlight-last': highlightLastColumn,
    'expand-last-column': expandLastColumn,
    'is-zebra': zebraTable,
  })

  return (
    <table { ...otherProps } className={ `${tableClasses} ${className}` }>
      <thead>
        <tr>{ headerElements }{ headerElementForMenu }</tr>
      </thead>
      { footerElement }
      <tbody>{ rowElements }</tbody>
    </table>
  )
}

MTTable.defaultProps = {
  footer: [],
  highlightFirstColumn: false,
  highlightLastColumn: false,
  expandLastColumn: false,
  zebraTable: false,
  className: '',
  data: [],
  rows: [ [], [] ],
  headers: [ 'Name' ],
  onSort: null,
}

MTTable.propTypes = {
  onSort: PropTypes.func,
  headers: PropTypes.arrayOf(PropTypes.node),
  rows: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.any)),
  footer: PropTypes.arrayOf(PropTypes.node),
  highlightFirstColumn: PropTypes.bool,
  highlightLastColumn: PropTypes.bool,
  expandLastColumn: PropTypes.bool,
  zebraTable: PropTypes.bool,
  className: PropTypes.string,
  data: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.string, key: PropTypes.string })),
}

export default MTTable
