/* eslint-disable no-undef */
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 Checkbox from 'shared/components/form/checkbox';
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 pick from 'lodash.pick';

import '../authentication_form.scss';
import 'shared/components/link/link.scss';
class RegisterForm extends PureComponent {
  static propTypes = {
    onSubmit: PropTypes.func.isRequired,
    activeTab: PropTypes.bool,
    gender_preference: PropTypes.string,
    novalidate: PropTypes.bool,
    location: PropTypes.string
  };

  static defaultProps = {
    gender_preference: 'women'
  };

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

  handleSubmit = () =>
    this.props.onSubmit({
      ...pick(this.state, [
        'first_name',
        'last_name',
        'email',
        'password',
        'remember_me',
        'opted_in'
      ]),
      gender_preference: this.props.gender_preference,
      country_code: 'US'
    });

  render() {
    const special_login =
      typeof isFeatureFlagEnabled !== 'undefined' &&
      // eslint-disable-next-line no-undef
      isFeatureFlagEnabled('new-rewards-page') &&
      this.props.location == '/rewards';
    return (
      <div
        className="mo-web-authentication-form"
        data-active={this.props.activeTab}
      >
        {!special_login ? (
          <>
            <div className="authentication-title">Not a Member?</div>
            <div className="authentication-description">
              Create a Moda Operandi account to check your orders, save your
              information and keep your favorite products
            </div>
          </>
        ) : (
          <>
            <div className="authentication-title">Become a member</div>
            <div className="authentication-description">
              Create a Moda Operandi account to join Moda Rewards. You can also
              use your account to track orders, save your information, and add
              products to your Favorites.
            </div>
          </>
        )}
        <Form
          validateOnlyAfterSubmit
          onSubmit={this.handleSubmit}
          errorMessage={this.state.errorMessage}
          novalidate
        >
          <FormErrors />
          <Input
            type="text"
            name="first_name"
            placeholder="First Name"
            wrapperClassName="authentication-name-input"
            required
            value={this.state.first_name}
            onChange={first_name => this.setState({first_name})}
          />
          <Input
            type="text"
            name="last_name"
            placeholder="Last Name"
            wrapperClassName="authentication-name-input"
            required
            value={this.state.last_name}
            onChange={last_name => this.setState({last_name})}
          />
          <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
            validators={[Validators.minLength(6)]}
            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>
          <div className="authentication-checkbox authentication-opt-in">
            <Checkbox
              value={this.state.opted_in}
              checked={this.state.opted_in}
              onChange={ev => this.setState({opted_in: ev.target.checked})}
              type="checkbox"
              name="opted_in"
            />
            <span className="authentication-opt-in--text">
              Put me on the e-mail list for style inspiration & special access
            </span>
          </div>
          <Button
            className="authentication-submit-button"
            type="submit"
            theme="primary"
          >
            {!special_login ? `Register Now` : `Register & Enroll Now`}
          </Button>
        </Form>
        {!special_login && (
          <section>
            <Divider />
            <OAuthButtons
              action="register"
              opted_in={this.state.opted_in}
              remember_me={this.state.remember_me}
              setError={errorMessage => this.setState({errorMessage})}
            />
            <Divider />
            <div className="authentication-opt-out-privacy-policy">
              To opt-out of email sharing or to view our Privacy Policy,{' '}
              <a
                className="mo-web-link mo-web-link--plain"
                href="/privacy"
                target="_blank"
              >
                click here
              </a>
            </div>
          </section>
        )}
      </div>
    );
  }
}

export default RegisterForm;
