import { COMMON, useTranslation } from 'core/i18n';
import {
  Button,
  ColumnsSelector,
  ConfirmText,
  Fields,
  Notification,
} from './types';
import { EnumButtonActions } from './DashboardTable/components/useActionsWithSelectedCards/types';
import { GenericGroupsDashboardLocale } from './GenericGroupsDashboard';

export type TypeGroupsTableMessages = {
  name: string;
  description: string;
  groupMembers: string;
  actions: string;
};

export type TypeCorrelationRulesLeftSidebarMessages = {
  groups: string;
  title: string;
  rules: string;
};

export type TypeNCIRCCLeftSidebarMessages = {
  title: string;
  information_exchange: string;
  organization_card: string;
  messages: string;
  bulletins: string;
};

export type TypeLicenseLeftSidebarMessages = {
  title: string;
  licenseCurrent: string;
  licenseActivation: string;
};

export type TypeAccountsLeftSidebarMessages = {
  title: string;
  list: string;
  groups: string;
  profile: string;
};

export type TypeLeftSidebarCommonMessages = {
  correlationRules: TypeCorrelationRulesLeftSidebarMessages;
  ncircc: TypeNCIRCCLeftSidebarMessages;
  license: TypeLicenseLeftSidebarMessages;
  accounts: TypeAccountsLeftSidebarMessages;
};

export type TypeConfirmMessages = {
  [ConfirmText.Ok]: string;
  [ConfirmText.Cancel]: string;
  [ConfirmText.Save]: string;
  [ConfirmText.Edit]: string;
};

export type TCommonMessages = {
  table: {
    selectedItemsCount: (itemsCount: number) => string;
  };
  confirmText: TypeConfirmMessages;
  fields: {
    [key: string]: string;
  };
  button: {
    [key: string]: string;
  };
  notification: {
    [Notification.PleaseCheckData]: string;
    [Notification.ProgressTitle]: string;
    [Notification.ProgressSubTitle]: (seconds: number) => string;
  };
  columnsSelector: string;
  groupsMessages: {
    table: TypeGroupsTableMessages;
    actions: {
      delete: string;
    };
    main: {
      [key: string]: string;
    };
    validation: {
      [key: string]: string;
    };
    notification: {
      success: {
        [key: string]: string;
      };
      error: {
        [key: string]: string;
      };
    };
  };
  collectiveActionsMessages: {
    [EnumButtonActions.Exports]: {
      title: string;
      popoverTitle: string;
    };
    [EnumButtonActions.Download]: {
      title: string;
      popoverTitle: string;
    };
    [EnumButtonActions.Delete]: {
      title: string;
      popoverTitle: string;
    };
    [EnumButtonActions.Imports]: {
      title: string;
      popoverTitle: string;
    };
    [EnumButtonActions.Update]: {
      title: string;
      popoverTitle: string;
    };
    [EnumButtonActions.Upload]: {
      title: string;
      popoverTitle: string;
    };
  };
  statusMessages: {
    connected: string;
    disconnected: string;
    unauthorized: string;
    error: string;
    connecting: string;
  };
  formValidators: {
    badPort: string;
    badPortsJson: string;
    badIp4Address: string;
  };
  genericGroupsDashboard: GenericGroupsDashboardLocale;
  leftSidebar: TypeLeftSidebarCommonMessages;
};

export type TypeCommonValidationMessages = {
  fieldNotBeEmpty: string;
  nameMaxLength: string;
  ipValidation: string;
};

