import React, { Component } from 'react'
import debounce from 'debounce'
import cx from 'classnames'
import {
  complement,
  defaultTo,
  equals,
  keys,
  omit,
  pick,
} from 'ramda'
import MTColorPicker from './mt-color-picker'

const pickEntityProps = pick([ 'name', 'color', 'abbreviation' ])
function entitiesAreEqual (a, b) {
  return equals(pickEntityProps(a), pickEntityProps(b))
}
const entityDiffers = complement(entitiesAreEqual)

class MTNameColorInput extends Component {
  constructor (props) {
    super(props)
    const { debounceTime, entity, status } = props
    this.state = { ...entity, status }
    this.commitChange = this.commitChange.bind(this)
    this.handleNameChange = this.handleNameChange.bind(this)
    this.handleAbbreviationChange = this.handleAbbreviationChange.bind(this)
    this.handleColorChange = this.handleColorChange.bind(this)
    this.debounceCommit = debounce(this.commitChange, debounceTime)
    this.clearStatus = this.clearStatus.bind(this)
    this.toCommit = {}
  }

  UNSAFE_componentWillReceiveProps (nextProps) {
    const isSaved = entityDiffers(nextProps.entity, this.props.entity) &&
      entitiesAreEqual(nextProps.entity, this.state)
    if (isSaved) {
      this.setState({ status: 'saved' })
    }
    this.setState({ entity: nextProps.entity })
  }

  componentWillUnmount () {
    clearTimeout(this.timeout)
  }

  handleColorChange (color) {
    this.setState({ color, status: 'pending' })
    this.toCommit.color = color
    this.debounceChanges()
  }

  handleNameChange (e) {
    const name = e.target.value
    this.setState({ name, status: 'pending' })
    this.toCommit.name = name
    this.debounceChanges()
  }

  handleAbbreviationChange (e) {
    const abbreviation = e.target.value
    this.setState({ abbreviation, status: 'pending' })
    this.toCommit.abbreviation = abbreviation
    this.debounceChanges()
  }

  hasNoChanges () {
    const changedKeys = keys(this.toCommit)
    const originalValues = pick(changedKeys, this.props.entity)
    return equals(this.toCommit, originalValues)
  }

  debounceChanges () {
    if (this.hasNoChanges()) {
      this.clearStatus()
      this.debounceCommit.clear()
    } else {
      this.debounceCommit(this.toCommit)
    }
    clearTimeout(this.timeout)
  }

  commitChange (entity) {
    const { onChange } = this.props
    this.toCommit = {}
    onChange(entity)
  }

  clearStatus () {
    this.setState({ status: '' })
  }

  render () {
    const {
      disabled,
      showAbbreviation,
      className,
      ...rest
    } = this.props
    const otherProps = omit([ 'entity', 'onChange', 'debounceTime' ], rest)
    const {
      color,
      name,
      status,
      abbreviation,
    } = this.state
    return (
      <div
        { ...otherProps }
        className={ cx(
          'mt-name-color-input',
          `mt-name-color-input--status-${status}`,
          disabled && 'is-disabled',
          className
        ) }
      >
        <input
          type="text"
          className="mt-name-color-input__input"
          value={ name }
          onChange={ this.handleNameChange }
          disabled={ disabled }
          placeholder="Name"
        />
        { showAbbreviation && (
          <input
            type="text"
            className="mt-name-color-input__input mt-name-color-input__input--abbr"
            value={ defaultTo('', abbreviation) }
            onChange={ this.handleAbbreviationChange }
            disabled={ disabled }
            placeholder="Abbreviation"
          />
        ) }
        { disabled
          ? (
            <div className="color-picker-placeholder" style={ { backgroundColor: color } } />
          )
          : (
            <MTColorPicker
              color={ color }
              handleChange={ this.handleColorChange }
            />
          )
        }
      </div>
    )
  }
}

MTNameColorInput.defaultProps = {
  entity: {},
  status: '',
  debounceTime: 1000,
  showAbbreviation: false,
}

export default MTNameColorInput
