import {
  TYPE_INTELLIGENT,
  TYPE_INTELLIGENT_AMP,
  TYPE_OUTSTREAM,
  TYPE_STORIES,
  TYPE_STORIES_AMP,
  TYPE_VERTICAL,
} from '@/modules/@common/constants/playerTypes';
import {
  ALL_FEEDS_ID,
  DISABLED_FLOW_TERM_OPTION,
  FLOATING_AMP_STICKY_AD_BAR,
  FLOATING_DISABLED,
  VIDEO_MONETIZATION_ENABLED,
} from '@/modules/players/Editor/constants';

import * as selectors from '../redux/selectors';
import { isAmpTypes } from '@/modules/players/Editor/helpers';

const commonPlayerFields = [
  'type',
  'publisherId',
  'alias',
  'publisherDomainId',
  'comments',
  'playerWidthUnit',
  'playerWidth',
  'playerWidthMobileUnit',
  'playerWidthMobile',
  'playerAspectRatio',
  'floatingMobileEnabled',
  'delayCloseIconEnabledMobile',
  'inviewAdIntervalMobile',
  'notInviewAdIntervalMobile',
  'adIndicator',
  'adIndicatorText',
  'skipAdButton',
  'skipAdAppearAfter',
  'firstAdRequestDelay',
  'adManagerMobile',
  'fallbackDisplayMonetizationMobile',
  'enableVideoTargeting',
  'videoFormatByDevice',
  'customPlaceholderEnable',
  'selectors',
];

