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

import { UPLOAD_STATUS_WAIT } from '@/modules/uploaderNew/constants';

const initialState = {
  activeTab: 0,
  video: null,
  csvFile: null,
  videoUrl: null,
  thumbnail: null,
  date: null,
  title: '',
  refId: '',
  description: '',
  owner: null,
  feed: null,
  feedList: [],
  language: { label: 'English', value: 'EN' },
  labels: [],
  keywords: [],
  iab: [],
  isNotify: true,
  uploadVideo: {},
  uploadCsv: {},
  uploadVideoCancelled: true,
  error: {},
  loading: false,
  loadingProgress: 0,
  isOpen: false,
  evergreen: false,
  notes: '',
  ccFiles: [],
  accessLevel: '',
  sites: [],
  sitesList: null,
  sendHubsNotification: false,

  // upload new video file ( video version feature)
  isOpenVideoVersionUpload: false,
  videoId: null,
  isNotifyToAccessUsers: false,
  videoVersionId: null,
  videoVersionNotes: null,
  videoAccess: null,

  // upload queue
  processedUploadQueueId: null,
  processedUploadStatus: UPLOAD_STATUS_WAIT,
  uploadConfirmDialog: null,
  queue: {},

  // sponsored content attribute
  landingPageLink: '',
  primaryText: '',
  buttonLabel: '',
  secondaryText: '',
  timeToAppear: 0,
  advertiserId: '',
  advertiserName: '',
  advertiserLogo: '',
  advertiserOptions: null,
  sponsored: false,
};

export const slice = createSlice({
  name: '@@uploaderNew/UPLOADER',
  initialState,
  reducers: {
    videoAction: (state, action) => {
      state.video = action.payload || initialState.video;
    },
    csvFileAction: (state, action) => {
      state.csvFile = action.payload || initialState.csvFile;
    },
    videoUrlAction: (state, action) => {
      state.videoUrl = action.payload || initialState.videoUrl;
    },
    thumbnailAction: (state, action) => {
      state.thumbnail = action.payload || initialState.thumbnail;
    },
    dateAction: (state, action) => {
      state.date = action.payload || initialState.date;
    },
    titleAction: (state, action) => {
      state.title = action.payload || initialState.title;
    },
    refIdAction: (state, action) => {
      state.refId = action.payload || initialState.refId;
    },
    descriptionAction: (state, action) => {
      state.description = action.payload || initialState.description;
    },
    ownerAction: (state, action) => {
      state.owner = action.payload || initialState.owner;
    },
    feedAction: (state, action) => {
      state.feed = action.payload || initialState.feed;
    },
    feedListAction: (state, action) => {
      state.feedList = action.payload || initialState.feedList;
    },
    languageAction: (state, action) => {
      state.language = action.payload || initialState.language;
    },
    labelsAction: (state, action) => {
      state.labels = action.payload || initialState.labels;
    },
    keywordsAction: (state, action) => {
      state.keywords = action.payload || initialState.keywords;
    },
    isNotifyAction: (state, action) => {
      state.isNotify = action.payload;
    },
    uploadVideoCancelledAction: (state, action) => {
      state.uploadVideoCancelled = action.payload || initialState.uploadVideoCancelled;
    },
    errorAction: (state, action) => {
      state.error = action.payload || initialState.error;
    },
    loadingAction: (state, action) => {
      state.loading = action.payload || initialState.loading;
    },
    loadingProgressAction: (state, action) => {
      state.queue = {
        ...state.queue,
        [action.payload.processedUploadQueueId]: {
          ...state.queue[action.payload.processedUploadQueueId],
          loadingProgress: action.payload.percentage || 0,
        },
      };
    },
    isOpenAction: (state, action) => {
      state.isOpen = action.payload || initialState.isOpen;
    },
    evergreenAction: (state, action) => {
      state.evergreen = action.payload || initialState.evergreen;
    },
    notesAction: (state, action) => {
      state.notes = action.payload || initialState.notes;
    },
    setCcFilesAction: (state, action) => {
      state.ccFiles = action.payload || initialState.ccFiles;
    },
    setUploadVideoCcFilesAction: (state, action) => {
      state.queue = {
        ...state.queue,
        [action.payload.processedUploadQueueId]: {
          ...state.queue[action.payload.processedUploadQueueId],
          uploadVideo: {
            ...state.queue[action.payload.processedUploadQueueId].uploadVideo,
            ccFiles: action.payload.ccFiles,
          },
        },
      };
    },
    accessLevelAction: (state, action) => {
      state.accessLevel = action.payload || initialState.accessLevel;
    },
    setSitesListAction: (state, action) => {
      state.sitesList = action.payload || initialState.sitesList;
    },
    setActiveTabAction: (state, action) => {
      state.activeTab = action.payload;
    },
    sendHubsNotificationAction: (state, action) => {
      state.sendHubsNotification = action.payload || initialState.sendHubsNotification;
    },
    clearAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        if (!['queue', 'processedUploadQueueId'].includes(key)) {
          state[key] = initialState[key];
        }
      });
    },
    clearAllAction: (state) => {
      Object.keys(initialState).forEach((key) => {
        state[key] = initialState[key];
      });
    },
    setS3LinksAction: (state, action) => {
      state[action.payload.key] = action.payload.s3uploadParams;
    },
    addToUploadQueueAction: (state) => {
      const { queue, ...slicedState } = state;
      state.queue = {
        ...state.queue,
        [`${Math.random()}-${Math.random()}`]: slicedState,
      };
    },
    setProcessUploadQueueIdAction: (state, action) => {
      state.processedUploadQueueId = action.payload;
    },
    setProcessUploadStatusAction: (state, action) => {
      state.queue[action.payload.processedUploadQueueId] = {
        ...state.queue[action.payload.processedUploadQueueId],
        processedUploadStatus: action.payload.status,
      };
    },
    showCancelUploadConfirmDialogAction: (state, action) => {
      state.uploadConfirmDialog = action.payload;
    },
    hideCancelUploadConfirmDialogAction: (state) => {
      state.uploadConfirmDialog = null;
    },
    openVideoVersionUploadAction: (state, action) => {
      state.isOpenVideoVersionUpload = true;
      state.videoId = action.payload.videoId;
      state.videoAccess = action.payload.access;
    },
    isNotifyToAccessUsersAction: (state, action) => {
      state.isNotifyToAccessUsers = action.payload;
    },
    setVideoVersionIdAction: (state, action) => {
      state.videoVersionId = action.payload;
    },
    setVideoVersionNotesAction: (state, action) => {
      state.videoVersionNotes = action.payload;
    },
    uploadVideoAction: (state) => state,
    uploadCsvAction: (state) => state,
    uploadThumbnailAction: (state) => state,
    createVideoAction: (state) => state,
    createVideoFromCsvAction: (state) => state,
    uploadCcFilesAction: (state) => state,
    getSitesListAction: (state) => state,
    sitesAction: (state, action) => {
      state.sites = action.payload;
    },
    startUploadAction: (state) => state,
    startUploadFromQueueAction: (state) => state,
    chooseUploadBranchAction: (state) => state,
    clearUploadQueueAction: (state) => state,
    handleCancelUploadFromConfirmDialogAction: (state) => state,
    getFeedSourcesAction: (state) => state,
    startVideoVersionUploadAction: (state) => state,
    chooseVideoVersionUploadBranchAction: (state) => state,
    uploadVideoVersionAction: (state) => state,
    createVideoVersionAction: (state) => state,
    iabAction: (state, action) => {
      state.iab = action.payload || initialState.iab;
    },
    setAction: (state, action) => {
      Object.keys(action.payload).forEach((key) => {
        state[key] = action.payload[key];
      });
    },
    getAdvertiserOptions: (state) => state,
  },
});

