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

import Checkbox from './Checkbox';
import TextArea from './TextArea';
import TourDetails from './TourDetails';
import Button from './Button';
import FormComponent from './FormComponent';
import Input from './Input';
import MobileSelect from './MobileSelect';

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';

import styles from './styles/MobileScheduleTour';

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

    render(){
        const {
            fieldConfig,
            formData,
            formErrors,
            classes,
            updateFormData,
            showValidationErrors,
            building,
            location,
            mapboxToken,
            onClickBack,
            onNext,
            isSubmitting,
            client,
            company,
            layoutOptions,
            thirdPartySGT,
        } = this.props;

        const moveInChoices = getMoveInChoices(formData.date);

        const buttonText = thirdPartySGT ? `Book Tour with ${thirdPartySGT}`: 'Book tour';

        const sharedProps = {
            onChange: updateFormData,
            fieldConfig,
            formData,
            showValidationErrors,
            formErrors,
        };

        const showBrokerFields = !!formData.appointment.broker_booked;
        
    return (
        <div>
            <TourDetails
                building={building}
                location={location}
                mapboxToken={mapboxToken}
                onClickBack={onClickBack}
                formData={formData}
                className={classes.tourDetails}
                displayMap={false}
            />
            <div className={classes.row}>
                <div className={classes.header}>Enter preferences</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"
                />
                <Button isSubmitting={isSubmitting} className={classes.button} buttonText={buttonText} onClick={onNext}/>
            </div>
        </div>
    );
    
}
}

MobileScheduleTourPreferences.propTypes = {
    building: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    company: PropTypes.object.isRequired,
    fieldConfig: PropTypes.object.isRequired,
    formData: PropTypes.object.isRequired,
    formErrors: PropTypes.object.isRequired,
    layoutOptions: PropTypes.array.isRequired,
    location: PropTypes.object.isRequired,
    logo: PropTypes.string,
    mapboxToken: PropTypes.string,
    onNext: PropTypes.func.isRequired,
    updateFormData: PropTypes.func.isRequired,
    showValidationErrors: PropTypes.bool.isRequired,
    client: PropTypes.object,
    onClickBack: PropTypes.func,
    isSubmitting: PropTypes.bool,
    thirdPartySGT: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
};

export default injectSheet(styles)(MobileScheduleTourPreferences);
