import { Checkbox, Input, Select } from 'antd';
import {
  FormFields,
  TypeCategory,
  TypeNCIRCCNotificationSendConfig,
  TypeNCIRCCNotificationShowConfig,
} from './types';
import { TypeSelectOptionType } from 'components/DashboardTable/types';
import { TypeNCIRCCNotificationMessage } from '../types';

import {
  ACTIVITY_STATUS_OPTIONS,
  AFFECTED_SYSTEM_CATEGORY_OPTION,
  ATTACK_TYPE_OPTION,
  IMPACT_OPTION,
  INCIDENT_TYPE_OPTION,
  TLP_STATUS_OPTIONS,
  VULNERABILITY_TYPE_OPTION,
} from './constants';

const { Option } = Select;
const { TextArea } = Input;

export const NCIRCCNotificationSendConfig = (
  translations: TypeNCIRCCNotificationMessage,
  categoryType: TypeCategory,
  isDisablesAfterSubmit: boolean,
): TypeNCIRCCNotificationSendConfig[] => {
  const {
    formFieldsMessages,
    formOptionsLabelMessages: {
      activityStatusOptionsMessages,
      attackTypeOptionsMessages,
      impactOptionMessages,
      incidentTypeOptionMessages,
      tlpStatusOptionsMessages,
      vulnerabilityTypeOptionMessages,
      affectedSystemCategoryMessages,
    },
    formItemsRules,
  } = translations;
  const typeOption = (): TypeSelectOptionType => {
    switch (categoryType) {
      case '0':
        return INCIDENT_TYPE_OPTION(incidentTypeOptionMessages);
      case '1':
        return ATTACK_TYPE_OPTION(attackTypeOptionsMessages);
      default:
        return VULNERABILITY_TYPE_OPTION(vulnerabilityTypeOptionMessages);
    }
  };

  const mainFieldsForAllFormConfig: TypeNCIRCCNotificationSendConfig[] = [
    {
      key: FormFields.TYPE,
      name: FormFields.TYPE,
      label: formFieldsMessages[FormFields.TYPE],
      jsxElem: (
        <Select disabled={isDisablesAfterSubmit}>
          {typeOption().map((item) => (
            <Option key={item.value}>{item.label}</Option>
          ))}
        </Select>
      ),
      rules: [{ required: true, message: formItemsRules[FormFields.TYPE] }],
    },
    {
      key: FormFields.ACTIVITY_STATUS,
      name: FormFields.ACTIVITY_STATUS,
      label: formFieldsMessages[FormFields.ACTIVITY_STATUS],
      jsxElem: (
        <Select disabled={isDisablesAfterSubmit}>
          {ACTIVITY_STATUS_OPTIONS(activityStatusOptionsMessages).map(
            (item) => (
              <Option key={item.value}>{item.label}</Option>
            ),
          )}
        </Select>
      ),
      rules: [
        { required: true, message: formItemsRules[FormFields.ACTIVITY_STATUS] },
      ],
    },
    {
      key: FormFields.TLP,
      name: FormFields.TLP,
      label: formFieldsMessages[FormFields.TLP],
      jsxElem: (
        <Select disabled={isDisablesAfterSubmit}>
          {TLP_STATUS_OPTIONS(tlpStatusOptionsMessages).map((item) => (
            <Option key={item.value}>{item.label}</Option>
          ))}
        </Select>
      ),
      rules: [{ required: true, message: formItemsRules[FormFields.TLP] }],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_NAME,
      name: FormFields.AFFECTED_SYSTEM_NAME,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_NAME],
      jsxElem: <Input disabled={isDisablesAfterSubmit} />,
      rules: [
        {
          required: true,
          message: formItemsRules[FormFields.AFFECTED_SYSTEM_NAME],
        },
      ],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_CATEGORY,
      name: FormFields.AFFECTED_SYSTEM_CATEGORY,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_CATEGORY],
      jsxElem: (
        <Select disabled={isDisablesAfterSubmit}>
          {AFFECTED_SYSTEM_CATEGORY_OPTION(affectedSystemCategoryMessages).map(
            (item) => (
              <Option key={item.value}>{item.label}</Option>
            ),
          )}
        </Select>
      ),
    },
    {
      key: FormFields.EVENT_DESCRIPTION,
      name: FormFields.EVENT_DESCRIPTION,
      label: formFieldsMessages[FormFields.EVENT_DESCRIPTION],
      jsxElem: <Input disabled={isDisablesAfterSubmit} />,
      rules: [
        {
          required: true,
          message: formItemsRules[FormFields.EVENT_DESCRIPTION],
        },
      ],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_CONNECTION,
      name: FormFields.AFFECTED_SYSTEM_CONNECTION,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_CONNECTION],
      valuePropName: 'checked',
      jsxElem: <Checkbox disabled={isDisablesAfterSubmit} />,
    },
    {
      key: FormFields.ASSISTANCE,
      name: FormFields.ASSISTANCE,
      label: formFieldsMessages[FormFields.ASSISTANCE],
      valuePropName: 'checked',
      jsxElem: <Checkbox disabled={isDisablesAfterSubmit} />,
    },
  ];
  const fieldsForIncidentAndAttackFormConfig: TypeNCIRCCNotificationSendConfig[] =
    [
      {
        key: FormFields.EFFECTS,
        name: FormFields.EFFECTS,
        type: 'collapsed',
        label: formFieldsMessages[FormFields.EFFECTS],
        collapsedItems: [
          {
            key: FormFields.INTEGRITY_IMPACT,
            name: FormFields.INTEGRITY_IMPACT,
            label: formFieldsMessages[FormFields.INTEGRITY_IMPACT],
            jsxElem: (
              <Select disabled={isDisablesAfterSubmit}>
                {IMPACT_OPTION(impactOptionMessages).map((item) => (
                  <Option key={item.value}>{item.label}</Option>
                ))}
              </Select>
            ),
            rules: [
              {
                required: true,
                message: formItemsRules[FormFields.INTEGRITY_IMPACT],
              },
            ],
          },
          {
            key: FormFields.AVAILABILITY_IMPACT,
            name: FormFields.AVAILABILITY_IMPACT,
            label: formFieldsMessages[FormFields.AVAILABILITY_IMPACT],
            jsxElem: (
              <Select disabled={isDisablesAfterSubmit}>
                {IMPACT_OPTION(impactOptionMessages).map((item) => (
                  <Option key={item.value}>{item.label}</Option>
                ))}
              </Select>
            ),
            rules: [
              {
                required: true,
                message: formItemsRules[FormFields.AVAILABILITY_IMPACT],
              },
            ],
          },
          {
            key: FormFields.CONFIDENTIALITY_IMPACT,
            name: FormFields.CONFIDENTIALITY_IMPACT,
            label: formFieldsMessages[FormFields.CONFIDENTIALITY_IMPACT],
            jsxElem: (
              <Select disabled={isDisablesAfterSubmit}>
                {IMPACT_OPTION(impactOptionMessages).map((item) => (
                  <Option key={item.value}>{item.label}</Option>
                ))}
              </Select>
            ),
            rules: [
              {
                required: true,
                message: formItemsRules[FormFields.CONFIDENTIALITY_IMPACT],
              },
            ],
          },
          {
            key: FormFields.CUSTOM_IMPACT,
            name: FormFields.CUSTOM_IMPACT,
            label: formFieldsMessages[FormFields.CUSTOM_IMPACT],
            jsxElem: <TextArea disabled={isDisablesAfterSubmit} rows={4} />,
          },
        ],
      },
    ];
  const showSelectedArray: {
    [key in TypeCategory]: TypeNCIRCCNotificationSendConfig[];
  } = {
    // Конфиг для отправки формы инцидента
    '0': [
      ...mainFieldsForAllFormConfig,
      ...fieldsForIncidentAndAttackFormConfig,
    ],
    // Конфиг для отправки формы атаки
    '1': [
      ...mainFieldsForAllFormConfig,
      ...fieldsForIncidentAndAttackFormConfig,
    ],
    // Конфиг для отправки формы уязвимости
    '2': [
      ...mainFieldsForAllFormConfig,
      {
        key: FormFields.EFFECTS,
        name: FormFields.EFFECTS,
        type: 'collapsed',
        label: formFieldsMessages[FormFields.ADDITIONAL],
        collapsedItems: [
          {
            key: FormFields.VULNERABILITY_ID,
            name: FormFields.VULNERABILITY_ID,
            label: formFieldsMessages[FormFields.VULNERABILITY_ID],
            jsxElem: <Input disabled={isDisablesAfterSubmit} />,
          },
          {
            key: FormFields.PRODUCT_CATEGORY,
            name: FormFields.PRODUCT_CATEGORY,
            label: formFieldsMessages[FormFields.PRODUCT_CATEGORY],
            jsxElem: <Input disabled={isDisablesAfterSubmit} />,
          },
        ],
      },
    ],
  };
  return showSelectedArray[categoryType];
};

