/* eslint no-nested-ternary: 0 */
import React, { Component } from 'react';
import ReactSlider from 'react-slider';
// import { FormattedCurrency } from 'react-formatted';
import cn from 'classnames';

import 'jqestate.ru/styles/ui/slider';

class Slider extends Component {
  constructor(props) {
    super(props);

    this.state = {
      ...props,
      value: props.value,
    };
  }

  componentWillReceiveProps(nextProps) {
    const { value } = nextProps;
    const { max, min } = nextProps.value;

    if (max === `max` || max > nextProps.max) nextProps.value.max = nextProps.max;
    if (max < nextProps.min) nextProps.value.max = nextProps.min;
    if (min < nextProps.min) nextProps.value.min = nextProps.min;

    this.setState({
      value,
    });
  }

  handleChange(val) {
    const [min, max] = val;
    const value = { min, max };

    this.setState({ value });
  }

  handleAfterChange() {
    const { transform } = this.props;
    const { value } = this.state;

    const min = transform ? transform(value.min) : value.min;
    // FIXME
    const max = (value.max === this.state.max) ? `max` : transform ? transform(value.max) : value.max;

    if (min === this.state.min && max === `max`) {
      return this.props.handleChange(this.props.reference, null);
    }

    this.props.handleChange(this.props.reference, { min, max });
  }

  render() {
    const { step, valueFormat } = this.props;
    const { value, min, max } = this.state;

    return (
      <div>
        <div className="slider-handle-title" key="min">
          {valueFormat.prefix}
          {value.min}
          &nbsp;{valueFormat.postfix}
        </div>
        <ReactSlider step={step} value={[value.min, value.max]} min={min} max={max} className={cn(`slider`, this.props.className)} barClassName="slider-bar" handleClassName="slider-handle" onChange={::this.handleChange} onAfterChange={::this.handleAfterChange} pearling withBars />
        <div className="slider-handle-title" key="max">
          {valueFormat.prefix}
          {value.max === max ? valueFormat.max : value.max}
          &nbsp;{valueFormat.postfix}
        </div>
      </div>
    );
  }
}

export default Slider;
