
import { IStrataAccountUser } from '@rh-support/api';
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 CaseContactSelector from './CaseContactSelector';
import CaseLanguageSelector from './CaseLanguageSelector';

export interface IProps {}

export interface IAccountUser extends IStrataAccountUser { selectedLabel: string; }

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

    // 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]);

    const handleFormSubmit = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault();
        // simulating click
        nextButtonRef.current.click();
    };

    return (<form onSubmit={handleFormSubmit}>
        <CaseContactSelector />
        <CaseLanguageSelector />
    </form>);
}
