import React from 'react';
import cancelablePromise from '../../utils/cancelable-promise';
import Tooltip from '../tooltip/tooltip.jsx';

const propTypes = {
    search: React.PropTypes.func.isRequired,
    defaultValue: React.PropTypes.string,
    language: React.PropTypes.object.isRequired
};

export default class RegistrationPhoneSearch extends React.Component {

    static get defaultProps(){
        return {
            search: () => {},
            defaultValue: '',
            language: {}
        };
    }

    constructor(props){
        super(props);
        this.cancelPromise = () => {};
        this.state = {
            isLoading: false,
            errorMessage: null
        };
    }

    onSubmit(event){
        event.preventDefault();
        this.setState({
            isLoading: true,
            errorMessage: null
        });
        const { value } = this.searchInput;
        const { search } = this.props;
        const nativePromise = search({phone: value});
        const { promise, cancel } = cancelablePromise(nativePromise);
        this.cancelPromise = cancel;
        return promise
            .then(this.clearLoadingState.bind(this))
            .catch(this.setErrorState.bind(this));
    }

    componentWillUnmount() {
        this.cancelPromise();
    }

    clearLoadingState(){
        this.setState({
            isLoading: false,
            errorMessage: null
        });
    }

    setErrorState(error){
        if(error.isCanceled) return;
        this.setState({
            isLoading: false,
            errorMessage: error.message
        });
    }

    clearErrorState(){
        this.setState({
            errorMessage: null
        });
    }

    renderLoader(){
        return this.state.isLoading &&
            <div className="registration__phone-search__loader"></div>;
    }

    renderErrorMessage(){
        return this.state.errorMessage &&
            <div className="registration__phone-search__error-message">
                <Tooltip
                    close={() => this.clearErrorState(null)}
                    type="error">
                    {this.state.errorMessage}
                </Tooltip>
            </div>;
    }

    className(){
        var modifier = this.state.isLoading ? 'registration__phone-search--loading' : '';
        return `registration__phone-search ${modifier}`;
    }

    render() {
        return (
            <form
                className={this.className()}
                onSubmit={event => this.onSubmit(event)}>
                <label
                    className="registration__phone-search__label"
                    htmlFor="registration__phone-search">
                    {this.props.language.searchHeading}
                </label>
                <div className="registration__phone-search__input-container">
                    <input
                        ref={searchInput => { this.searchInput = searchInput; }}
                        className="registration__phone-search__input"
                        id="registration__phone-search"
                        type="tel"
                        defaultValue={this.props.defaultValue}
                        placeholder={this.props.language.searchPhoneNumberPlaceholder} />
                    {this.renderLoader()}
                    {this.renderErrorMessage()}
                </div>
                <button
                    className="registration__phone-search__submit"
                    type="submit">
                    {this.props.language.searchSubmit}
                </button>
            </form>
        );
    }
}

RegistrationPhoneSearch.propTypes = propTypes;