/**
 * notifcation reducers
 */
import { applicationState } from "../../state"
import { clone, mergeDeepRight } from "ramda"
import { $store } from "@racletteCore"
/**
 * add notifcation
 */
const notificationsAdd = (state, action) => {
  const items = clone(state.items)
  const notificationTime = new Date()
  const notificationEntry = action.payload

  notificationEntry.time = notificationTime
  notificationEntry.state = "unread"
  // as we just want updates pushed to the notification que per item and editor each 5min
  const notificationTimeThreshold = 1000 * 60 * 5
  const notificationKeyTime = new Date(
    Math.round(notificationTime.getTime() / notificationTimeThreshold) *
      notificationTimeThreshold,
  )
  const notificationKey = `${notificationTime.getMonth()}-${notificationTime.getDay()}-${notificationTime.getHours()}-${notificationKeyTime.getMinutes()}-${notificationEntry.event}-${notificationEntry.updatedBy}-${notificationEntry.item ? notificationEntry.item : 0}`

  items[notificationKey] = notificationEntry

  return {
    ...state,
    items,
    unreadCount: state.unreadCount + 1,
  }
}

/**
 * clear notifications
 */
const notificationsClear = (state, action) => applicationState.notifications

/**
 * mark all notifcations as seen
 */
const notificationsMarkAllAsSeen = (state, action) => {
  const items = clone(state.items)

  Object.values(items).forEach((item) => {
    if (item.state !== "read") {
      item.state = "seen"
    }
  })
  $store.dispatch({
    type: "notifications/update",
    payload: {
      unreadCount: 0,
    },
  })

  return {
    ...state,
    items,
  }
}

/**
 * mark all notifactions as read
 */
const notificationsMarkAllAsRead = (state, action) => {
  const items = clone(state.items)

  Object.values(items).forEach((item) => {
    if (item.state === "unread") {
      item.state = "read"
    }
  })
  $store.dispatch({
    type: "notifications/update",
    payload: {
      unreadCount: 0,
    },
  })

  return {
    ...state,
    items,
  }
}

/**
 * mark notification as read
 */
const notificationsMarkAsRead = (state, action) => {
  const items = clone(state.items)

  if (items[action.payload.queKey].state !== "read") {
    items[action.payload.queKey].state = "read"
    if (state.unreadCount > 0) {
      $store.dispatch({
        type: "notifications/update",
        payload: {
          unreadCount: state.unreadCount - 1,
        },
      })
    }
  }

  return {
    ...state,
    items: items,
  }
}

/**
 * mark notifcation as unread
 */
const notificationsMarkUnread = (state, action) => {
  const items = clone(state.items)

  if (items[action.payload.queKey].state !== "unread") {
    items[action.payload.queKey].state = "unread"
    $store.dispatch({
      type: "notifications/update",
      payload: {
        unreadCount: state.unreadCount + 1,
      },
    })
  }

  return {
    ...state,
    items: items,
  }
}

/**
 * toggle notifications
 */
const notificationsToggle = (state, action) => {
  if (action.payload.toggled === false && state.toggled === true) {
    $store.dispatch({
      type: "notifications/update",
      payload: {
        unreadCount: 0,
      },
    })
    $store.dispatch({
      type: "notifications/markAllAsSeen",
    })
  }

  return {
    ...state,
    ...action.payload,
  }
}

/**
 * update notifications
 */
const notificationsUpdate = (state, action) =>
  mergeDeepRight(state, action.payload)

export {
  notificationsAdd,
  notificationsClear,
  notificationsMarkAllAsSeen,
  notificationsMarkAllAsRead,
  notificationsMarkAsRead,
  notificationsMarkUnread,
  notificationsToggle,
  notificationsUpdate,
}
