// Hooks
import { useEffect } from 'react';

// Types
import { RootStore } from 'stores/RootStore';
import {
  EndpointGroupName,
  TypeCardInfoEndpointMessages,
} from 'pages/DevicesDashboard/types';
import { TypeUseEndpointFormConfiguration } from './types';
import { FormInstance } from 'antd/lib/form';
import { TypeCommonValidationMessages } from 'components/i18n';

// Antd
import { Checkbox, Input, InputNumber, Select } from 'antd';

// Functions
import { useStore } from 'stores/StoreProvider';
import { validationIp } from 'pages/AssetsDashboard/AssetsCardInfo/validation';
import { RotationBlock } from './RotationBlock';

// Antd Items
const { TextArea } = Input;

export const useEndpointFormConfiguration = (
  locale: TypeCardInfoEndpointMessages,
  localeValidation: TypeCommonValidationMessages,
  form: FormInstance,
): TypeUseEndpointFormConfiguration => {
  // Stores
  const rootStore: RootStore = useStore();
  const devicesUiStore = rootStore.devicesUiStore;

  useEffect(() => {
    form.getFieldsValue();
  }, [form]);

  return {
    // Имя
    name: {
      key: 'name',
      name: 'name',
      label: locale.main.name,
      rules: [
        {
          required: true,
          message: localeValidation.fieldNotBeEmpty,
        },
        {
          max: 128,
          message: localeValidation.nameMaxLength,
        },
      ],
      extra: locale.main.nameExtra,
      component: (
        <Input
          disabled={devicesUiStore.isDisabledForm}
          placeholder={locale.main.namePlaceholder}
        />
      ),
    },

    // Ip
    ip: {
      key: 'ip',
      name: 'ip',
      label: locale.main.ip,
      rules: [
        {
          required: true,
          message: localeValidation.fieldNotBeEmpty,
        },
        () => ({
          async validator(_: unknown, value: string) {
            await validationIp(_, value, localeValidation.ipValidation);
          },
        }),
      ],
      extra: locale.main.ipExtra,
      component: (
        <Input
          disabled={devicesUiStore.isDisabledForm}
          placeholder={locale.main.ipPlaceholder}
        />
      ),
    },

    // Порт
    port: {
      key: 'port',
      name: 'port',
      label: locale.main.port,
      rules: [
        {
          required: true,
          message: localeValidation.fieldNotBeEmpty,
        },
      ],
      extra: locale.main.portExtra,
      component: (
        <InputNumber
          style={{ width: '100%' }}
          min={1500}
          max={65535}
          disabled={devicesUiStore.isDisabledForm}
          placeholder={locale.main.portPlaceholder}
        />
      ),
    },

    // Группа
    group: {
      key: 'group',
      name: 'group',
      label: locale.main.group,
      extra: locale.main.groupExtra,
      component: (
        <Select
          allowClear
          disabled={devicesUiStore.isDisabledForm}
          options={devicesUiStore.groupList.map((item) => ({
            value: item.id,
            label: item.name,
          }))}
        />
      ),
    },

    // Описание
    description: {
      key: 'description',
      name: 'description',
      label: locale.main.info,
      component: (
        <TextArea
          style={{ height: 30 }}
          disabled={devicesUiStore.isDisabledForm}
        />
      ),
    },

    // Включить контроль целостности
    integrityControlEnabled: {
      key: 'integrity_control_enabled',
      name: 'integrity_control_enabled',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.scanDirAtStartUp.icEnabled}
        </Checkbox>
      ),
    },

    // Период буферизации событий
    integrityControlTimeout: {
      key: 'integrity_control_timeout',
      label: locale.scanDirAtStartUp.icTimeout,
      name: 'integrity_control_timeout',
      extra: locale.scanDirAtStartUp.icTimeoutExtra,
      component: (
        <InputNumber
          min={0}
          max={86400}
          // Todo надо проверить нужно ли это
          // value={form.getFieldValue('integrity_control_timeout')}
          style={{ width: '100%' }}
          disabled={devicesUiStore.isDisabledForm}
        />
      ),
    },

    // Включить белый список
    whitelistEnabled: {
      key: 'whitelist_enabled',
      name: 'whitelist_enabled',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.whiteList.wlEnable}
        </Checkbox>
      ),
    },

    // Локальный администратор игнорирует белый список
    whitelistAdmin: {
      key: 'whitelist_admin',
      name: 'whitelist_admin',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.whiteList.wlAdmin}
        </Checkbox>
      ),
    },

    // Включить контроль устройств
    deviceControlEnabled: {
      key: 'device_control_enabled',
      name: 'device_control_enabled',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.deviceManagementSettings.dcEnabled}
        </Checkbox>
      ),
    },

    // Запретить доступ на чтение CD/DVD
    prohibitCdAccess: {
      key: 'prohibit_cd_access',
      name: 'prohibit_cd_access',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.deviceManagementSettings.prohibitCdAccess}
        </Checkbox>
      ),
      extra: locale.deviceManagementSettings.prohibitCdAccessExtra,
    },

    // Включить контроль USB устройств
    usbControlEnabled: {
      key: 'usb_control_enabled',
      name: 'usb_control_enabled',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.deviceManagementSettings.usbControlEnabled}
        </Checkbox>
      ),
      extra: locale.deviceManagementSettings.usbControlEnabledExtra,
    },

    // Настройки ротации
    rotationSettings: {
      key: 'rotation_settings',
      name: 'rotation_settings',
      valuePropName: 'checked',
      component: <RotationBlock disabled={devicesUiStore.isDisabledForm} />,
    },

    // Включить модуль антивируса
    antivirusEnabled: {
      key: 'antivirus_enabled',
      name: 'antivirus_enabled',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.antivirusSettings.clamavEnabled}
        </Checkbox>
      ),
    },

    // Удалять инфицированные файлы
    antivirusRemoveInfectedFiles: {
      key: 'antivirus_remove_infected_files',
      name: 'antivirus_remove_infected_files',
      valuePropName: 'checked',
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.antivirusSettings.clamavDeleteInfectedFiles}
        </Checkbox>
      ),
    },

    // Сканирование на вирусы
    antivirusStartScan: {
      key: 'antivirus_start_scan',
      name: 'antivirus_start_scan',
      valuePropName: 'checked',
      extra: locale.antivirusSettings.clamavStartScanExtra,
      component: (
        <Checkbox disabled={devicesUiStore.isDisabledForm}>
          {locale.antivirusSettings.clamavStartScan}
        </Checkbox>
      ),
    },
  };
};