export const NCIRCCNotificationShowConfig = (
  translations: TypeNCIRCCNotificationMessage,
  isDisabled: boolean,
  category: TypeCategory,
): TypeNCIRCCNotificationShowConfig => {
  const {
    formFieldsMessages,
    formOptionsLabelMessages: {
      activityStatusOptionsMessages,
      impactOptionMessages,
      tlpStatusOptionsMessages,
    },
    formItemsRules,
  } = translations;
  const mainFieldsForAllFormConfig: TypeNCIRCCNotificationSendConfig[] = [
    {
      key: FormFields.IDENTIFIER,
      name: FormFields.IDENTIFIER,
      label: formFieldsMessages[FormFields.IDENTIFIER],
      jsxElem: <Input disabled />,
    },
    {
      key: FormFields.TYPE,
      name: FormFields.TYPE,
      label: formFieldsMessages[FormFields.TYPE],
      jsxElem: <Input disabled />,
      rules: [
        {
          required: true,
          message: formItemsRules[FormFields.TYPE],
        },
      ],
    },
    {
      key: FormFields.ACTIVITY_STATUS,
      name: FormFields.ACTIVITY_STATUS,
      label: formFieldsMessages[FormFields.ACTIVITY_STATUS],
      jsxElem: (
        <Select disabled={isDisabled}>
          {ACTIVITY_STATUS_OPTIONS(activityStatusOptionsMessages).map(
            (item) => (
              <Option key={item.value}>{item.label}</Option>
            ),
          )}
        </Select>
      ),
      rules: [
        { required: true, message: formItemsRules[FormFields.ACTIVITY_STATUS] },
      ],
    },
    {
      key: FormFields.TLP,
      name: FormFields.TLP,
      label: formFieldsMessages[FormFields.TLP],
      jsxElem: (
        <Select disabled>
          {TLP_STATUS_OPTIONS(tlpStatusOptionsMessages).map((item) => (
            <Option key={item.value}>{item.label}</Option>
          ))}
        </Select>
      ),
      rules: [{ required: true, message: formItemsRules[FormFields.TLP] }],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_NAME,
      name: FormFields.AFFECTED_SYSTEM_NAME,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_NAME],
      jsxElem: <Input disabled />,
      rules: [
        {
          required: true,
          message: formItemsRules[FormFields.AFFECTED_SYSTEM_NAME],
        },
      ],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_CATEGORY,
      name: FormFields.AFFECTED_SYSTEM_CATEGORY,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_CATEGORY],
      jsxElem: <Input disabled />,
    },
    {
      key: FormFields.EVENT_DESCRIPTION,
      name: FormFields.EVENT_DESCRIPTION,
      label: formFieldsMessages[FormFields.EVENT_DESCRIPTION],
      jsxElem: <Input disabled={isDisabled} />,
      rules: [
        {
          required: true,
          message: formItemsRules[FormFields.EVENT_DESCRIPTION],
        },
      ],
    },
    {
      key: FormFields.AFFECTED_SYSTEM_CONNECTION,
      name: FormFields.AFFECTED_SYSTEM_CONNECTION,
      label: formFieldsMessages[FormFields.AFFECTED_SYSTEM_CONNECTION],
      valuePropName: 'checked',
      jsxElem: <Checkbox disabled />,
    },
    {
      key: FormFields.ASSISTANCE,
      name: FormFields.ASSISTANCE,
      label: formFieldsMessages[FormFields.ASSISTANCE],
      valuePropName: 'checked',
      jsxElem: <Checkbox disabled />,
    },
    {
      key: FormFields.NOTIFICATION_STATUS,
      name: FormFields.NOTIFICATION_STATUS,
      label: formFieldsMessages[FormFields.NOTIFICATION_STATUS],
      jsxElem: <Input disabled />,
    },
  ];
  const fieldsForIncidentAndAttackFormConfig: TypeNCIRCCNotificationSendConfig[] =
    [
      {
        key: FormFields.INTEGRITY_IMPACT,
        name: FormFields.INTEGRITY_IMPACT,
        label: formFieldsMessages[FormFields.INTEGRITY_IMPACT],
        jsxElem: (
          <Select disabled={isDisabled}>
            {IMPACT_OPTION(impactOptionMessages).map((item) => (
              <Option key={item.value}>{item.label}</Option>
            ))}
          </Select>
        ),
        rules: [
          {
            required: true,
            message: 'Выберите приоритет влияния на целостность',
          },
        ],
      },
      {
        key: FormFields.AVAILABILITY_IMPACT,
        name: FormFields.AVAILABILITY_IMPACT,
        label: formFieldsMessages[FormFields.AVAILABILITY_IMPACT],
        jsxElem: (
          <Select disabled={isDisabled}>
            {IMPACT_OPTION(impactOptionMessages).map((item) => (
              <Option key={item.value}>{item.label}</Option>
            ))}
          </Select>
        ),
        rules: [
          {
            required: true,
            message: formItemsRules[FormFields.AVAILABILITY_IMPACT],
          },
        ],
      },
      {
        key: FormFields.CONFIDENTIALITY_IMPACT,
        name: FormFields.CONFIDENTIALITY_IMPACT,
        label: formFieldsMessages[FormFields.CONFIDENTIALITY_IMPACT],
        jsxElem: (
          <Select disabled={isDisabled}>
            {IMPACT_OPTION(impactOptionMessages).map((item) => (
              <Option key={item.value}>{item.label}</Option>
            ))}
          </Select>
        ),
        rules: [
          {
            required: true,
            message: formItemsRules[FormFields.CONFIDENTIALITY_IMPACT],
          },
        ],
      },
      {
        key: FormFields.CUSTOM_IMPACT,
        name: FormFields.CUSTOM_IMPACT,
        label: formFieldsMessages[FormFields.CUSTOM_IMPACT],
        jsxElem: <TextArea rows={4} disabled={isDisabled} />,
      },
    ];

  const showSelectedArray: {
    [key in TypeCategory]: TypeNCIRCCNotificationShowConfig;
  } = {
    // Конфиг для просмотра формы инцидента
    '0': [
      ...mainFieldsForAllFormConfig,
      ...fieldsForIncidentAndAttackFormConfig,
    ],

    // Конфиг для просмотра формы атаки
    '1': [
      ...mainFieldsForAllFormConfig,
      ...fieldsForIncidentAndAttackFormConfig,
    ],

    // Конфиг для просмотра формы уязвимости
    '2': [
      ...mainFieldsForAllFormConfig,
      {
        key: FormFields.VULNERABILITY_ID,
        name: FormFields.VULNERABILITY_ID,
        label: formFieldsMessages[FormFields.VULNERABILITY_ID],
        jsxElem: <Input disabled={isDisabled} />,
      },
      {
        key: FormFields.PRODUCT_CATEGORY,
        name: FormFields.PRODUCT_CATEGORY,
        label: formFieldsMessages[FormFields.PRODUCT_CATEGORY],
        jsxElem: <Input disabled={isDisabled} />,
      },
    ],
  };
  return showSelectedArray[category];
};
