import React from 'react';
import Message from '../../components/message/message.jsx';
import ForgotPassword from '../forgot-password/forgot-password.jsx';

const propTypes = {
    language: React.PropTypes.object.isRequired,
    description: React.PropTypes.string,
    login: React.PropTypes.func.isRequired,
    cancel: React.PropTypes.func.isRequired,
    resetPassword: React.PropTypes.func.isRequired
};

export default class Login extends React.Component {

    constructor(props) {
        super(props);
        this.showForgotPassword = this.showForgotPassword.bind(this);
        this.hideForgotPassword = this.hideForgotPassword.bind(this);
        this.state = {
            errorMessage: null,
            showForgotPassword: false
        };
    }

    login(event){
        event.preventDefault();
        var userId = this.userIdInput.value;
        var password = this.passwordInput.value;
        this.props.login(userId, password)
            .catch(this.setErrorState.bind(this));
    }

    resetPassword(userId){
        this.props.resetPassword(userId)
            .catch(this.setErrorState.bind(this));
    }

    setErrorState(error){
        this.setState({errorMessage: error.message});
    }

    showForgotPassword(){
        this.setState({showForgotPassword: true});
    }

    hideForgotPassword(){
        this.setState({showForgotPassword: false});
    }

    renderHeader(){
        return this.props.language.heading && (
            <header className="login__header">
                <h2 className="login__heading">
                    {this.props.language.heading}
                </h2>
            </header>
        );
    }

    renderError(){
        return (
            <div className="login__error-message">
                <Message type="error" text={this.state.errorMessage} />
            </div>
        );
    }

    renderLoginForm(){
        return <form onSubmit={event => this.login(event)}>
            <div className="login__fields">
                <input
                    ref={input => { this.userIdInput = input; }}
                    className="login__email"
                    type="email"
                    placeholder={this.props.language.userIdPlaceholder} />
                <input
                    ref={input => { this.passwordInput = input; }}
                    className="login__password"
                    type="password"
                    placeholder={this.props.language.passwordPlaceholder} />
                <button
                    className="login__submit"
                    type="submit">
                    {this.props.language.loginSubmit}
                </button>
            </div>
            <div
                className="login__links">
                <button
                    className="login__link"
                    type="button"
                    onClick={this.showForgotPassword}>
                    {this.props.language.forgotPassword}
                </button>
                <button
                    className="login__link"
                    type="button"
                    onClick={this.props.cancel}>
                    {this.props.language.cancel}
                </button>
            </div>
        </form>;
    }

    renderFooter(){
        return <footer className="login__footer">
            {this.props.description}
        </footer>;
    }

    render() {
        return (
            <section className="login">
                {this.renderHeader()}
                {this.state.errorMessage && this.renderError()}
                {!this.state.showForgotPassword && this.renderLoginForm()}
                {this.state.showForgotPassword && (
                    <ForgotPassword
                        language={this.props.language}
                        onCancel={this.hideForgotPassword}
                        onSubmit={userId => this.resetPassword(userId)} />
                )}
                {!this.state.showForgotPassword && this.renderFooter()}
            </section>
        );
    }
}

Login.propTypes = propTypes;