export function useCommonMessages(): TCommonMessages {
  const { t } = useTranslation([COMMON]);

  return {
    table: {
      selectedItemsCount: (itemsCount) => {
        let s1 = ' ';
        let s2 = ' ';
        if (itemsCount > 1 && itemsCount <= 4) {
          s1 = 'но';
          s2 = 'а';
        }
        if (itemsCount > 4) {
          s1 = 'но';
          s2 = 'ов';
        }
        return t('common:app.common.table.selected_items', {
          s1,
          itemsCount,
          s2,
        });
      },
    },
    confirmText: {
      [ConfirmText.Ok]: t('common:app.common.confirmText.ok'),
      [ConfirmText.Cancel]: t('common:app.common.confirmText.cancel'),
      [ConfirmText.Save]: t('common:app.common.confirmText.save'),
      [ConfirmText.Edit]: t('common:app.common.confirmText.edit'),
    },
    fields: {
      [Fields.Name]: t('common:app.common.fields.name'),
      [Fields.Description]: t('common:app.common.fields.description'),
    },
    button: {
      [Button.AddGroup]: t('common:app.common.button.add_group'),
      [Button.AddAccount]: t('common:app.common.button.add_user'),
    },
    notification: {
      [Notification.PleaseCheckData]: t(
        'common:app.common.notification.please_check_data',
      ),
      [Notification.ProgressTitle]: t(
        'common:app.common.notification.progress.title',
      ),
      [Notification.ProgressSubTitle]: (seconds: number) =>
        t('common:app.common.notification.progress.sub_title', {
          seconds,
        }),
    },
    [ColumnsSelector.ColumnsSelector]: t('common:app.common.columnsSelector'),
    groupsMessages: {
      table: {
        name: t('common:app.common.groups.table.title'),
        description: t('common:app.common.groups.table.description'),
        groupMembers: t('common:app.common.groups.table.groups_members'),
        actions: t('common:app.common.groups.table.action'),
      },
      actions: {
        delete: t('common:app.common.single_actions.delete_group.title'),
      },
      main: {
        title: t('common:app.common.groups.drawer.title'),
        [Fields.Name]: t('common:app.common.fields.name'),
        [Fields.Description]: t('common:app.common.fields.description'),
      },
      validation: {
        name: t('common:app.common.groups.drawer.validation.name'),
        nameMaxLength: t(
          'common:app.common.groups.drawer.validation.name_max_length',
        ),
      },
      notification: {
        success: {
          post: t('common:app.common.groups.drawer.notification.success.post'),
          patch: t(
            'common:app.common.groups.drawer.notification.success.patch',
          ),
        },
        error: {
          duplicateName: t(
            'common:app.common.groups.drawer.notification.error.duplicateName',
          ),
        },
      },
    },
    collectiveActionsMessages: {
      exports: {
        title: t('common:app.common.collective_actions.exports.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.exports.popover_title',
        ),
      },
      delete: {
        title: t('common:app.common.collective_actions.delete.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.delete.popover_title',
        ),
      },
      download: {
        title: t('common:app.common.collective_actions.download.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.download.popover_title',
        ),
      },
      imports: {
        title: t('common:app.common.collective_actions.imports.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.imports.popover_title',
        ),
      },
      update: {
        title: t('common:app.common.collective_actions.update.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.update.popover_title',
        ),
      },
      upload: {
        title: t('common:app.common.collective_actions.upload.title'),
        popoverTitle: t(
          'common:app.common.collective_actions.upload.popover_title',
        ),
      },
    },
    statusMessages: {
      connected: t('common:app.common.table.column.status.connected'),
      disconnected: t('common:app.common.table.column.status.disconnected'),
      unauthorized: t('common:app.common.table.column.status.unauthorized'),
      error: t('common:app.common.table.column.status.error'),
      connecting: t('common:app.common.table.column.status.connecting'),
    },
    formValidators: {
      badPort: t('common:app.common.form_validators.bad_port'),
      badPortsJson: t('common:app.common.form_validators.bad_ports_json'),
      badIp4Address: t('common:app.common.form_validators.bad_ip4_address'),
    },
    genericGroupsDashboard: {
      id: t('common:app.common.generic_groups_dashboard.id'),
      name: t('common:app.common.generic_groups_dashboard.name'),
      description: t('common:app.common.generic_groups_dashboard.description'),
      actions: t('common:app.common.generic_groups_dashboard.actions'),
      creationSuccess: t(
        'common:app.common.generic_groups_dashboard.creation_success',
      ),
      creationError: t(
        'common:app.common.generic_groups_dashboard.creation_error',
      ),
      updateSuccess: t(
        'common:app.common.generic_groups_dashboard.update_success',
      ),
      updateError: t('common:app.common.generic_groups_dashboard.update_error'),
      deletionSuccess: t(
        'common:app.common.generic_groups_dashboard.deletion_success',
      ),
      deletionError: t(
        'common:app.common.generic_groups_dashboard.deletion_error',
      ),
      deletionConfirmation: t(
        'common:app.common.generic_groups_dashboard.deletion_confirmation',
      ),
      add: t('common:app.common.generic_groups_dashboard.add'),
      delete: t('common:app.common.generic_groups_dashboard.delete'),
      edit: t('common:app.common.generic_groups_dashboard.edit'),
      newEntity: t('common:app.common.generic_groups_dashboard.new_entity'),
      fieldIsRequired: t(
        'common:app.common.generic_groups_dashboard.field_is_required',
      ),
      maxFieldLengthReached: (limit: number) =>
        t(
          'common:app.common.generic_groups_dashboard.max_text_length_reached',
          { limit },
        ),
    },
    leftSidebar: {
      correlationRules: {
        groups: t('common:app.common.left_sidebar.groups'),
        title: t('common:app.common.left_sidebar.correlation_rules'),
        rules: t('common:app.common.left_sidebar.rules'),
      },
      ncircc: {
        title: t('common:app.common.left_sidebar.ncircc'),
        information_exchange: t(
          'common:app.common.left_sidebar.information_exchange',
        ),
        organization_card: t(
          'common:app.common.left_sidebar.organization_card',
        ),
        messages: t('common:app.common.left_sidebar.messages'),
        bulletins: t('common:app.common.left_sidebar.bulletins'),
      },
      license: {
        title: t('common:app.common.left_sidebar.license'),
        licenseCurrent: t('common:app.common.left_sidebar.licenseCurrent'),
        licenseActivation: t(
          'common:app.common.left_sidebar.licenseActivation',
        ),
      },
      accounts: {
        title: t('common:app.common.left_sidebar.users'),
        list: t('common:app.common.left_sidebar.list'),
        groups: t('common:app.common.left_sidebar.groups'),
        profile: t('common:app.common.left_sidebar.profile'),
      },
    },
  };
}

export const useCommonValidationMessages = (): TypeCommonValidationMessages => {
  const { t } = useTranslation([COMMON]);
  return {
    fieldNotBeEmpty: t('common:app.common.validation.field_not_be_empty'),
    nameMaxLength: t('common:app.common.validation.name_max_length'),
    ipValidation: t('common:app.common.validation.ip_validation'),
  };
};