const playerTypesFields = {
  [TYPE_INTELLIGENT]: [
    'adManagerDesktop',
    'fallbackDisplayMonetizationDesktop',
    'adsOnLoopOff',
    'allFeeds',
    'autoplayDesktop',
    'autoplayMobile',
    'boosts',
    'brandingLeft',
    'brandingRight',
    'carouselEnabled',
    'carouselEnabledMobile',
    'carouselOrientation',
    'chapteringMenuNavigation',
    'chapteringTimeline',
    'clipAutomaticSkipEnabled',
    'clipAutomaticSkipTimeToPresent',
    'clipAutomaticSkipTimeToSkip',
    'clipNotInViewPlayAdsMobile',
    'clipNotInViewPlayAdsDesktop',
    'closedCaptioning',
    'displayClipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsMobile',
    'displayInviewAdInterval',
    'displayInviewAdIntervalMobile',
    'displayMonetizationDesktop',
    'displayMonetizationMobile',
    'displayNotInviewAdInterval',
    'displayNotInviewAdIntervalMobile',
    'editorialEnabled',
    'editorialType',
    'ffRewind',
    'floatingDesktopDelay',
    'floatingDesktopEnabled',
    'floatingDesktopMode',
    'floatingDesktopPosition',
    'floatingDesktopWidth',
    'floatingMobileDelay',
    'floatingMobileMode',
    'floatingMobilePosition',
    'floatingMobileWidth',
    'flowTermId',
    'fullScreen',
    'inviewAdInterval',
    'leftBrandingColor',
    'leftBrandingSize',
    'loopPlaylist',
    'maxAdsPerClip',
    'maxAdsPerClipChecked',
    'notInviewAdInterval',
    'playbackSpeedControl',
    'playerBrandSafeties',
    'playerContentOwnerFeeds',
    'playerEditorialPlaylistId',
    'playerFeedLanguages',
    'playerLreLogo',
    'playlistLimit',
    'preRoll',
    'preRollWaitMs',
    'progressBarColor',
    'settingsMenuHighlightColor',
    'shufflePlaylist',
    'socialLikeButton',
    'socialViewButton',
    'startWithSound',
    'startWithSoundMobile',
    'theaterMode',
    'timestampTooltipColor',
    'useDefaultContentOwner',
    'videoResolutionControl',
    'volumeBarColor',
    'xRayCampaignsEnabled',
  ],
  [TYPE_INTELLIGENT_AMP]: [
    'adsOnLoopOff',
    'allFeeds',
    'autoplayMobile',
    'boosts',
    'brandingLeft',
    'brandingRight',
    'chapteringMenuNavigation',
    'chapteringTimeline',
    'clipNotInViewPlayAdsMobile',
    'clipAutomaticSkipEnabled',
    'clipAutomaticSkipTimeToPresent',
    'clipAutomaticSkipTimeToSkip',
    'closedCaptioning',
    'displayClipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsMobile',
    'displayInviewAdInterval',
    'displayInviewAdIntervalMobile',
    'displayMonetizationDesktop',
    'displayMonetizationMobile',
    'displayNotInviewAdInterval',
    'displayNotInviewAdIntervalMobile',
    'editorialEnabled',
    'editorialType',
    'ffRewind',
    'flowTermId',
    'fullScreen',
    'leftBrandingColor',
    'leftBrandingSize',
    'loopPlaylist',
    'maxAdsPerClip',
    'maxAdsPerClipChecked',
    'playbackSpeedControl',
    'playerBrandSafeties',
    'playerContentOwnerFeeds',
    'playerEditorialPlaylistId',
    'playerFeedLanguages',
    'playerLreLogo',
    'playlistLimit',
    'preRoll',
    'preRollWaitMs',
    'progressBarColor',
    'settingsMenuHighlightColor',
    'shufflePlaylist',
    'socialLikeButton',
    'socialViewButton',
    'startWithSoundMobile',
    'timestampTooltipColor',
    'useDefaultContentOwner',
    'videoResolutionControl',
    'volumeBarColor',
  ],
  [TYPE_STORIES]: [
    'adManagerDesktop',
    'fallbackDisplayMonetizationDesktop',
    'adsOnLoopOff',
    'allFeeds',
    'autoplayDesktop',
    'autoplayMobile',
    'brandingLeft',
    'brandingRight',
    'clipAutomaticSkipEnabled',
    'clipAutomaticSkipTimeToPresent',
    'clipAutomaticSkipTimeToSkip',
    'clipNotInViewPlayAdsMobile',
    'clipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsMobile',
    'displayInviewAdInterval',
    'displayInviewAdIntervalMobile',
    'displayMonetizationDesktop',
    'displayMonetizationMobile',
    'displayNotInviewAdInterval',
    'displayNotInviewAdIntervalMobile',
    'floatingDesktopDelay',
    'floatingDesktopEnabled',
    'floatingDesktopMode',
    'floatingDesktopPosition',
    'floatingDesktopWidth',
    'floatingMobileDelay',
    'floatingMobileMode',
    'floatingMobilePosition',
    'floatingMobileWidth',
    'inviewAdInterval',
    'loopPlaylist',
    'notInviewAdInterval',
    'playerContentOwnerFeeds',
    'playerLreLogo',
    'playlistLimit',
    'preRoll',
    'preRollWaitMs',
    'prerollOnly',
    'prerollOnlyClipsCount',
  ],
  [TYPE_STORIES_AMP]: [
    'adsOnLoopOff',
    'allFeeds',
    'autoplayMobile',
    'brandingLeft',
    'brandingRight',
    'clipNotInViewPlayAdsMobile',
    'clipAutomaticSkipEnabled',
    'clipAutomaticSkipTimeToPresent',
    'clipAutomaticSkipTimeToSkip',
    'displayClipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsMobile',
    'displayInviewAdInterval',
    'displayInviewAdIntervalMobile',
    'displayMonetizationDesktop',
    'displayMonetizationMobile',
    'displayNotInviewAdInterval',
    'displayNotInviewAdIntervalMobile',
    'loopPlaylist',
    'playerContentOwnerFeeds',
    'playerLreLogo',
    'playlistLimit',
    'preRoll',
    'preRollWaitMs',
    'prerollOnly',
    'prerollOnlyClipsCount',
  ],
  [TYPE_VERTICAL]: [
    'adManagerDesktop',
    'fallbackDisplayMonetizationDesktop',
    'adsOnLoopOff',
    'allFeeds',
    'autoplayDesktop',
    'autoplayMobile',
    'boosts',
    'brandingLeft',
    'brandingRight',
    'carouselEnabled',
    'carouselEnabledMobile',
    'chapteringMenuNavigation',
    'chapteringTimeline',
    'clipAutomaticSkipEnabled',
    'clipAutomaticSkipTimeToPresent',
    'clipAutomaticSkipTimeToSkip',
    'clipNotInViewPlayAdsMobile',
    'clipNotInViewPlayAdsDesktop',
    'closedCaptioning',
    'displayClipNotInViewPlayAdsDesktop',
    'displayClipNotInViewPlayAdsMobile',
    'displayInviewAdInterval',
    'displayInviewAdIntervalMobile',
    'displayMonetizationDesktop',
    'displayMonetizationMobile',
    'displayNotInviewAdInterval',
    'displayNotInviewAdIntervalMobile',
    'editorialEnabled',
    'editorialType',
    'ffRewind',
    'floatingDesktopDelay',
    'floatingDesktopEnabled',
    'floatingDesktopMode',
    'floatingDesktopPosition',
    'floatingDesktopWidth',
    'floatingMobileDelay',
    'floatingMobileMode',
    'floatingMobilePosition',
    'floatingMobileWidth',
    'flowTermId',
    'fullScreen',
    'inviewAdInterval',
    'leftBrandingColor',
    'leftBrandingSize',
    'loopPlaylist',
    'maxAdsPerClip',
    'maxAdsPerClipChecked',
    'notInviewAdInterval',
    'playbackSpeedControl',
    'playerBrandSafeties',
    'playerContentOwnerFeeds',
    'playerEditorialPlaylistId',
    'playerFeedLanguages',
    'playerLreLogo',
    'playlistLimit',
    'preRoll',
    'preRollWaitMs',
    'progressBarColor',
    'settingsMenuHighlightColor',
    'shufflePlaylist',
    'socialLikeButton',
    'socialViewButton',
    'startWithSound',
    'startWithSoundMobile',
    'timestampTooltipColor',
    'useDefaultContentOwner',
    'videoResolutionControl',
    'volumeBarColor',
    'xRayCampaignsEnabled',
    'verticalFullscreenTitleTimelineVisibility',
    'swipeAnimation',
    'swipeAnimationMaxVisits',
    'swipeAnimationDuration',
    'prerollOnly',
    'prerollOnlyClipsCount',
  ],
  [TYPE_OUTSTREAM]: [
    'floatingDesktopEnabled',
    'floatingDesktopMode',
    'floatingDesktopPosition',
    'floatingDesktopWidth',
    'floatingMobileMode',
    'floatingMobilePosition',
    'floatingMobileWidth',
    'adManagerDesktop',
    'fallbackDisplayMonetizationDesktop',
    'inviewAdInterval',
    'notInviewAdInterval',
  ],
};

