import React from 'react';
import Button from '../button/button.jsx';
import CustomerForm from '../customer-form/customer-form.jsx';

const SHOW_INFORMATION = 1;
const SHOW_FORM = 2;

const propTypes = {
    language: React.PropTypes.object.isRequired,
    customerFormLanguage: React.PropTypes.object.isRequired,
    logout: React.PropTypes.func,
    update: React.PropTypes.func,
    formData: React.PropTypes.shape({
        firstName: React.PropTypes.string,
        lastName: React.PropTypes.string,
        address: React.PropTypes.object,
        phone: React.PropTypes.string,
        email: React.PropTypes.string
    })
};

export default class CustomerProfile extends React.Component {

    static get defaultProps(){
        return {
            formData: {}
        };
    }

    constructor(props){
        super(props);
        this.state = {
            show: SHOW_INFORMATION,
            formData: props.formData
        };
    }

    show(value){
        this.setState({show: value});
    }

    static renderText(value, cls){
        var className = 'customer-profile__' + cls;
        return value && (
            <span className={className}>
                {value}
            </span>
        );
    }

    renderInformation(){
        var language = this.props.language;
        var formData = this.state.formData;
        return (
            <div className="customer-profile__information">
                <span className="customer-profile__name">
                    {this.constructor.renderText(language.hello, 'name__hello')}
                    {this.constructor.renderText(formData.firstName, 'name__first')}
                    {this.constructor.renderText(formData.lastName, 'name__last')}
                </span>
                <span className="customer-profile__address">
                    {this.constructor.renderText(formData.address.address, 'address__address')}
                    {this.constructor.renderText(formData.address.zipcode, 'address__zipcode')}
                    {this.constructor.renderText(formData.address.city, 'address__city')}
                    {this.constructor.renderText(formData.address.state, 'address__state')}
                    {this.constructor.renderText(formData.address.country, 'address__country')}
                </span>
                <span className="customer-profile__contact">
                    {this.constructor.renderText(formData.email, 'contact__email')}
                    {this.constructor.renderText(formData.phone, 'contact__phone')}
                </span>
                <div className="customer-profile__edit-link">
                    <button
                        type="button"
                        className="customer-profile__edit-link__button"
                        onClick={() => this.show(SHOW_FORM)}>
                        {language.edit}
                    </button>
                </div>
            </div>
        );
    }

    onChange(formData){
        this.setState({
            formData: formData
        });
    }

    onSubmit(){
        return this.props.update(this.state.formData);
    }

    renderForm(){
        const { customerFormLanguage, formData } = this.props;
        const onChange = this.onChange.bind(this);
        const onSubmit = this.onSubmit.bind(this);
        return (
            <div className="customer-profile__form">
                <CustomerForm
                    language={customerFormLanguage}
                    showPasswordToggle={false}
                    formData={formData}
                    onSubmit={onSubmit}
                    onChange={onChange}
                    onCancel={() => this.show(SHOW_INFORMATION)} />
            </div>
        );
    }

    render() {
        var language = this.props.language;
        return (
            <section className="customer-profile">
                {this.state.show == SHOW_INFORMATION && this.renderInformation()}
                {this.state.show == SHOW_FORM && this.renderForm()}
                <aside className="customer-profile__aside">
                    <Button
                        className="customer-profile__logout"
                        text={language.logout}
                        onClick={this.props.logout}/>
                </aside>
            </section>
        );
    }
}

CustomerProfile.propTypes = propTypes;