import { EActionType } from 'components/DashboardTable/types';
import {
  DeleteFilled,
  ExportOutlined,
  FileSyncOutlined,
  RedoOutlined,
  UploadOutlined,
} from '@ant-design/icons';

import {
  EnumButtonActions,
  TypeActionButtonsConfig,
} from 'components/DashboardTable/components/useActionsWithSelectedCards/types';
import { TypeCollectiveActionMessages } from './types';

type TypeActions =
  | 'exports'
  | 'updateClamAVDB'
  | 'delete'
  | 'updateEndpointConfig'
  | 'uploadFireWallConfig'
  | 'uploadIDSRules';

export const collectionButtonForDeviceCollectiveActions = (
  collectiveActionsMessages: TypeCollectiveActionMessages,
  collectiveHandleActions: {
    onExport: () => void;
    onUpdateClamAVDB: (file: FormData) => void;
    onCollectiveDeviceDelete: () => void;
    onCollectiveUploadFirewallConfiguration: (file: FormData) => void;
    onCollectiveFirewallIDSRulesUpdated: (file: FormData) => void;
    onCollectiveUpdateEndpointConfiguration: () => void;
  },
  hasSelected?: boolean,
): { [key in TypeActions]: TypeActionButtonsConfig } => {
  const {
    onExport,
    onUpdateClamAVDB,
    onCollectiveDeviceDelete,
    onCollectiveUploadFirewallConfiguration,
    onCollectiveFirewallIDSRulesUpdated,
    onCollectiveUpdateEndpointConfiguration,
  } = collectiveHandleActions;

  return {
    exports: {
      alwaysVisible: true,
      actionType: EActionType.SpecificAction,
      name: 'exports',
      action: onExport,
      translationsKey: EnumButtonActions.Exports,
      icon: ExportOutlined,
    },
    updateClamAVDB: {
      alwaysVisible: true,
      actionType: EActionType.Upload,
      name: 'updateClamAVDB',
      filename: 'file',
      translationsKey: EnumButtonActions.Upload,
      icon: FileSyncOutlined,
      action: (file) => onUpdateClamAVDB(file as FormData),
      popoverMessage: collectiveActionsMessages.updateClamAVDB.popoverMessage,
      confirmMessage: collectiveActionsMessages.updateClamAVDB.confirmMessage,
      accept: '.zip',
    },
    delete: {
      actionType: EActionType.SpecificAction,
      hasSelected,
      name: 'delete',
      action: onCollectiveDeviceDelete,
      translationsKey: EnumButtonActions.Delete,
      icon: DeleteFilled,
    },
    updateEndpointConfig: {
      actionType: EActionType.SpecificAction,
      name: 'updateEndpointConfig',
      translationsKey: EnumButtonActions.Update,
      icon: RedoOutlined,
      hasSelected,
      action: onCollectiveUpdateEndpointConfiguration,
      popoverMessage:
        collectiveActionsMessages.updateEndpointConfig.popoverMessage,
      confirmMessage:
        collectiveActionsMessages.updateEndpointConfig.confirmMessage,
    },
    uploadFireWallConfig: {
      actionType: EActionType.Upload,
      name: 'uploadFireWallConfig',
      filename: 'conffile',
      translationsKey: EnumButtonActions.Upload,
      icon: UploadOutlined,
      hasSelected,
      action: (file) =>
        onCollectiveUploadFirewallConfiguration(file as FormData),
      popoverMessage:
        collectiveActionsMessages.uploadFireWallConfig.popoverMessage,
      confirmMessage:
        collectiveActionsMessages.uploadFireWallConfig.confirmMessage,
      accept: '.xml',
    },
    uploadIDSRules: {
      actionType: EActionType.Upload,
      name: 'uploadIDSRules',
      translationsKey: EnumButtonActions.Upload,
      filename: 'rulesets',
      icon: RedoOutlined,
      hasSelected,
      action: (file) => onCollectiveFirewallIDSRulesUpdated(file as FormData),
      popoverMessage:
        collectiveActionsMessages.uploadFireWallRules.popoverMessage,
      confirmMessage:
        collectiveActionsMessages.uploadFireWallRules.confirmMessage,
      accept: '.tar.gz',
    },
  };
};

export const checkForExistingFields = (
  locale: (field: string) => string,
  fieldLocale: { [key: string]: string },
  existingFields: string[],
): { name: string; errors: string[] }[] => {
  return existingFields.map((field) => {
    return {
      name: field,
      errors: [locale(fieldLocale[field])],
    };
  });
};
