class NotificationsController < ApplicationController
  add_breadcrumb 'Dashboard', :dashboard_path
  before_filter :find_trackable, except: [:index]

  # Get all the notifications for current user.
  def index
    add_breadcrumb 'Notifications'
    current_user.reset_cached_notification_count
    #FIXME : Need to include profile_image in preload list. Curently htis is a bug in rails
    #https://github.com/rails/rails/issues/16070
    @notifications = current_user.notifications.order('created_at DESC').includes(:owner).page(params[:page]).per(params[:per_page])
    respond_to do |format|
      format.js {}
      format.html do
        render partial: params[:type] if params[:type]
      end
    end
  end

  def accept
    @notification.update_columns(response: "Accepted")
    if @trackable.is_a? ActsAsFriendable::Friendship
      @trackable.update(approved: true)
      FriendMailer.delay(queue: :mailer).friendship_request_status_email(@trackable) if @user.notification_enabled_for?('friendship')
      redirect_url = profile_path(@trackable.friend.id)
    else
      group_schedule_exception = @user.group_schedule_exceptions.find_by(schedule_id: @trackable.id)
      group_schedule_exception.destroy if group_schedule_exception.present?
      @confirmation = @trackable.confirmations.find_by(user: @user, initiator: false)
      if @confirmation.present?
        @confirmation.accepted = true unless @confirmation.accepted
      else
        @confirmation = @trackable.confirmations.build(user: @user, accepted: true, initiator: false)
      end
      if @confirmation.save
        if @trackable.is_a? Event
          redirect_url = personal_calendar_path
          EventMailer.delay(queue: :mailer).notify_owner_for_event_acceptance(@trackable.id, @user.id, 'accepted') if @user.notification_enabled_for?('schedule')
        else
          redirect_url = task_path(@trackable.id)
          TaskMailer.delay(queue: :mailer).notify_owner_for_task_acceptance(@trackable.id, @user.id, 'accepted') if @user.notification_enabled_for?('schedule')
        end
      end
    end
    respond_to do |format|
      format.js {render 'confirmation'}
      format.html {redirect_to redirect_url}
    end
  end

  def reject
    @notification.update_columns(response: "Rejected")
    if @trackable.is_a? ActsAsFriendable::Friendship
      @trackable.update(approved: false)
      FriendMailer.delay(queue: :mailer).friendship_request_status_email(@trackable) if @user.notification_enabled_for?('friendship')
      redirect_url = profile_path(@trackable.friend.id)
    else
      @confirmation = @trackable.confirmations.build(user: @user, accepted: false, initiator: false)
      if @confirmation.save
        if @trackable.is_a? Event
          redirect_url = personal_calendar_path
          EventMailer.delay(queue: :mailer).notify_owner_for_event_acceptance(@trackable.id, @user.id, 'rejected') if @user.notification_enabled_for?('schedule')
        else
          redirect_url = task_path(@trackable.id)
          TaskMailer.delay(queue: :mailer).notify_owner_for_task_acceptance(@trackable.id, @user.id, 'rejected') if @user.notification_enabled_for?('schedule')
        end
      end
    end
    respond_to do |format|
      format.js {render 'confirmation'}
      format.html {redirect_to redirect_url}
    end
  end

  private

  def find_trackable
    if params[:user_id].present?
      @user = User.find(params[:user_id])
      auto_login @user
    else
      @user = current_user
    end
    if params[:type] == 'friendship'
      @trackable = ActsAsFriendable::Friendship.find(params[:id])
      @notification = @user.notifications.where(trackable_id: params[:id]).last
    elsif params[:type] == 'schedule'
      @trackable = Schedule.find(params[:id])
      @notification = @user.notifications.where(trackable_id: params[:id]).last
    else
      @notification = @user.notifications.find(params[:id])
      @trackable = @notification.trackable
    end
  end


end
