import { MAX_INT_DB } from '@/modules/@common/constants/db';
import {
  CONTENT_FILTERS_TAB,
  CUSTOM_PLACEHOLDER_TAB,
  DELAY_MAX_VALUE,
  DELAY_MIN_VALUE,
  FLOATING_TAB,
  FLOATING_WIDTH_MAX,
  FLOATING_WIDTH_MIN,
  GENERAL_TAB,
  LOOK_AND_FEEL_TAB,
  NAME_MAX_LENGTH,
  NAME_MIN_LENGTH,
  PLAYBACK_TAB,
  PLAYLIST_LIMIT_MAX,
  PLAYLIST_LIMIT_MIN,
  SKIP_AD_APPEARS_AFTER_MS_MAX,
  SKIP_AD_APPEARS_AFTER_MS_MIN,
  VIDEO_ADS_TAB,
  WIDTH_MAX,
  WIDTH_MIN,
} from '@/modules/players/Editor/constants';

import { isInRange } from '@/modules/@common/helpers/number';
import { isValidUrl } from '@/modules/@common/helpers/string';
import { isVerticalType } from '@/modules/players/Editor/helpers/index';

export const validationScheme = [
  {
    fieldName: 'publisher',
    tabId: GENERAL_TAB,
    validation: (value) => {
      if (!value) {
        return 'Field cannot be empty';
      }
      return '';
    },
  },
  {
    fieldName: 'alias',
    tabId: GENERAL_TAB,
    validation: (title) => {
      const value = title?.trim();

      if (!value) {
        return 'Field cannot be empty';
      }
      if (value.length < NAME_MIN_LENGTH) {
        return `Field cannot be less then ${NAME_MIN_LENGTH} symbols`;
      }
      if (value.length > NAME_MAX_LENGTH) {
        return `Field cannot be greater then ${NAME_MAX_LENGTH} symbols`;
      }

      return '';
    },
  },
  {
    fieldName: 'publisherDomain',
    tabId: GENERAL_TAB,
    validation: (domain) => {
      if (!domain) {
        return 'Field cannot be empty';
      }
      return '';
    },
  },
  {
    fieldName: 'playerLreLogo.link',
    tabId: LOOK_AND_FEEL_TAB,
    validation: (url, state) => {
      const hasLogo = state.playerLreLogo?.enabled;
      const value = url?.trim();

      if (hasLogo && !value) {
        return 'Field cannot be empty';
      }
      if (value && !isValidUrl(value)) {
        return 'Wrong link format';
      }
      return '';
    },
  },
  {
    fieldName: 'swipeAnimationMaxVisits',
    tabId: LOOK_AND_FEEL_TAB,
    validation: (value, state) => {
      if (isVerticalType(state.playerType?.id)) {
        const min = 1;
        const max = 9999;
        if (!isInRange(value, min, max)) {
          return `${min}-${max}`;
        }
      }
      return '';
    },
  },
  {
    fieldName: 'swipeAnimationDuration',
    tabId: LOOK_AND_FEEL_TAB,
    validation: (value, state) => {
      if (isVerticalType(state.playerType?.id)) {
        const min = 1;
        const max = 99;
        if (!isInRange(value, min, max)) {
          return `${min}-${max}`;
        }
      }
      return '';
    },
  },

  {
    fieldName: 'playerWidth',
    tabId: GENERAL_TAB,
    validation: (width) => {
      if (width < WIDTH_MIN) {
        return `Cannot be less than ${WIDTH_MIN}px`;
      }

      if (width > WIDTH_MAX) {
        return `Cannot be greater than ${WIDTH_MAX}px`;
      }

      return '';
    },
  },
  {
    fieldName: 'playerWidthMobile',
    tabId: GENERAL_TAB,
    validation: (width) => {
      if (width < WIDTH_MIN) {
        return `Cannot be less than ${WIDTH_MIN}px`;
      }

      if (width > WIDTH_MAX) {
        return `Cannot be greater than ${WIDTH_MAX}px`;
      }

      return '';
    },
  },
  {
    fieldName: 'floatingDesktopWidth',
    tabId: FLOATING_TAB,
    validation: (width) => {
      if (width < FLOATING_WIDTH_MIN) {
        return `Cannot be less than ${FLOATING_WIDTH_MIN}px`;
      }

      if (width > FLOATING_WIDTH_MAX) {
        return `Cannot be greater than ${FLOATING_WIDTH_MAX}px`;
      }

      return '';
    },
  },
  {
    fieldName: 'floatingMobileWidth',
    tabId: FLOATING_TAB,
    validation: (width) => {
      if (width < FLOATING_WIDTH_MIN) {
        return `Cannot be less than ${FLOATING_WIDTH_MIN}px`;
      }

      if (width > FLOATING_WIDTH_MAX) {
        return `Cannot be greater than ${FLOATING_WIDTH_MAX}px`;
      }

      return '';
    },
  },
  {
    fieldName: 'floatingDesktopDelay',
    tabId: FLOATING_TAB,
    validation: (value) => {
      if (!value && typeof value !== 'number') {
        return 'Field cannot be empty';
      }
      if (value < DELAY_MIN_VALUE) {
        return `Cannot be less than ${DELAY_MIN_VALUE}`;
      }
      if (value > DELAY_MAX_VALUE) {
        return `Cannot be greater than ${DELAY_MAX_VALUE}`;
      }

      return '';
    },
  },
  {
    fieldName: 'floatingMobileDelay',
    tabId: FLOATING_TAB,
    validation: (value) => {
      if (!value && typeof value !== 'number') {
        return 'Field cannot be empty';
      }
      if (value < DELAY_MIN_VALUE) {
        return `Cannot be less than ${DELAY_MIN_VALUE}`;
      }
      if (value > DELAY_MAX_VALUE) {
        return `Cannot be greater than ${DELAY_MAX_VALUE}`;
      }

      return '';
    },
  },
  {
    fieldName: 'playerContentOwnerFeeds',
    tabId: CONTENT_FILTERS_TAB,
    validation: (sources, state) => {
      if (!state.useDefaultContentOwner && (!sources || !sources.length)) {
        return 'Field cannot be empty';
      }
      return '';
    },
  },
  {
    fieldName: 'playerFeedLanguages',
    tabId: CONTENT_FILTERS_TAB,
    validation: (languages) => {
      if (!languages || !languages.length) {
        return 'Field cannot be empty';
      }
      return '';
    },
  },
  {
    fieldName: 'playlistLimit',
    tabId: PLAYBACK_TAB,
    validation: (width) => {
      if (!isInRange(width, PLAYLIST_LIMIT_MIN, PLAYLIST_LIMIT_MAX)) {
        return `${PLAYLIST_LIMIT_MIN}-${PLAYLIST_LIMIT_MAX}`;
      }
      return '';
    },
  },
  {
    fieldName: 'clipAutomaticSkipTimeToPresent',
    tabId: PLAYBACK_TAB,
    validation: (width, state) => {
      const min = state.clipAutomaticSkipOptions.clipAutomaticSkipTimeToPresent.minValue;
      const max = state.clipAutomaticSkipOptions.clipAutomaticSkipTimeToPresent.maxValue;
      if (!isInRange(width, min, max)) {
        return `${min}-${max}`;
      }
      return '';
    },
  },
  {
    fieldName: 'clipAutomaticSkipTimeToSkip',
    tabId: PLAYBACK_TAB,
    validation: (width, state) => {
      const min = state.clipAutomaticSkipOptions.clipAutomaticSkipTimeToSkip.minValue;
      const max = state.clipAutomaticSkipOptions.clipAutomaticSkipTimeToSkip.maxValue;
      if (!isInRange(width, min, max)) {
        return `${min}-${max}`;
      }
      return '';
    },
  },
  {
    fieldName: 'preRollWaitMs',
    tabId: VIDEO_ADS_TAB,
    validation: (width, state) => {
      const min = state.preRollOptions.preRollWaitMs.minValue;
      const max = state.preRollOptions.preRollWaitMs.maxValue;
      if (!isInRange(width, min, max)) {
        return `${min}-${max}`;
      }
      return '';
    },
  },
  {
    fieldName: 'firstAdRequestDelay',
    tabId: VIDEO_ADS_TAB,
    validation: (width) => {
      const min = 0;
      const max = MAX_INT_DB;
      if (!isInRange(width, min, max)) {
        return `${min}-${max}`;
      }
      return '';
    },
  },
  {
    fieldName: 'skipAdAppearAfter',
    tabId: VIDEO_ADS_TAB,
    validation: (width) => {
      const min = SKIP_AD_APPEARS_AFTER_MS_MIN;
      const max = SKIP_AD_APPEARS_AFTER_MS_MAX;
      if (!isInRange(width, min, max)) {
        return `${min}-${max}`;
      }
      return '';
    },
  },
  {
    fieldName: 'placeholderSelector',
    tabId: CUSTOM_PLACEHOLDER_TAB,
    validation: (value, allProps) => {
      if (allProps.customPlaceholderEnable && !allProps.selectors.length) {
        return 'Add at least one selector';
      }
      return '';
    },
  },
  // dynamic fields
  {
    fieldName: 'selectors',
    dynamic: true,
    tabId: CUSTOM_PLACEHOLDER_TAB,

    validation: (items) =>
      items.reduce((acc, field) => {
        const trimedValue = field.selector.trim();
        let error = '';
        if (!trimedValue) {
          error = 'Field cannot be empty';
        } else if (items.filter((item) => item.id !== field.id).some((item) => item.selector.trim() === trimedValue)) {
          error = 'Field has duplicate';
        }
        acc[field.id] = {
          selector: error,
        };

        return acc;
      }, {}),
  },
];