export const {
  videoAction,
  csvFileAction,
  videoUrlAction,
  thumbnailAction,
  dateAction,
  titleAction,
  refIdAction,
  descriptionAction,
  ownerAction,
  feedAction,
  feedListAction,
  languageAction,
  labelsAction,
  keywordsAction,
  isNotifyAction,
  uploadVideoCancelledAction,
  errorAction,
  loadingAction,
  loadingProgressAction,
  isOpenAction,
  evergreenAction,
  notesAction,
  setCcFilesAction,
  setUploadVideoCcFilesAction,
  accessLevelAction,
  setSitesListAction,
  setActiveTabAction,
  sendHubsNotificationAction,
  clearAction,
  clearAllAction,
  setS3LinksAction,
  addToUploadQueueAction,
  setProcessUploadQueueIdAction,
  setProcessUploadStatusAction,
  showCancelUploadConfirmDialogAction,
  hideCancelUploadConfirmDialogAction,
  openVideoVersionUploadAction,
  isNotifyToAccessUsersAction,
  setVideoVersionIdAction,
  setVideoVersionNotesAction,
  uploadVideoAction,
  uploadCsvAction,
  uploadThumbnailAction,
  createVideoAction,
  createVideoFromCsvAction,
  uploadCcFilesAction,
  getSitesListAction,
  sitesAction,
  startUploadAction,
  startUploadFromQueueAction,
  chooseUploadBranchAction,
  clearUploadQueueAction,
  handleCancelUploadFromConfirmDialogAction,
  getFeedSourcesAction,
  startVideoVersionUploadAction,
  chooseVideoVersionUploadBranchAction,
  uploadVideoVersionAction,
  createVideoVersionAction,
  iabAction,
  setAction,
  getAdvertiserOptions,
} = slice.actions;
