import { createSlice } from '@reduxjs/toolkit';

const initialState = {
  videos: [],
  totalCount: 0,
  isVideosLoading: false,
  isNextVideosLoading: false,
  filterParams: {},
  filterInternalParams: {},
  searchFilters: null,
  selectedVideo: null,
  page: 0,
  size: 0,
  players: [],
  maxPerformance: {},
  isSearchFiltered: false,
  targetVideoId: null,
  targetClipId: null,
  isTargetClipIdLoading: false,
  canAddToPlaylist: false,
  showNotification: '',
  startSearchEvent: null,
  selectedPlayer: null,
  selectedAspectRatio: null,
  embedCode: '',
  getEmbedCode: null,
  dragAndDropEnd: null,
  playlistMode: null,
  showEmbedCodeButton: false,
  aiPlaylistId: 0,
  showDivider: false,
  currentFiltersAsString: '',
};

export const slice = createSlice({
  name: '@@editorialSearchResults/EDITORIAL_SEARCH_RESULTS',
  initialState,
  reducers: {
    videosAction: (state, action) => {
      state.videos = action.payload || initialState.videos;
    },
    totalCountAction: (state, action) => {
      state.totalCount = action.payload || initialState.totalCount;
    },
    isVideosLoadingAction: (state, action) => {
      state.isVideosLoading = action.payload || initialState.isVideosLoading;
    },
    isNextVideosLoadingAction: (state, action) => {
      state.isNextVideosLoading = action.payload || initialState.isNextVideosLoading;
    },
    filterParamsAction: (state, action) => {
      state.filterParams = action.payload || initialState.filterParams;
    },
    filterInternalParamsAction: (state, action) => {
      state.filterInternalParams = action.payload || initialState.filterInternalParams;
    },
    searchFiltersAction: (state, action) => {
      state.searchFilters = action.payload || initialState.searchFilters;
    },
    selectedVideoAction: (state, action) => {
      state.selectedVideo = action.payload || initialState.selectedVideo;
    },
    pageAction: (state, action) => {
      state.page = action.payload || initialState.page;
    },
    sizeAction: (state, action) => {
      state.size = action.payload || initialState.size;
    },
    playersAction: (state, action) => {
      state.players = action.payload || initialState.players;
    },
    maxPerformanceAction: (state, action) => {
      state.maxPerformance = action.payload || initialState.maxPerformance;
    },
    isSearchFilteredAction: (state, action) => {
      state.isSearchFiltered = action.payload || initialState.isSearchFiltered;
    },
    targetVideoIdAction: (state, action) => {
      state.targetVideoId = action.payload || initialState.targetVideoId;
    },
    targetClipIdAction: (state, action) => {
      state.targetClipId = action.payload || initialState.targetClipId;
    },
    isTargetClipIdLoadingAction: (state, action) => {
      state.isTargetClipIdLoading = action.payload || initialState.isTargetClipIdLoading;
    },
    canAddToPlaylistAction: (state, action) => {
      state.canAddToPlaylist = action.payload || initialState.canAddToPlaylist;
    },
    showNotificationAction: (state, action) => {
      state.showNotification = action.payload || initialState.showNotification;
    },
    startSearchEventAction: (state, action) => {
      state.startSearchEvent = action.payload || initialState.startSearchEvent;
    },
    selectedPlayerAction: (state, action) => {
      state.selectedPlayer = action.payload || initialState.selectedPlayer;
    },
    selectedAspectRatioAction: (state, action) => {
      state.selectedAspectRatio = action.payload || initialState.selectedAspectRatio;
    },
    embedCodeAction: (state, action) => {
      state.embedCode = action.payload || initialState.embedCode;
    },
    getEmbedCodeAction: (state, action) => {
      state.getEmbedCode = action.payload || initialState.getEmbedCode;
    },
    dragAndDropEndAction: (state, action) => {
      state.dragAndDropEnd = action.payload || initialState.dragAndDropEnd;
    },
    playlistModeAction: (state, action) => {
      state.playlistMode = action.payload || initialState.playlistMode;
    },
    showEmbedCodeButtonAction: (state, action) => {
      state.showEmbedCodeButton = action.payload || initialState.showEmbedCodeButton;
    },
    aiPlaylistIdAction: (state, action) => {
      state.aiPlaylistId = action.payload || initialState.aiPlaylistId;
    },
    showDividerAction: (state, action) => {
      state.showDivider = action.payload || initialState.showDivider;
    },
    clearAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        state[key] = initialState[key];
      });
    },
    currentFiltersAsStringAction: (state, action) => {
      state.currentFiltersAsString = JSON.stringify(action.payload);
    },
    restartSearchEventAction: (state) => state,
    targetClipIdEventAction: (state) => state,
    addFiltersQueriesAction: (state) => state,
    getPlayersAction: (state) => state,
    downloadEventAction: (state) => state,
    videosChangeProcessingStatusMonitoringAction: (state) => state,
    loadNextEventAction: (state) => state,
  },
});

export const {
  videosAction,
  totalCountAction,
  isVideosLoadingAction,
  isNextVideosLoadingAction,
  filterParamsAction,
  filterInternalParamsAction,
  searchFiltersAction,
  selectedVideoAction,
  pageAction,
  sizeAction,
  playersAction,
  maxPerformanceAction,
  isSearchFilteredAction,
  targetVideoIdAction,
  targetClipIdAction,
  isTargetClipIdLoadingAction,
  canAddToPlaylistAction,
  showNotificationAction,
  startSearchEventAction,
  selectedPlayerAction,
  selectedAspectRatioAction,
  embedCodeAction,
  getEmbedCodeAction,
  dragAndDropEndAction,
  playlistModeAction,
  showEmbedCodeButtonAction,
  aiPlaylistIdAction,
  showDividerAction,
  clearAction,
  restartSearchEventAction,
  targetClipIdEventAction,
  addFiltersQueriesAction,
  getPlayersAction,
  downloadEventAction,
  videosChangeProcessingStatusMonitoringAction,
  loadNextEventAction,
  currentFiltersAsStringAction,
} = slice.actions;
