import {
  DEFAULT_EVERGREEN_TYPE_VALUE,
  DEFAULT_FEED_TYPE_VALUE,
  DEFAULT_PUBLISH_PLATFORM_TYPE_VALUE,
  DEFAULT_PUBLISH_STATUS_TYPE_VALUE,
  DEFAULT_VERIFICATION_TYPE_VALUE,
  DEFAULT_VIDEO_STATUS_VALUE,
  evergreenOptions,
  FEED_TYPE_VIDEO_AUDIO,
  feedTypeOptions,
  FILTER_COMPONENTS_NAMES,
  filterPublishPlatformOptions,
  filterPublishStatusOptions,
  mediaTypeOptions,
  SPONSORED_ALL,
  sponsoredOptions,
  verificationOptions,
  VIDEO_DURATION_4_TO_20_MIN,
  VIDEO_DURATION_ALL,
  VIDEO_DURATION_LABELS,
  VIDEO_DURATION_OVER_20_MIN,
  VIDEO_DURATION_UNDER_4_MIN,
  VIDEO_TABS_ENUM,
  VIDEO_TARGETING_ANY,
  VIDEO_TARGETING_OPTIONS,
  videoStatusOptions,
} from './constants';
import { PCN_GET_DESTINATIONS, VIDEO_ACCESS_TARGETING } from '@/modules/@common/acl/constants';

import { deepClone } from '@/modules/@common/helpers';

const LAST_DAY = '1';
const LAST_WEEK = '7';
const LAST_MONTH = '30';

export const timeMap = {
  days: LAST_DAY,
  week: LAST_WEEK,
  month: LAST_MONTH,
};

export const timeValueMap = {
  [LAST_DAY]: { label: 'Past 24 hours', value: 'days' },
  [LAST_WEEK]: { label: 'Past Week', value: 'week' },
  [LAST_MONTH]: { label: 'Past Month', value: 'month' },
};

export const DEFAULT_FILTER_NAMES = {
  time: 'time',
  duration: 'duration',
  order: 'order',
};

export const INSIGHTS_FILTER_NAMES = {
  timeFrame: 'timeFrame',
};