const getValue = (data, field) => {
  const fieldValue = data[field];

  switch (field) {
    case 'verticalFullscreenTitleTimelineVisibility':
      return fieldValue || 'ALWAYS';
    case 'flowTermId':
      return fieldValue === 0 ? DISABLED_FLOW_TERM_OPTION.ApiValue : fieldValue;
    case 'playerContentOwnerFeeds':
      return Array.isArray(fieldValue) ? fieldValue.filter((f) => f.id !== ALL_FEEDS_ID) : fieldValue;
    case 'fallbackDisplayMonetizationDesktop':
      return data.adManagerDesktop === VIDEO_MONETIZATION_ENABLED ? fieldValue : false;
    case 'fallbackDisplayMonetizationMobile':
      return data.adManagerMobile === VIDEO_MONETIZATION_ENABLED ? fieldValue : false;
    case 'floatingMobileEnabled':
      if (isAmpTypes(data.type) && ![FLOATING_DISABLED, FLOATING_AMP_STICKY_AD_BAR].includes(fieldValue)) {
        return FLOATING_DISABLED;
      }
      return fieldValue;
    case 'selectors':
      return data.customPlaceholderEnable ? fieldValue.map((s) => s.selector) : [];
    default:
      return fieldValue;
  }
};

const createPlayerRequestBody = (playerType, state) => {
  const playerFields = [...commonPlayerFields, ...playerTypesFields[playerType]];
  const playerData = selectors.playerStateSelector(state);
  const body = {};
  playerFields.forEach((field) => {
    body[field] = getValue(playerData, field);
  });
  return body;
};

export default createPlayerRequestBody;
