import React from 'react';
import Tick from '../tick/tick.jsx';

const propTypes = {
    onChange: React.PropTypes.func,
    language: React.PropTypes.object.isRequired,
    showToggle: React.PropTypes.bool,
    password: React.PropTypes.string,
    passwordRepeated: React.PropTypes.string
};

var id = 0;

export default class CustomerFormPassword extends React.Component {

    static get defaultProps(){
        return {
            onChange: () => {},
            language: {},
            showToggle: true,
            password: '',
            passwordRepeated: ''
        };
    }

    constructor(props) {
        super(props);
        this.id = `customer-form__password-${id++}`;
        this.state = {
            show: !this.props.showToggle
        };
    }

    show(){
        this.setState({ show: true });
    }

    hide(){
        this.setState({ show: false });
    }

    render() {
        const { onChange, language, showToggle } = this.props;
        const passwordId = `${this.id}__password`;
        const passwordRepeatId = `${this.id}__password-repeat`;
        return (
            <div className="customer-form__password">
                {showToggle && (
                    <div className="customer-form__password__tick">
                        <Tick
                            label={language.passwordToggle}
                            onCheck={() => this.show()}
                            onUncheck={() => this.hide()} />
                    </div>
                )}
                {this.state.show && (
                    <div className="customer-form__password__fields">
                        <label
                            htmlFor={passwordId}
                            className="customer-form__password__label">
                            {language.createPassword}
                        </label>
                        <input
                            id={passwordId}
                            className="customer-form__text-input"
                            type="password"
                            name="password"
                            value={this.props.password}
                            onChange={onChange}
                            placeholder={language.createPasswordPlaceholder} />
                        <label
                            htmlFor={passwordRepeatId}
                            className="customer-form__password__label">
                            {language.repeatPassword}
                        </label>
                        <input
                            id={passwordRepeatId}
                            className="customer-form__text-input"
                            type="password"
                            name="passwordRepeated"
                            value={this.props.passwordRepeated}
                            onChange={onChange}
                            placeholder={language.repeatPasswordPlaceholder} />
                    </div>
                )}
            </div>
        );
    }
}

CustomerFormPassword.propTypes = propTypes;