import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import Form from 'shared/components/form/form';
import FormErrors from 'shared/components/form/form_errors';
import Input from 'shared/components/form/input';
import Button from 'shared/components/form/button';
import Divider from 'shared/components/divider';
import OAuthButtons from '../../oauth_buttons';
import * as Validators from 'shared/constants/validators';

import '../authentication_form.scss';
import 'shared/components/link/link.scss';

class LoginForm extends PureComponent {
  static propTypes = {
    onSubmit: PropTypes.func.isRequired,
    activeTab: PropTypes.bool,
    openPasswordModal: PropTypes.func
  };

  constructor() {
    super();
    this.state = {remember_me: true, email: '', password: ''};
  }

  onSubmit = () => this.props.onSubmit(this.state);

  render() {
    return (
      <div
        className="mo-web-authentication-form"
        data-active={this.props.activeTab}
      >
        <div className="authentication-title">Have an Account?</div>
        <div className="authentication-description">
          Sign in to speed up the checkout process, check your order status and
          save your favorite products.
        </div>
        <Form validateOnlyAfterSubmit onSubmit={this.onSubmit} novalidate>
          <FormErrors />
          <Input
            type="email"
            name="email"
            placeholder="Email"
            required
            validators={[Validators.email]}
            value={this.state.email}
            onChange={email => this.setState({email})}
          />
          <Input
            type="password"
            name="password"
            placeholder="Password"
            required
            value={this.state.password}
            onChange={password => this.setState({password})}
          />
          <div className="authentication-checkbox authentication-remember-me">
            <input
              type="checkbox"
              checked={this.state.remember_me}
              onChange={ev => this.setState({remember_me: ev.target.checked})}
            />
            <span>Remember me</span>
          </div>
          <a
            className="authentication-forgot-password mo-web-link mo-web-link--plain"
            onClick={this.props.openPasswordModal}
            href="#"
          >
            Forgot password?
          </a>
          <Button
            className="authentication-submit-button"
            type="submit"
            theme="primary"
          >
            Sign In
          </Button>
        </Form>
        <Divider />
        <OAuthButtons action="login" remember_me={this.state.remember_me} />
        <a
          className="authentication-privacy-policy mo-web-link mo-web-link--plain"
          href="/privacy"
          target="_blank"
        >
          Privacy Policy
        </a>
      </div>
    );
  }
}

export default LoginForm;
