import React, { Component } from 'react'
import cx from 'classnames'
import PropTypes from 'prop-types'
import { identity, isNil, omit } from 'ramda'
import { CSSTransition, TransitionGroup } from 'react-transition-group'
import MTIcon from './mt-icon'

class MTSlideshow extends Component {
  constructor (props) {
    super(props)
    this.handleKeyDown = this.handleKeyDown.bind(this)
    this.state = {
      activeSlideIndex: 0,
    }
    this.moveBack = this.moveBack.bind(this)
    this.moveForward = this.moveForward.bind(this)
    this.getSlideElement = this.getSlideElement.bind(this)
    this.createdSlides = {}
  }

  componentDidMount () {
    window.addEventListener('keydown', this.handleKeyDown)
  }

  UNSAFE_componentWillReceiveProps (nextProps) {
    if (nextProps.slideFunctions !== this.props.slideFunctions) {
      this.createdSlides = {}
    }
  }

  componentWillUnmount () {
    window.removeEventListener('keydown', this.handleKeyDown)
  }

  handleKeyDown (e) {
    const { onClose } = this.props
    if (e.keyCode === 37) {
      // left key press
      this.moveBack()
    } else if (e.keyCode === 39) {
      // right key press
      this.moveForward()
    } else if (e.keyCode === 27) {
      // escape key press
      onClose()
    }
  }

  getSlideElement (index) {
    const {
      slideFunctions,
      enterTimeout,
      exitTimeout,
      enter,
      enterActive,
      exit,
      exitActive,
    } = this.props
    const { activeSlideIndex } = this.state
    if (isNil(this.createdSlides[index])) {
      this.createdSlides[index] = (
        <CSSTransition
          in={ activeSlideIndex === index }
          unmountOnExit={ true }
          mountOnEnter={ true }
          timeout={ {
            enter: enterTimeout,
            exit: exitTimeout,
          } }
          key={ index }
          classNames={ {
            enter,
            enterActive,
            exit,
            exitActive,
          } }
        >
          <div className="mt-slideshow__slide">
            { slideFunctions[index]() }
          </div>
        </CSSTransition>
      )
    }
    return this.createdSlides[index]
  }

  moveForward () {
    const { activeSlideIndex } = this.state
    const { slideFunctions } = this.props
    if (activeSlideIndex < slideFunctions.length - 1) {
      this.setState((prevState) => ({
        activeSlideIndex: prevState.activeSlideIndex + 1,
      }))
    }
  }

  moveBack () {
    const { activeSlideIndex } = this.state
    if (activeSlideIndex > 0) {
      this.setState((prevState) => ({
        activeSlideIndex: prevState.activeSlideIndex - 1,
      }))
    }
  }

  render () {
    const {
      slideFunctions,
      slideshowHeader,
      onClose,
      ...rest
    } = this.props
    const otherProps = omit([
      'enterTimeout',
      'exitTimeout',
      'enter',
      'enterActive',
      'exit',
      'exitActive',
    ], rest)
    const { activeSlideIndex } = this.state

    return (
      <div
        { ...otherProps }
        className={ cx('mt-slideshow__wrapper') }
      >
        { slideshowHeader }
        <TransitionGroup className="mt-slideshow__slides" component={ null }>
          { this.getSlideElement(activeSlideIndex) }
        </TransitionGroup>
        <MTIcon
          type="arrow-left"
          className={ `mt-slideshow__back ${activeSlideIndex === 0 ? 'is-disabled' : ''}` }
          onClick={ this.moveBack }
        />
        <MTIcon
          type="arrow-right"
          className={ `mt-slideshow__forward ${activeSlideIndex === slideFunctions.length - 1 ? 'is-disabled' : ''}` }
          onClick={ this.moveForward }
        />
        <MTIcon
          type="close"
          className="mt-slideshow__close"
          onClick={ onClose }
        />
      </div>
    )
  }
}

MTSlideshow.defaultProps = {
  slideshowHeader: null,
  enterTimeout: 320,
  exitTimeout: 160,
  enter: 'animated-short',
  enterActive: 'tdFadeIn',
  exit: 'animated-instant',
  exitActive: 'tdFadeOut',
  onClose: identity,
}

MTSlideshow.propTypes = {
  slideshowHeader: PropTypes.element,
  slideFunctions: PropTypes.arrayOf(PropTypes.func).isRequired,
  enterTimeout: PropTypes.number,
  exitTimeout: PropTypes.number,
  enter: PropTypes.string,
  enterActive: PropTypes.string,
  exit: PropTypes.string,
  exitActive: PropTypes.string,
  onClose: PropTypes.func,
}

export default MTSlideshow
