// Libs
// -----------------------------------------------------------------------------
import { SelectProps } from 'antd';

// App
// -----------------------------------------------------------------------------
import {
  TypeFirewallFieldValueOptions,
  TypeFirewallFieldValueBoolean,
  CorrelationRulesActionRecord,
  TypeFirewallDevice,
  TypeFirewallFieldValues,
} from 'types/default';

export const firewallOptionsToSelectOptions = (
  values: TypeFirewallFieldValueOptions | undefined,
): SelectProps['options'] =>
  values
    ? Object.entries(values).map(([key, { value }]) => ({
        value: key,
        label: value,
      }))
    : [];

export const firewallOptionsToSingleDefaultValue = (
  values: TypeFirewallFieldValueOptions | undefined,
): string | undefined =>
  values
    ? Object.values(values).find(({ selected }) => selected === 1)?.value
    : undefined;

export const firewallOptionsToMultipleDefaultValues = (
  values: TypeFirewallFieldValueOptions | undefined,
): Array<string> =>
  values
    ? Object.values(values)
        .filter(({ selected }) => selected === 1)
        .map(({ value }) => value)
    : [];

export const firewallBooleanToRealBoolean = (
  value: TypeFirewallFieldValueBoolean | undefined,
): boolean | undefined => {
  if (value === '0') {
    return false;
  } else if (value === '1') {
    return true;
  } else {
    return undefined;
  }
};

export const makeFirewallActionFormValues = (
  firewallId: string,
  firewallDevice: TypeFirewallDevice,
  fieldValues: TypeFirewallFieldValues,
): CorrelationRulesActionRecord => {
  return {
    type: 'firewall',
    gateway: '',
    armaif: firewallId,
    sensor: {
      ip: firewallDevice.ip ?? '',
      scheme: 'https',
      key: firewallDevice.key ?? '',
      secret: firewallDevice.secret ?? '',
    },
    enabled: firewallBooleanToRealBoolean(fieldValues.rule.enabled) ?? false,
    quick: firewallBooleanToRealBoolean(fieldValues.rule.quick) ?? false,
    log: firewallBooleanToRealBoolean(fieldValues.rule.log) ?? false,
    interface:
      firewallOptionsToMultipleDefaultValues(fieldValues.rule.interface) ?? [],
    direction:
      firewallOptionsToSingleDefaultValue(fieldValues.rule.direction) ?? '',
    sequence: fieldValues.rule.sequence ?? '',
    action: firewallOptionsToSingleDefaultValue(fieldValues.rule.action) ?? '',
    ipprotocol:
      firewallOptionsToSingleDefaultValue(fieldValues.rule.ipprotocol) ?? '',
    protocol:
      firewallOptionsToSingleDefaultValue(fieldValues.rule.protocol) ?? '',
    source_net: fieldValues.rule.source_net ?? '',
    source_port: fieldValues.rule.source_port ?? '',
    source_not:
      firewallBooleanToRealBoolean(fieldValues.rule.source_not) ?? false,
    destination_net: fieldValues.rule.destination_net ?? '',
    destination_port: fieldValues.rule.destination_port ?? '',
    destination_not:
      firewallBooleanToRealBoolean(fieldValues.rule.destination_not) ?? false,
    description: fieldValues.rule.description ?? '',
  };
};
