import React from 'react';
import Quantity from '../quantity/quantity.jsx';
import Tooltip from '../tooltip/tooltip.jsx';

const propTypes = {
    enabled: React.PropTypes.bool,
    language: React.PropTypes.object.isRequired,
    id: React.PropTypes.string.isRequired,
    discountPrice: React.PropTypes.object,
    discountPriceWithoutTax: React.PropTypes.object,
    imageUrl: React.PropTypes.string,
    listPrice: React.PropTypes.object,
    name: React.PropTypes.string,
    quantity: React.PropTypes.number,
    quantityInStock: React.PropTypes.number,
    totalPrice: React.PropTypes.object,
    totalDiscount: React.PropTypes.object,
    url: React.PropTypes.string,
    meta: React.PropTypes.object,
    removeItem: React.PropTypes.func,
    addItem: React.PropTypes.func
};

export default class CartItem extends React.Component {

    static get defaultProps(){
        return {
            enabled: true,
            language: {},
            id: null,
            name: null,
            quantity: null,
            quantityInStock: null,
            imageUrl: null,
            url: null,
            discountPrice: null,
            discountPriceWithoutTax: null,
            listPrice: null,
            totalPrice: null,
            totalDiscount: null,
            meta: {},
            removeItem: () => {},
            addItem: () => {}
        };
    }

    constructor(props) {
        super(props);
        this.remove = this.remove.bind(this);
        this.state = {
            errorMessage: null
        };
    }

    renderMeta(){
        var items = [];
        for(var key in this.props.meta){
            var value = this.props.meta[key];
            items.push(`${key}: ${value}`);
        }
        return items;
    }

    remove(quantity){
        return this.props.removeItem(this.props.id, quantity)
            .catch(this.setErrorState.bind(this));
    }

    add(quantity){
        return this.props.addItem(this.props.id, quantity)
            .catch(this.setErrorState.bind(this));
    }

    setErrorState(error){
        this.setState({
            errorMessage: error.message
        });
    }

    clearErrorState(){
        this.setState({errorMessage: null});
    }

    renderErrorMessage(){
        return this.state.errorMessage &&
            <div className="cart-item__error-message">
                <Tooltip
                    close={() => this.clearErrorState()}
                    type="error">
                    {this.state.errorMessage}
                </Tooltip>
            </div>;
    }

    cssClass(){
        var modifier = !this.props.enabled ? 'cart-item--disabled' : '';
        return `cart-item ${modifier}`;
    }

    render() {
        const {
            language,
            imageUrl,
            url,
            name,
            discountPrice,
            listPrice,
            discountPriceWithoutTax,
            totalDiscount,
            totalPrice
        } = this.props;
        return (
            <tr className={this.cssClass()}>
                <td className="cart-item__column">
                    <div className="cart-item__product">
                        <img
                            className="cart-item__product-image"
                            src={imageUrl}
                            alt="" />
                    </div>
                </td>
                <td className="cart-item__column">
                    <div className="cart-item__product">
                        <div className="cart-item__product-text">
                            <h3 className="cart-item__name">
                                <a className="cart-item__name-anchor"
                                href={url}>
                                {name}
                            </a>
                        </h3>
                        <p className="cart-item__description">
                            {this.renderMeta()}
                        </p>
                    </div>
                </div>
                </td>
                <td className="cart-item__column">
                    <span className="cart-item__discount-price">
                        {discountPrice.formatted}
                    </span>
                    <span className="cart-item__list-price">
                        {listPrice.formatted}
                    </span>
                    {discountPriceWithoutTax.formatted && (
                        <span className="cart-item__discount-price-without-tax">
                            {discountPriceWithoutTax.formatted} {language.exVat}
                        </span>
                    )}
                </td>
                <td className="cart-item__column">
                    <div className="cart-item__quantity">
                        <Quantity
                            enabled={this.props.enabled}
                            value={this.props.quantity}
                            add={this.add.bind(this)}
                            remove={this.remove.bind(this)}
                            removeAll={this.remove.bind(this)} />
                        {this.renderErrorMessage()}
                    </div>
                </td>
                <td className="cart-item__column">
                    <div className="cart-item__discount">
                        {totalDiscount.formatted}
                    </div>
                </td>
                <td className="cart-item__column">
                    <div className="cart-item__total">
                        {totalPrice.formatted}
                    </div>
                </td>
            </tr>
        );
    }
}

CartItem.propTypes = propTypes;