import PropTypes from 'prop-types';
import React, { Fragment, PureComponent } from 'react';
import injectSheet from 'react-jss';
import classnames from 'classnames';
import get from 'lodash/get';

import Checkbox from './Checkbox';
import MobileSelect from './MobileSelect';
import FormComponent from './FormComponent';
import TextArea from './TextArea';
import Input from './Input';
import styles from './styles/Preferences';
import { getMoveInChoices, getBudgetChoices } from '../../utils/choices';
import { getDisclaimerText } from '../../utils/text';
import { filterLayoutOptions } from '../../constants/layouts';
import { AMENITY_CHOICES, PET_CHOICES, NONE, SMS_OPT_IN_MARKETING_ENABLED } from '../../constants/config';

export class Preferences extends PureComponent {
    constructor(props){
        super(props);
        this.state = {
            budgetChoices: getBudgetChoices(props.formData.client.price_ceiling)
        };
    }

    render(){
        const {
            company,
            fieldConfig,
            formData,
            formErrors,
            classes,
            layoutOptions,
            updateFormData,
            showValidationErrors,
            submitButton,
            disabled,
            client,
            isSubmitting,
        } = this.props;

        const moveInChoices = getMoveInChoices(formData.date);

        const showBrokerFields = !!formData.appointment.broker_booked;
        const sharedProps = {
            onChange: updateFormData,
            fieldConfig,
            formData,
            showValidationErrors,
            formErrors,
        };

        return (
            <div className={classnames(classes.preferences, {
                ['disabled']: disabled
            })}>
                <div className={classes.row}>
                    <div className={classes.header}>Enter your Preferences</div>
                </div>
                <div className={classes.requiredNote}>
                    * = Required
                </div>
                <FormComponent
                    {...sharedProps}
                    showLabel={false}
                    FieldComponent={Checkbox}
                    label="I'm a broker"
                    name="appointment.broker_booked"
                    fieldConfigName="client.broker_toggle"
                />
                {
                    showBrokerFields &&
                    <FormComponent
                        {...sharedProps}
                        isAlwaysRequired
                        FieldComponent={Input}
                        label="Broker First Name"
                        name="client.broker_first_name"
                    />
                }
                {
                    showBrokerFields &&
                    <FormComponent
                        {...sharedProps}
                        isAlwaysRequired
                        FieldComponent={Input}
                        label="Broker Last Name"
                        name="client.broker_last_name"
                    />
                }
                {
                    showBrokerFields &&
                    <FormComponent
                        {...sharedProps}
                        isAlwaysRequired
                        FieldComponent={Input}
                        label="Broker Phone"
                        name="client.broker_phone"
                    />
                }
                {
                    showBrokerFields &&
                    <FormComponent
                        {...sharedProps}
                        isAlwaysRequired
                        FieldComponent={Input}
                        label="Broker Email"
                        name="client.broker_email"
                    />
                }
                {
                    showBrokerFields &&
                    <FormComponent
                        {...sharedProps}
                        FieldComponent={Input}
                        label="Brokerage"
                        name="client.broker_company"
                    />
                }
                { (!client.person.first_name || !client.person.last_name) &&
                    <Fragment>
                        <FormComponent
                            {...sharedProps}
                            FieldComponent={Input}
                            label="First Name"
                            name="client.person.first_name"
                        />
                        <FormComponent
                            {...sharedProps}
                            FieldComponent={Input}
                            label="Last Name"
                            name="client.person.last_name"
                        />
                    </Fragment>
                }
                { (!client.person.phone_1 || client.sms_opted_in !== SMS_OPT_IN_MARKETING_ENABLED) &&
                    <Fragment>
                        <FormComponent
                            {...sharedProps}
                            FieldComponent={Input}
                            label="Phone"
                            name="client.person.phone_1"
                            hideValue={isSubmitting && get(formData, 'client.sms_opted_in') !== SMS_OPT_IN_MARKETING_ENABLED}
                        />
                        <div className={classes.disclaimer}>
                            { getDisclaimerText({ managementCompanyName: company.name, submitBtnLabel: 'Book Tour' }) }
                        </div>
                    </Fragment>
                }
                { !client.person.email &&
                    <FormComponent
                        {...sharedProps}
                        FieldComponent={Input}
                        label="Email"
                        name="client.person.email"
                    />
                }
                <FormComponent
                    {...sharedProps }
                    FieldComponent={MobileSelect}
                    options={filterLayoutOptions(layoutOptions)}
                    label="Layouts"
                    name="client.layout"
                    multiple
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={MobileSelect}
                    options={PET_CHOICES}
                    label="I have"
                    name="client.pets"
                    multiple
                    exclusiveOptions={[NONE]}
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={MobileSelect}
                    options={AMENITY_CHOICES}
                    label="Amenities"
                    name="client.amenities"
                    multiple
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={MobileSelect}
                    options={moveInChoices}
                    label="Move In Date (approx)"
                    name="client.move_in_date"
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={MobileSelect}
                    options={this.state.budgetChoices}
                    label="Maximum Budget"
                    name="client.price_ceiling"
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={Input}
                    label="Other Occupants"
                    name="client.other_occupants"
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={Input}
                    label="Zip Code"
                    name="client.current_postal_code"
                />
                <FormComponent
                    {...sharedProps}
                    FieldComponent={TextArea}
                    label="Leave a Note"
                    name="appointment.prospect_notes"
                />
                {submitButton}
            </div>
        );
    }
}

Preferences.propTypes = {
    company: PropTypes.object.isRequired,
    fieldConfig: PropTypes.object.isRequired,
    showPeopleFields: PropTypes.bool,
    formErrors: PropTypes.object.isRequired,
    layoutOptions: PropTypes.array.isRequired,
    formData: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    updateFormData: PropTypes.func.isRequired,
    showValidationErrors: PropTypes.bool.isRequired,
    disabled: PropTypes.bool.isRequired,
    submitButton: PropTypes.node.isRequired,
    client: PropTypes.object,
    isSubmitting: PropTypes.bool,
};

export default injectSheet(styles)(Preferences);
