// Antd
import {
  AppstoreOutlined,
  AuditOutlined,
  IdcardOutlined,
  MenuFoldOutlined,
  MenuOutlined,
  MessageOutlined,
  FileOutlined,
  FileAddOutlined,
  TagOutlined,
  CheckSquareOutlined,
  TeamOutlined,
  TagsOutlined,
  UserOutlined,
} from '@ant-design/icons';

// Apps
import LeftSidebarItem from './LeftSidebarItem';

// Types
import {
  FONT_SIZE_LARGE_ICON,
  FONT_SIZE_SMALL_ICON,
} from 'components/constants';
import { Key } from 'react';
import { MenuProps } from 'antd/es/menu';
import { Page, paths } from 'pages/AppRoutes/constants';

import { TypeLeftSidebar } from 'components/Dashboard/types';
import { TypeLeftSidebarCommonMessages } from 'components/i18n';
type MenuItem = Required<MenuProps>['items'][number];

// Constants
const iconStyle = { color: '#334c8f' };
const MISSING_TEXT = 'MISSING_TEXT';

export const getLefSidebarItem = (
  label: JSX.Element | string,
  key: Key,
  icon: JSX.Element,
  disabled?: boolean,
  children?: MenuItem[],
): MenuItem => {
  return {
    key,
    icon,
    children,
    label,
    disabled,
  } as MenuItem;
};

export const leftSidebarMenuItem = (
  sidebarContent: TypeLeftSidebar[],
  collapsed: boolean,
): MenuItem[] =>
  sidebarContent.map((item) => {
    if (item.key === 0) {
      return getLefSidebarItem(
        <LeftSidebarItem title={item.title} path={item.path} isTitle />,
        item.key,
        <MenuOutlined style={iconStyle} />,
        true,
      );
    }
    if (item?.subMenu) {
      return getLefSidebarItem(
        item.title,
        item.key,
        <AppstoreOutlined
          style={{
            fontSize: collapsed ? FONT_SIZE_SMALL_ICON : FONT_SIZE_LARGE_ICON,
            ...iconStyle,
          }}
        />,
        item.disabled,
        item.subMenu.map((subItem) =>
          getLefSidebarItem(
            <LeftSidebarItem title={subItem.title} path={subItem.path} />,
            subItem.key,
            subItem.icon ? (
              subItem.icon
            ) : (
              <MenuFoldOutlined style={iconStyle} />
            ),
          ),
        ),
      );
    }
    return getLefSidebarItem(
      <LeftSidebarItem title={item.title} path={item.path} />,
      item.key,
      item.icon ? item.icon : <MenuFoldOutlined style={iconStyle} />,
      item.disabled,
    );
  });

export const makeLeftSidebarContent = (
  page: Page,
  locales: TypeLeftSidebarCommonMessages,
): Array<TypeLeftSidebar> => {
  const { correlationRules, ncircc, license, accounts } = locales;
  if (
    page === Page.CORRELATION_RULES_DASHBOARD ||
    page === Page.CORRELATION_RULES_GROUPS_DASHBOARD
  ) {
    return [
      {
        title: correlationRules.title ?? MISSING_TEXT,
        key: 0,
      },
      {
        title: correlationRules.rules ?? MISSING_TEXT,
        path: paths[Page.CORRELATION_RULES_DASHBOARD],
        key: 1,
        icon: <CheckSquareOutlined />,
      },
      {
        title: correlationRules.groups ?? MISSING_TEXT,
        path: paths[Page.CORRELATION_RULES_GROUPS_DASHBOARD],
        key: 2,
        icon: <TagOutlined />,
      },
    ];
  } else if (
    page === Page.LICENSE_CURRENT ||
    page === Page.LICENSE_ACTIVATION
  ) {
    return [
      {
        title: license.title ?? MISSING_TEXT,
        key: 0,
      },
      {
        title: license.licenseCurrent ?? MISSING_TEXT,
        path: paths[Page.LICENSE_CURRENT],
        key: 1,
        icon: <FileOutlined />,
      },
      {
        title: license.licenseActivation ?? MISSING_TEXT,
        path: paths[Page.LICENSE_ACTIVATION],
        key: 2,
        icon: <FileAddOutlined />,
      },
    ];
  } else if (page === Page.NCIRCC_ORGANIZATION_CARD) {
    return [
      {
        title: ncircc.title,
        key: 0,
      },
      {
        title: ncircc.organization_card,
        path: '/ncircc/organization_card',
        key: 1,
        icon: <IdcardOutlined />,
      },
      {
        title: ncircc.information_exchange,
        path: '/ncircc/information_exchange_message',
        key: 2,
        subMenu: [
          {
            title: ncircc.messages,
            path: '/ncircc/information_exchange_message',
            key: 3,
            icon: <MessageOutlined />,
          },
          // TODO: раскомментировать после релиза
          // {
          //   title: ncircc.bulletins,
          //   path: '/ncircc/information_exchange_bulletin',
          //   key: 4,
          //   icon: <AuditOutlined />,
          // },
        ],
      },
    ];
  } else if (page === Page.ACCOUNTS_USER || page === Page.ACCOUNTS_GROUP) {
    return [
      {
        title: accounts.title,
        key: 0,
      },
      {
        title: accounts.list,
        path: paths[Page.ACCOUNTS_USER],
        key: 1,
        icon: <TeamOutlined />,
      },
      // TODO: раскомментировать после релиза
      // {
      //   title: accounts.groups,
      //   path: paths[Page.ACCOUNTS_GROUP],
      //   key: 2,
      //   disabled: true,
      //   icon: <TagsOutlined />,
      // },
    ];
  } else if (page === Page.USER_PROFILE) {
    return [
      {
        title: accounts.title,
        key: 0,
      },
      {
        title: accounts.profile,
        path: paths[Page.USER_PROFILE],
        key: 1,
        icon: <UserOutlined />,
      },
      // TODO: раскомментировать после релиза
      // {
      //   title: accounts.groups,
      //   path: paths[Page.USER_GROUPS],
      //   key: 2,
      //   disabled: true,
      //   icon: <TagsOutlined />,
      // },
    ];
  } else {
    return [];
  }
};
