import PropTypes from 'prop-types';
import React, { Suspense, lazy } from 'react';

import {
    FIND_APPOINTMENT,
    SELECT_APPOINTMENT,
    CLIENT_INFORMATION,
    CONFIRMATION,
    APPOINTMENT_CONFIRMED,
} from '../../constants/formSections';
import { WIDGET_TYPES, WIDGET_TYPE_LEAD_CAPTURE, WIDGET_TYPE_LEAD_CAPTURE_APPOINTMENT } from '../../constants/config';
import { Loading } from './AppContainer';


export const AppointmentConfirmed = lazy(() => import('./AppointmentConfirmed'));
export const FindAppointment = lazy(() => import('./FindAppointment'));
export const SelectAppointment = lazy(() => import('./SelectAppointment'));
export const ClientInformation = lazy(() => import('./ClientInformation'));
export const ConfirmationAppointment = lazy(() => import('./ConfirmationAppointment'));
export const ConfirmationLeadCapture = lazy(() => import('./ConfirmationLeadCapture'));

export default class SectionContainer extends React.Component {
    render() {
        const { section, formData, timeChoices, ...restProps } = this.props;

        const SECTIONS = {
            [FIND_APPOINTMENT]: () => <FindAppointment timeChoices={timeChoices} {...restProps} />,
            [SELECT_APPOINTMENT]: () => <SelectAppointment timeChoices={timeChoices} {...restProps} />,
            [CLIENT_INFORMATION]: () => <ClientInformation initialValues={formData} {...restProps} />,
            [CONFIRMATION]: () => {
                switch (this.context.widgetType) {
                    case WIDGET_TYPE_LEAD_CAPTURE:
                        return <ConfirmationLeadCapture location={this.props.appointment.location} />;
                    case WIDGET_TYPE_LEAD_CAPTURE_APPOINTMENT:
                        return (
                            <ConfirmationAppointment
                                appointment={this.props.appointment}
                                confirmationEmail={this.props.clientEmail}
                            />
                        );
                }
            },
            [APPOINTMENT_CONFIRMED]: () => <AppointmentConfirmed email={this.props.clientEmail} {...restProps} />
        };

        const renderSection = SECTIONS[section];

        if (renderSection) {
            return <Suspense fallback={<Loading />}>
                {renderSection()}
            </Suspense>;
        }

        return null;
    }
}

SectionContainer.propTypes = {
    section: PropTypes.string,
    setFormSection: PropTypes.func.isRequired,
    setFormData: PropTypes.func.isRequired,
    setAvailableTimes: PropTypes.func.isRequired,
    setNextAvailableDate: PropTypes.func.isRequired,
    timeChoices: PropTypes.array.isRequired,
    employeeGroup: PropTypes.object,
    appointment: PropTypes.shape({
        start: PropTypes.string,
        expiration_date: PropTypes.string,
        location: PropTypes.string,
    }).isRequired,
    clientEmail: PropTypes.string,
    formData: PropTypes.object,
    onComplete: PropTypes.func.isRequired,
    customColor: PropTypes.string,
    tourTypeOptions: PropTypes.array,
    platformOfChoiceOptions: PropTypes.array,
    nextAvailableDate: PropTypes.string,
    isThirdPartySelfGuidedTour: PropTypes.bool,
};

SectionContainer.contextTypes = {
    widgetType: PropTypes.oneOf(WIDGET_TYPES).isRequired,
};
