import React from 'react';
import cancelablePromise from '../../utils/cancelable-promise';
import numeral from 'numeral';

const propTypes = {
    enabled: React.PropTypes.bool,
    value: React.PropTypes.oneOfType([
        React.PropTypes.string,
        React.PropTypes.number
    ]),
    add: React.PropTypes.func,
    remove: React.PropTypes.func
};

export default class Quantity extends React.Component {

    static get defaultProps(){
        return {
            enabled: true,
            value: 0,
            add: () => {},
            remove: () => {}
        };
    }

    constructor(props) {
        super(props);
        this.state = {
            isLoading: false,
            value: props.value
        };
        this.cancelPromise = () => {};
        this.onSubmit = this.onSubmit.bind(this);
        this.onChange = this.onChange.bind(this);
        this.onDecrease = this.onDecrease.bind(this);
        this.onIncrease = this.onIncrease.bind(this);
        this.remove = this.remove.bind(this);
    }

    componentWillReceiveProps(nextProps) {
        if(!this.state.isLoading){
            this.setState({
                value: nextProps.value
            });
        }
    }

    onSubmit(event) {
        event.preventDefault();
    }

    onChange(event) {
        var value = numeral(event.target.value).value();
        var change = value - this.props.value;
        change !== 0 && this.modify(change);
    }

    onDecrease(event){
        event.preventDefault();
        this.modify(-1);
    }

    onIncrease(event){
        event.preventDefault();
        this.modify(1);
    }

    modify(add){
        this.cancelPromise();
        var value = this.state.value;
        this.setState({
            isLoading: true,
            value: value + add
        });
        var callback;
        if(add < 0){
            callback = this.props.remove;
        }
        else if(add > 0){
            callback = this.props.add;
        }
        const nativePromise = callback(Math.abs(add));
        const { promise, cancel } = cancelablePromise(nativePromise);
        this.cancelPromise = cancel;
        return promise
            .then(this.clearLoadingState.bind(this))
            .catch(this.onError.bind(this));
    }

    remove(event){
        event.preventDefault();
        this.setState({value: 0});
        this.props.remove();
    }

    clearLoadingState(){
        this.setState({
            isLoading: false,
            value: this.props.value
        });
    }

    onError(){}

    className(){
        var modifier = this.state.isLoading ? 'quantity--loading' : '';
        return `quantity ${modifier}`;
    }

    render() {
        const disabled = !this.props.enabled;
        return (
            <form
                onSubmit={this.onSubmit}
                className={this.className()}>
                <button
                    className="quantity__step quantity__step--descrease"
                    type="button"
                    onClick={this.onDecrease}
                    disabled={disabled || this.state.value <= 1}>
                    Decrease
                </button>
                <input
                    disabled={disabled}
                    className="quantity__input"
                    type="number"
                    onChange={this.onChange}
                    min="1"
                    value={this.state.value} />
                <button
                    disabled={disabled}
                    className="quantity__step quantity__step--increase"
                    type="button"
                    onClick={this.onIncrease}>
                    Increase
                </button>
                <button
                    disabled={disabled}
                    className="quantity__remove"
                    type="button"
                    onClick={this.remove}>
                    Remove
                </button>
                <div className="quantity__loader"></div>
            </form>
        );
    }
}

Quantity.propTypes = propTypes;