import React from 'react';
import extend from 'extend';
import CustomerForm from '../customer-form/customer-form.jsx';
import PhoneSearch from './phone-search.jsx';
import SearchResults from './search-results.jsx';

const SHOW_SEARCH = 1;
const SHOW_RESULTS = 2;
const SHOW_FORM = 3;

const isSet = value => [undefined, null, ''].indexOf(value) === -1;

const propTypes = {
    register: React.PropTypes.func.isRequired,
    search: React.PropTypes.func.isRequired,
    language: React.PropTypes.object.isRequired,
    customerFormLanguage: React.PropTypes.object.isRequired,
    formData: React.PropTypes.shape({
        firstName: React.PropTypes.string,
        lastName: React.PropTypes.string,
        address: React.PropTypes.object,
        phone: React.PropTypes.string,
        email: React.PropTypes.string,
        password: React.PropTypes.string,
        passwordRepeated: React.PropTypes.string
    })
};

export default class Registration extends React.Component {

    static get defaultProps(){
        return {
            register: () => {},
            search: () => {},
            language: {},
            customerFormLanguage: {},
            formData: {
                lastName: null,
                firstName: null,
                email: null,
                phone: null,
                address: {},
                password: null,
                passwordRepeated: null
            }
        };
    }

    constructor(props) {
        super(props);
        this.state = {
            show: SHOW_SEARCH,
            formData: props.formData
        };
    }

    componentWillReceiveProps(nextProps) {
        var hasSearchResults = this.constructor.hasSearchResults(nextProps);
        if(hasSearchResults && this.state.show == SHOW_SEARCH){
            var formData = nextProps.formData;
            this.setState({ formData: formData });
            this.show(SHOW_RESULTS);
        }
    }

    static hasSearchResults(props){
        var phone = props.formData.phone;
        return isSet(phone);
    }

    handleCustomerFormChange(formData){
        this.setState({
            formData: extend(true, this.state.formData, formData)
        });
    }

    onSubmit(event){
        event && event.preventDefault();
        return this.props.register(this.state.formData);
    }

    show(value){
        this.setState({show: value});
    }

    onDetailsCancel(){
        if(this.constructor.hasSearchResults(this.props)){
            this.show(SHOW_RESULTS);
        }
        else {
            this.show(SHOW_SEARCH);
        }
    }

    renderSearch(){
        return (
            <div>
                <PhoneSearch
                    search={this.props.search}
                    language={this.props.language} />
                <button
                    className="registration__details-link"
                    type="button"
                    onClick={() => this.show(SHOW_FORM)}>
                    {this.props.language.registerNewAccount}
                </button>
            </div>
        );
    }

    renderSearchResults(){
        var formData = this.state.formData;
        const onSubmit = this.props.register;
        const customerFormLanguage = extend({}, this.props.customerFormLanguage);
        delete customerFormLanguage.heading;
        return (
            <div>
                <PhoneSearch
                    search={this.props.search}
                    defaultValue={formData.phone}
                    language={this.props.language} />
                <SearchResults
                    {...formData} />
                <button
                    className="registration__details-link"
                    type="button"
                    onClick={() => this.show(SHOW_FORM)}>
                    {customerFormLanguage.useAnotherAddress}
                </button>
                <CustomerForm
                    formData={formData}
                    fieldsWhitelist={['email']}
                    language={customerFormLanguage}
                    onChange={formData => this.handleCustomerFormChange(formData)}
                    onCancel={() => this.onDetailsCancel()}
                    onSubmit={onSubmit} />
            </div>
        );
    }

    renderForm(){
        var formData = this.state.formData;
        const onSubmit = this.props.register;
        return (
            <div>
                <CustomerForm
                    formData={formData}
                    language={this.props.customerFormLanguage}
                    onChange={formData => this.handleCustomerFormChange(formData)}
                    onCancel={() => this.onDetailsCancel()}
                    onSubmit={onSubmit} />
            </div>
        );
    }

    render() {
        const { show } = this.state;
        const showSearch = show == SHOW_SEARCH;
        const showResults = show == SHOW_RESULTS;
        const showForm = show == SHOW_FORM;
        return (
            <div className="registration">
                {showSearch && this.renderSearch()}
                {showResults && this.renderSearchResults()}
                {showForm && this.renderForm()}
            </div>
        );
    }
}

Registration.propTypes = propTypes;