import React, { Component } from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import { addIndex, append, contains, equals, isArrayLike, map, omit, reject } from 'ramda'
import MTIcon from './mt-icon'

const mapIndexed = addIndex(map)

class MTAccordion extends Component {
  constructor (props) {
    const { initialSectionIndex } = props
    super(props)
    this.state = {
      selectedSectionsIndex: [ initialSectionIndex ],
    }
  }

  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.initialSectionIndex !== this.props.initialSectionIndex) {
      this.setState({ selectedSectionsIndex: [ nextProps.initialSectionIndex ] })
    }
  }

  toggleSection (idx) {
    const { keepOpen } = this.props
    const { selectedSectionsIndex } = this.state
    const isOpen = contains(idx, selectedSectionsIndex)
    if (keepOpen) {
      this.setState({
        selectedSectionsIndex: isOpen
          ? reject(equals(idx), selectedSectionsIndex)
          : append(idx, selectedSectionsIndex),
      })
    } else {
      this.setState({
        selectedSectionsIndex: isOpen
          ? [ -1 ]
          : [ idx ],
      })
    }
  }

  render () {
    const {
      sections,
      sectionLabels,
      maxHeight,
      className,
      ...rest
    } = this.props
    const otherProps = omit([ 'initialSectionIndex', 'keepOpen' ], rest)
    const { selectedSectionsIndex } = this.state
    const sectionItems = mapIndexed((section, idx) => {
      const isActive = contains(idx, selectedSectionsIndex)
      const sectionClass = cx('mt-accordion', { 'is-active': isActive }, className)
      return (
        <div { ...otherProps } className={ sectionClass } key={ idx }>
          <div className="mt-accordion__label" onClick={ () => this.toggleSection(idx) }>
            <MTIcon type={ isActive ? 'caret-up' : 'caret-down' } /> { sectionLabels[idx] }
          </div>
          <div className="mt-accordion__content" style={ isActive ? { maxHeight } : {} }>
            { section }
          </div>
        </div>
      )
    }, sections)

    return (
      <div className="mt-accordions">
        { sectionItems }
      </div>
    )
  }
}

MTAccordion.defaultProps = {
  initialSectionIndex: 0,
  sectionLabels: [],
  // Set max height as close as possible to the height of an expanded section for smooth transitions
  maxHeight: 200,
  keepOpen: false,
}

MTAccordion.propTypes = {
  sections: PropTypes.arrayOf(PropTypes.element).isRequired,
  sectionLabels: (props, propName, componentName) => {
    if (!isArrayLike(props[propName]) || props[propName].length !== props.sections.length) {
      return new Error(
        `Invalid prop ${propName} supplied to ${componentName}. Must be array of same length as sections.`
      )
    }
    return null
  },
  initialSectionIndex: (props, propName, componentName) => {
    if (props[propName] > props.sections.length - 1) {
      return new Error(
        `Invalid prop ${propName} supplied to ${componentName}. Index larger than length of sections.`
      )
    }
    return null
  },
  maxHeight: PropTypes.number,
  keepOpen: PropTypes.bool,
}

export default MTAccordion
