class EventsController < ApplicationController

  before_filter :require_login
  before_filter :find_event, except: [:new, :create, :index]

  # Retrieves all event.
  def index
    @events = Event.all
  end

  # Opens the edit form without rendering layout.
  def edit
    render layout: false
  end

  # Update the event.
  def update
    if @event.update_attributes(event_params)
      if !event_params["user_emails"].blank?
        @event.share_with!(event_params["user_emails"].split(','))
      end
      respond_to do |format|
        format.js {render 'save'}
        format.json {render json: {}}
      end
    else
      respond_to do |format|
        format.js {render 'save'}
        format.json {render json: {error: @event.errors.full_messages}, status: 403}
      end
    end
  end

  # Method to intialize object for event and tutoring session.
  def new
    @event = Event.new(start_time: params[:start_time], end_time: params[:end_time])
    @tutoring_session = TutoringSession.new(start_time: params[:start_time], end_time: params[:end_time], date: params[:start_time].to_date)
    if params[:user_id]
      @tutor = User.find(params[:user_id])
      if @tutor.tutor?
        @tutoring_session.tutor = @tutor
      end
    end
    render layout: false
  end

  # Method to create event and send invitation of event to the selected group member.
  def create
    @event = current_user.owned_events.build(event_params)
    if !event_params["user_emails"].blank?
      users = User.where(email: event_params["user_emails"].split(','))
      invitee_emails = event_params["user_emails"].split(',') - users.map(&:email)
      users.each do |user|
        @event.shares.build(user_id: current_user.id, shared_to: user)
      end
    end
    if @event.save
      Invite.invite_users(invitee_emails, @event) if invitee_emails.present?
      respond_to do |format|
        format.js {render 'save'}
      end
    end
  end

  # Displays the event information.
  def show
    if @event && @event.grouped?
      @group = @event.shares.last.shared_to
    end
    render layout: false
  end

  # Destroy the event.
  def destroy
    @event.destroy
    respond_to do |format|
      format.js {render 'remove'}
    end
  end

  # To accept the event and build confirmation for the event with accept
  def accept
    @confirmation = @event.confirmations.build(user: current_user)
    @confirmation.accept
    if @confirmation.save
      EventMailer.delay(queue: :mailer).notify_owner_for_event_acceptance(@event.id, current_user.id, 'accepted')
    end
    respond_to do |format|
      format.js {render 'confirmation'}
    end
  end

  # To reject the event and build confirmation for the event with reject
  def reject
    @confirmation = @event.confirmations.build(user: current_user)
    @confirmation.reject
    if @confirmation.save
      EventMailer.delay(queue: :mailer).notify_owner_for_event_acceptance(@event.id, current_user.id, 'rejected')
    end
    respond_to do |format|
      format.js {render 'confirmation'}
    end
  end

  private
  def event_params
    params.required(:event).permit(:name, :description, :start_time, :user_emails, :end_time, :topic_type, :stime, :etime, :date, :frequency, :group_id, :schedule_type, :owner_id, :reminder)
  end

  def find_event
    @event = current_user.events.find(params[:id])
  end

end
