import { useCallback } from 'react';

import { useDrawerLayer } from '@material-hu/components/layers/Drawers';

import CustomTaskForm from 'src/pages/dashboard/EmployeeLifecycle/Process/components/forms/StepActionForms/CustomTaskForm/provider';
import DocumentRequestFormProvider from 'src/pages/dashboard/EmployeeLifecycle/Process/components/forms/StepActionForms/DocumentRequestForm/provider';
import ExperienceSurveyFormProvider from 'src/pages/dashboard/EmployeeLifecycle/Process/components/forms/StepActionForms/ExperienceSurveyForm/provider';
import SurveyConfigForm from 'src/pages/dashboard/EmployeeLifecycle/Process/components/forms/StepActionForms/SurveyConfigForm/provider';
import {
  ACTION_TYPES,
  CUSTOM_TASK_FORM_ID,
  DOCUMENT_REQUEST_FORM_ID,
  STEP_ACTION_DRAWER_ID,
  SURVEY_CONFIG_FORM_ID,
  SURVEY_FORM_ID,
} from 'src/pages/dashboard/EmployeeLifecycle/Process/constants';
import { type ActionTypes } from 'src/pages/dashboard/EmployeeLifecycle/types';

const STEP_ACTION_DRAWER_PAPER_SX = {
  maxWidth: '800px',
  width: 'min(800px, 100%)',
};

export type UseActionDrawerParams = {
  processId: string;
  stepId: string;
  disabled?: boolean;
};

export type OpenActionDrawerFn = (type: ActionTypes, actionId?: string) => void;

/**
 * Returns a function to open the step action drawer (Custom Task or Survey).
 * Type and actionId are passed as arguments to openDrawer so a single hook call
 * can serve both create (menu) and edit (card) flows.
 */
export function useActionDrawer({
  processId,
  stepId,
  disabled = false,
}: UseActionDrawerParams) {
  const { openDrawer } = useDrawerLayer();

  const openDrawerFn = useCallback<OpenActionDrawerFn>(
    (type, actionId = '') => {
      if (type === ACTION_TYPES.FORM) {
        openDrawer(
          {
            content: (
              <SurveyConfigForm
                processId={processId}
                stepId={stepId}
                actionId={actionId}
                formId={SURVEY_CONFIG_FORM_ID}
                disabled={disabled}
              />
            ),
            wrapperProps: {
              sx: { zIndex: 400 },
              PaperProps: { sx: STEP_ACTION_DRAWER_PAPER_SX },
            },
          },
          STEP_ACTION_DRAWER_ID,
        );
      } else if (type === ACTION_TYPES.CUSTOM) {
        openDrawer(
          {
            content: (
              <CustomTaskForm
                processId={processId}
                stepId={stepId}
                actionId={actionId}
                formId={CUSTOM_TASK_FORM_ID}
                disabled={disabled}
              />
            ),
            wrapperProps: {
              disableEscapeKeyDown: true,
              PaperProps: { sx: STEP_ACTION_DRAWER_PAPER_SX },
            },
          },
          STEP_ACTION_DRAWER_ID,
        );
      } else if (type === ACTION_TYPES.DOCUMENT) {
        openDrawer(
          {
            content: (
              <DocumentRequestFormProvider
                processId={processId}
                stepId={stepId}
                actionId={actionId}
                formId={DOCUMENT_REQUEST_FORM_ID}
                disabled={disabled}
              />
            ),
            wrapperProps: {
              disableEscapeKeyDown: true,
              PaperProps: { sx: STEP_ACTION_DRAWER_PAPER_SX },
            },
          },
          STEP_ACTION_DRAWER_ID,
        );
      } else if (type === ACTION_TYPES.SURVEY) {
        openDrawer(
          {
            content: (
              <ExperienceSurveyFormProvider
                processId={processId}
                stepId={stepId}
                actionId={actionId}
                formId={SURVEY_FORM_ID}
                disabled={disabled}
              />
            ),
            wrapperProps: {
              disableEscapeKeyDown: true,
              PaperProps: { sx: STEP_ACTION_DRAWER_PAPER_SX },
            },
          },
          STEP_ACTION_DRAWER_ID,
        );
      }
    },
    [openDrawer, processId, stepId, disabled],
  );

  return { openDrawer: openDrawerFn };
}
