import { IconInbox } from '@material-hu/icons/tabler';
import List from '@material-hu/mui/List';

import HuSidebarNavItem from '@material-hu/components/design-system/Sidebar/components/NavItem';

import { useAuth } from 'src/contexts/JWTContext';
import useCustomServerTranslation from 'src/hooks/useCustomServerTranslation';
import { useBubbalooAggregate } from 'src/pages/dashboard/serviceManagement/hooks/useBubbalooCount';
import { serviceManagementRoutes } from 'src/pages/dashboard/serviceManagement/routes';
import { useLokaliseTranslation } from 'src/utils/i18n';

import itemsConfig from '../constants';
import useGetSidebarIcon from '../hooks/useSidebarIcon';
import { SidebarItemProps } from '../types';
import { getItemIsActive, getSubItemIsActive } from '../utils';

const { key, titleTranslationKey } = itemsConfig.serviceManagement;

export const MODULE_NAME = 'SERVICE_PORTAL';

const NavServiceManagementItem = (props: SidebarItemProps) => {
  const { isCollapsed = false, onClick, isActive: isActiveProp } = props;
  const { user } = useAuth();
  const { t } = useLokaliseTranslation(['service_management', 'time_off']);

  const title = useCustomServerTranslation({
    module: MODULE_NAME,
    defaultTranslationKey: titleTranslationKey,
    namespace: 'dashboard_sidebar_app',
  });

  const { Icon } = useGetSidebarIcon({
    defaultIcon: IconInbox,
    key: 'Service management',
  });

  const { global, personalSpace, agentPanel } = useBubbalooAggregate({
    isAgent: !!user?.isAgent,
  });

  const subItems = [
    {
      key: 'userServicePortal',
      path: serviceManagementRoutes.servicePortal(),
      title: t('time_off:personal_space'),
      notificationCount: personalSpace,
    },
    {
      key: 'agentServicePortal',
      path: serviceManagementRoutes.agentWorkspace(),
      title: t('agent_panel'),
      notificationCount: agentPanel,
    },
  ];

  const isActive =
    isActiveProp ?? subItems.some(item => getItemIsActive(item.path));

  const renderSubItems = () => {
    if (isCollapsed) return null;

    return (
      <List
        disablePadding
        sx={{
          '& > *:not(:last-child)': {
            mb: 0,
          },
        }}
      >
        {subItems.map((item, index) => (
          <HuSidebarNavItem
            key={item.key}
            title={item.title}
            path={item.path}
            depth={1}
            isLastChild={index === subItems.length - 1}
            isCollapsed={isCollapsed}
            active={getSubItemIsActive(item.path)}
            info={{ notificationCount: item.notificationCount }}
            onClick={onClick}
          />
        ))}
      </List>
    );
  };

  return (
    <>
      <HuSidebarNavItem
        key={key}
        depth={0}
        title={title}
        icon={<Icon />}
        path={serviceManagementRoutes.servicePortal()}
        info={{ notificationCount: global }}
        isCollapsed={isCollapsed}
        onClick={onClick}
        active={isActive}
        open={isActive}
      >
        {!isCollapsed && user?.isAgent && (
          <>
            <div
              id="service-management-main-item"
              style={{
                position: 'absolute',
                top: 0,
                left: 0,
                right: 0,
                height: '48px',
                pointerEvents: 'none',
              }}
            />
            {renderSubItems()}
          </>
        )}
      </HuSidebarNavItem>
    </>
  );
};

export default NavServiceManagementItem;
