module NotificationsHelper

  # Returns unread notifications count.
  def unread_notifications_count
    current_user.cached_notification_count
  end

  # Generates path for notification.
  def path_for_notification(notification)
    if notification.owner.persisted?
      case notification.trackable_type
        when 'Invoice', 'Transaction'
          dashboard_path(tab: "pay")
        when 'Schedule'
          if notification.key.split(".")[0] == "group_event"
            dashboard_path(tab: "appointments")
          else notification.key.split(".")[0] == "group_task"
            dashboard_path(tab: "tasks")
          end
        when 'ActsAsVotable::Vote'
          profile_path(notification.owner)
        when 'TutoringSession'
          personal_calendar_path()
        when 'ProfileVisit'
          profile_path(notification.owner)
      end
    end
  end

  # Generate notification links for actions(i.e. redirect to tasks or events).
  def notification_action_links(notification)
    links = ""
    case notification.key
      when 'group_task.update'
        links << notification_accept_link(accept_changes_task_path(notification.trackable_id))
        links << notification_reject_link(reject_changes_task_path(notification.trackable_id))
      when 'group_event.create'
        links << notification_accept_link(accept_event_path(notification.trackable_id))
        links << notification_reject_link(reject_event_path(notification.trackable_id))
      when 'group_task.create'
        links << notification_accept_link(accept_task_path(notification.trackable_id))
        links << notification_reject_link(reject_task_path(notification.trackable_id))
      when 'favorite.create'
        links << notification_accept_link(accept_user_favorites_path(notification.trackable_id))
        links << notification_reject_link(reject_user_favorites_path(notification.trackable_id))
    end
    links.html_safe
  end

  # Generates link to accept notifications.
  def notification_accept_link(path)
    link_to 'Accept', path, method: 'patch', remote: true, class: 'accept-btn'
  end

  # Generates link to reject notifications.
  def notification_reject_link(path)
    link_to 'Reject', path, method: 'patch', remote: true, class: 'reject-btn'
  end

end
