import React, { useContext, useEffect } from 'react';
import { AppMetadataStateContext } from '../../context/AppMetadataContext';
import { CreateCaseStateContext } from '../../context/CreateCaseContext';
import { RouteContext, RouteDispatchContext } from '../../context/RouteContext';
import { isCaseStateValid } from '../../reducers/CreateCaseHelpers';
import { AppRouteSections, RouteReducerConstants } from '../../reducers/RouteReducer';
import FileSelector from '../shared/fileUpload/FileSelector';
import FileEARule from './FileEARule';
import KtQuestions from './KtQuestions';

interface IProps {}

export default function ImproveCase(props: IProps) {
    const { createCaseState } = useContext(CreateCaseStateContext);
    const { routeState: {activeSection, isSectionValid} } = useContext(RouteContext);
    const dispatchToRouteReducer = useContext(RouteDispatchContext);
    const { appMetadataState: {
        loggedInUser,
        loggedInUserJwtToken,
        loggedInUsersAccount
    } } = useContext(AppMetadataStateContext);

    // Validations effect
    useEffect(() => {
        if (activeSection !== AppRouteSections.OPEN_CASE) { return; }
        const isSectionValidLocal = isCaseStateValid(createCaseState, loggedInUsersAccount.data.require_cgroup_on_create, loggedInUserJwtToken);
        if (isSectionValid === isSectionValidLocal) { return; }
        dispatchToRouteReducer({ type: RouteReducerConstants.setIsSectionValid, payload: { isSectionValid: isSectionValidLocal } });
    }, [activeSection, createCaseState, loggedInUsersAccount.data]);

    return (<>
        {loggedInUser.data.can_add_attachments && !loggedInUsersAccount.data.is_secured_support &&
            <><FileEARule /><FileSelector /></>
        }
        <KtQuestions />
    </>);
}