export const filterConfig = {
  time: {
    filters: [
      { label: 'Any Time', value: 'any' },
      { label: 'Past 24 hours', value: 'days' },
      { label: 'Past Week', value: 'week' },
      { label: 'Past Month', value: 'month' },
      {
        label: 'Custom',
        value: {
          startDate: '',
          endDate: '',
        },
        component: FILTER_COMPONENTS_NAMES.FilterTimePicker,
      },
    ],
    isMobile: true,
  },
  duration: {
    filters: [
      { label: VIDEO_DURATION_LABELS[VIDEO_DURATION_ALL], value: VIDEO_DURATION_ALL },
      { label: VIDEO_DURATION_LABELS[VIDEO_DURATION_UNDER_4_MIN], value: VIDEO_DURATION_UNDER_4_MIN },
      { label: VIDEO_DURATION_LABELS[VIDEO_DURATION_4_TO_20_MIN], value: VIDEO_DURATION_4_TO_20_MIN },
      { label: VIDEO_DURATION_LABELS[VIDEO_DURATION_OVER_20_MIN], value: VIDEO_DURATION_OVER_20_MIN },
    ],
    isMobile: false,
  },
  languages: {
    filters: [
      {
        label: 'Languages',
        value: [],
        type: 'LANGUAGES',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  owner: {
    filters: [
      {
        label: 'Owner',
        value: [],
        type: 'OWNER',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  hubs: {
    filters: [
      {
        label: 'Hubs',
        value: [],
        type: 'HUBS',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  source: {
    filters: [
      {
        label: 'Sources',
        value: [],
        type: 'SOURCE',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  people: {
    filters: [
      {
        label: 'People',
        value: [],
        type: 'PEOPLE',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  brands: {
    filters: [
      {
        label: 'Brands',
        value: [],
        type: 'BRANDS',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  categories: {
    filters: [
      {
        label: 'Categories',
        value: [],
        type: 'IAB',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  brandSafety: {
    filters: [
      {
        label: 'Brand Safety',
        value: [],
        type: 'BRAND_SAFETY',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  order: {
    filters: [
      { label: 'Date', value: 'creationDate', isSortable: true },
      { label: 'Upload Date', value: 'created', isSortable: true },
      { label: 'Relevance', value: 'relevance', isSortable: false },
      { label: 'Trending', value: 'trending', isSortable: false },
      { label: 'Duration', value: 'duration', isSortable: true },
    ],
    isReversed: false,
    isSortable: true,
    isMobile: true,
  },
  labels: {
    filters: [
      {
        label: 'Custom Tags',
        value: [],
        type: 'LABELS',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  uploadDate: {
    filters: [
      {
        label: 'Upload Date',
        value: [
          {
            from: null,
            to: null,
          },
        ],
        component: FILTER_COMPONENTS_NAMES.FilterDatePicker,
      },
    ],
  },
  evergreen: {
    filters: [
      {
        label: 'Evergreen',
        value: [
          {
            label: 'All',
            value: DEFAULT_EVERGREEN_TYPE_VALUE,
          },
        ],
        type: 'EVERGREEN',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: evergreenOptions,
        defaultValue: DEFAULT_EVERGREEN_TYPE_VALUE,
        dataId: 'evergreen',
      },
    ],
  },
  sponsored: {
    filters: [
      {
        label: 'Sponsored',
        value: [{ label: 'All', value: SPONSORED_ALL }],
        type: 'SPONSORED',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: sponsoredOptions,
        defaultValue: SPONSORED_ALL,
        dataId: 'sponsored-type',
      },
    ],
  },
  publishPlatform: {
    filters: [
      {
        label: 'Publish platform',
        value: [
          {
            label: 'All',
            value: DEFAULT_PUBLISH_PLATFORM_TYPE_VALUE,
          },
        ],
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: filterPublishPlatformOptions,
        defaultValue: DEFAULT_PUBLISH_PLATFORM_TYPE_VALUE,
        dataId: 'publish-platform',
        permission: PCN_GET_DESTINATIONS,
      },
    ],
  },
  publishStatus: {
    filters: [
      {
        label: 'Publish status',
        value: [
          {
            label: 'All',
            value: DEFAULT_PUBLISH_STATUS_TYPE_VALUE,
          },
        ],
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: filterPublishStatusOptions,
        defaultValue: DEFAULT_PUBLISH_STATUS_TYPE_VALUE,
        dataId: 'publish-status',
        permission: PCN_GET_DESTINATIONS,
      },
    ],
  },
  status: {
    filters: [
      {
        label: 'Video Status',
        value: [{ label: 'Any', value: DEFAULT_VIDEO_STATUS_VALUE }],
        type: 'VIDEO_STATUS',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: videoStatusOptions,
        defaultValue: DEFAULT_VIDEO_STATUS_VALUE,
        dataId: 'video-state',
      },
    ],
  },
  verification: {
    filters: [
      {
        label: 'Verification',
        value: [{ label: 'Any', value: DEFAULT_VERIFICATION_TYPE_VALUE }],
        type: 'VERIFICATION',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: verificationOptions,
        defaultValue: DEFAULT_VERIFICATION_TYPE_VALUE,
        dataId: 'verification',
      },
    ],
  },
  feedType: {
    filters: [
      {
        label: 'Source Type',
        value: [{ label: 'Any', value: DEFAULT_FEED_TYPE_VALUE }],
        type: 'FEED_TYPE',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: feedTypeOptions,
        defaultValue: DEFAULT_PUBLISH_STATUS_TYPE_VALUE,
        dataId: 'feed-type',
      },
    ],
  },
  mediaType: {
    filters: [
      {
        label: 'Media Type',
        value: [{ label: 'Video+Audio', value: FEED_TYPE_VIDEO_AUDIO }],
        type: 'FEED_TYPE',
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: mediaTypeOptions,
        defaultValue: FEED_TYPE_VIDEO_AUDIO,
        dataId: 'feed-type',
      },
    ],
  },
  timeFrame: {
    filters: [
      { label: 'Last 7 days', value: 'LAST_7_DAYS' },
      { label: 'Today', value: 'TODAY' },
      { label: 'Yesterday', value: 'YESTERDAY' },
      { label: 'Last 30 days', value: 'LAST_30_DAYS' },
    ],
  },
  orderForInsights: {
    filters: [
      {
        label: 'Views',
        value: 'insightsViews',
        info: 'Total number of views of this video',
      },
      {
        label: 'Likes',
        value: 'insightsLikes',
        info: 'Total number of likes of this video',
      },
      {
        label: 'Shares',
        value: 'insightsShares',
        info: 'Total number of shares of this video',
      },
      {
        label: 'Min Viewed',
        value: 'minutesViewed',
        info: 'Total number of minutes viewed of the video',
      },
      {
        label: 'Avg Playback',
        value: 'avgPlayback',
        info: 'Average playback duration of this video',
      },
      {
        label: 'Trending Score',
        value: 'trending',
        info: 'The trending score (based on views likes shares on the last 24-36 hours) of this video',
      },
      {
        label: 'Interactions',
        value: 'engagement',
        info: 'Total number of interactions (positive player actions) of this video ',
      },
    ],
    isReversed: false,
  },
  keywords: {
    filters: [
      {
        label: 'Keywords',
        value: [],
        type: 'KEYWORDS',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  videoTabs: {
    filters: [
      {
        label: 'All videos',
        value: VIDEO_TABS_ENUM.all,
      },
      {
        label: 'My videos',
        value: VIDEO_TABS_ENUM.own,
      },
      {
        label: 'Shared with me',
        value: VIDEO_TABS_ENUM.shared,
      },
    ],
  },
  account: {
    filters: [
      {
        label: 'Account',
        value: [], // { label, value, owners:[] }
        type: 'ACCOUNT',
        component: FILTER_COMPONENTS_NAMES.FilterSuggester,
      },
    ],
  },
  targetingStatus: {
    filters: [
      {
        label: 'Video Targeting',
        value: [VIDEO_TARGETING_OPTIONS[0]],
        component: FILTER_COMPONENTS_NAMES.FilterSelector,
        options: VIDEO_TARGETING_OPTIONS,
        defaultValue: VIDEO_TARGETING_ANY,
        dataId: 'targeting-status',
        permission: VIDEO_ACCESS_TARGETING,
      },
    ],
  },
};

export const getDefaultConfig = () => {
  const copiedFilters = deepClone(filterConfig);

  const getFilters = (key, filters) => {
    if (key === 'orderForInsights') {
      return [];
    }

    if (key === 'timeFrame') {
      return [filters[key].filters[3]];
    }

    return [copiedFilters[key].filters[0]];
  };

  return Object.keys(copiedFilters).reduce(
    (acc, key) => ({
      ...acc,
      ...{
        [key]: {
          ...copiedFilters[key],
          filters: getFilters(key, copiedFilters),
        },
      },
    }),
    {},
  );
};

export const getDefaultRelevanceOrderConfig = () => ({
  order: {
    ...deepClone(filterConfig).order,
    filters: [{ label: 'Relevance', value: 'relevance', isSortable: false }],
  },
});

export const getEmptySearchOrderConfig = () => ({
  order: {
    ...deepClone(filterConfig).order,
    filters: [{ label: 'Date', value: 'creationDate', isSortable: true }],
  },
});
