import React from 'react';
import PropTypes from 'prop-types';
import injectSheet from 'react-jss';

import TourDetails from './TourDetails';
import AppointmentPreference from './AppointmentPreference';
import Button from './Button';
import FormComponent from './FormComponent';

import {
    EMPLOYEEGROUP_FIELD_CONFIG_CHOICES,
    SELF_GUIDED_TOUR_OPTION_V3,
    GUIDED_TOUR_OPTION_V3,
    VIDEO_GUIDED_TOUR_OPTION_V3,
    SELF_GUIDED_TOUR_PROVIDER_GENERIC,
} from '../../constants/config';

import styles from './styles/DesktopScheduleTourContainer';

const { HIDDEN, REQUIRED } = EMPLOYEEGROUP_FIELD_CONFIG_CHOICES;

export function DesktopScheduleTourIntro(props) {
    const {
        building,
        classes,
        location,
        mapboxToken,
        previousAppointment,
        onNext,
        updateFormData,
        fieldConfig,
        formData,
        showValidationErrors,
        formErrors,
        employeeGroup,
        updateFieldConfig
    } = props;

    const getTourTypes = () => {
        const { self_guided_tour_settings, guided_tour_settings, self_guided_tour_provider } = employeeGroup;
        const tourTypeOptions = [];

        if (self_guided_tour_settings.tours_enabled) {
            const thirdPartySGT = self_guided_tour_provider !== SELF_GUIDED_TOUR_PROVIDER_GENERIC;
            const externalBookingEnabled = self_guided_tour_settings.self_guided_tours_external_bookings_enabled;
            if (externalBookingEnabled || thirdPartySGT) {
                tourTypeOptions.push(SELF_GUIDED_TOUR_OPTION_V3);
            }
        }
    
        if (guided_tour_settings.video_tours_enabled && guided_tour_settings.video_tours_external_bookings_enabled) {
            tourTypeOptions.push(VIDEO_GUIDED_TOUR_OPTION_V3);
        }
    
        if (guided_tour_settings.tours_enabled && guided_tour_settings.guided_tours_external_bookings_enabled) {
            tourTypeOptions.push(GUIDED_TOUR_OPTION_V3);
        }

        return tourTypeOptions;
    };

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

    return (
        <div className={classes.columns}>
            <TourDetails
                building={building}
                location={location}
                mapboxToken={mapboxToken}
                previousAppointment={previousAppointment}
                formData={formData}
            />
            <div className={classes.column}>
                <div className={classes.row}>
                    <div className={classes.header}>Select tour preference</div>
                </div>
                <FormComponent
                    {...sharedProps}
                    isAlwaysRequired
                    showLabel={false}
                    FieldComponent={
                        ({ onChange, value }) => getTourTypes().map((preference, i) => {
                            const handlePreferenceChange = (newValue) => {
                                const newConfig = {};
                                if (newValue === 'video') {
                                    newConfig['appointment.platform_of_choice'] = REQUIRED;
                                    newConfig['appointment.platform_of_choice_details'] = REQUIRED;
                                } else {
                                    newConfig['appointment.platform_of_choice'] = HIDDEN;
                                    newConfig['appointment.platform_of_choice_details'] = HIDDEN;
                                }

                                updateFieldConfig(newConfig);
                                onChange(newValue);
                            };

                            return <AppointmentPreference
                                        onClick={handlePreferenceChange}
                                        isSelected={preference.value === value}
                                        key={i}
                                        value={preference.value}
                                        icon={preference.icon}
                                        title={preference.label}
                                        subtitle={preference.helperText}
                                   />;
                        })
                    }
                    label="Tour type"
                    name="appointment.tour_type"
                />
                <Button buttonText="Next" onClick={onNext} disabled={!formData.appointment.tour_type}/>
            </div>
        </div>
    );
}

DesktopScheduleTourIntro.propTypes = {
    building: PropTypes.object.isRequired,
    employeeGroup: PropTypes.object.isRequired,
    classes: PropTypes.object.isRequired,
    fieldConfig: PropTypes.object.isRequired,
    formData: PropTypes.object.isRequired,
    formErrors: PropTypes.object.isRequired,
    location: PropTypes.object.isRequired,
    logo: PropTypes.string,
    mapboxToken: PropTypes.string,
    onNext: PropTypes.func.isRequired,
    updateFieldConfig: PropTypes.func.isRequired,
    updateFormData: PropTypes.func.isRequired,
    showValidationErrors: PropTypes.bool.isRequired,
    previousAppointment: PropTypes.object,
};

export default injectSheet(styles)(DesktopScheduleTourIntro);
