import React from 'react';
import Table from './table.jsx';
import Tooltip from '../tooltip/tooltip.jsx';

const propTypes = {
    enabled: React.PropTypes.bool,
    language: React.PropTypes.object.isRequired,
    appliedCouponCodes: React.PropTypes.array.isRequired,
    apply: React.PropTypes.func.isRequired
};

export default class Coupon extends React.Component {

    static get defaultProps(){
        return {
            enabled: true
        };
    }

    constructor(props) {
        super(props);
        this.state = {
            active: false,
            errorMessage: null,
            loadingCount: 0
        };
    }

    componentWillReceiveProps() {
        this.setState({ loadingCount: 0 });
    }

    toggleForm(){
        this.setState({active: true});
    }

    apply(event){
        event.preventDefault();
        this.setState({
            active: false,
            loadingCount: this.state.loadingCount + 1,
            errorMessage: null
        });
        var value = this.input.value;
        this.props.apply(value)
            .catch(this.setErrorState.bind(this));
    }

    setErrorState(error){
        this.setState({
            active: true,
            errorMessage: error.message
        });
    }

    clearErrorState(){
        this.setState({errorMessage: null});
    }

    renderToggle(){
        const disabled = !this.props.enabled;
        return (
            <button
                disabled={disabled}
                ref={toggle => { this.toggle = toggle; }}
                className="coupon__toggle"
                onClick={this.toggleForm.bind(this)}>
                {this.props.language.addCode}
            </button>
        );
    }

    renderForm(){
        const disabled = !this.props.enabled;
        return (
            <form
                className="coupon__form"
                onSubmit={event => this.apply(event)}>
                <input
                    disabled={disabled}
                    ref={input => { this.input = input; }}
                    type="text"
                    className="coupon__input" />
                <button
                    disabled={disabled}
                    type="submit"
                    className="coupon__submit">
                    {this.props.language.sendCode}
                </button>
            </form>
        );
    }

    renderErrorMessage(){
        return this.state.errorMessage &&
            <div className="coupon__error-message">
                <Tooltip
                    close={() => this.clearErrorState()}
                    type="error">
                    {this.state.errorMessage}
                </Tooltip>
            </div>;
    }

    cssClass(){
        var modifier = !this.props.enabled ? 'coupon--disabled' : '';
        return `coupon ${modifier}`;
    }

    render() {
        return (
            <section className={this.cssClass()}>
                <Table
                    appliedCouponCodes={this.props.appliedCouponCodes}
                    loadingCount={this.state.loadingCount} />
                {!this.state.active && this.renderToggle()}
                {this.state.active && this.renderForm()}
                {this.state.errorMessage && this.renderErrorMessage()}
            </section>
        );
    }
}

Coupon.propTypes = propTypes;