class EventsController < ApplicationController
	helper EventsHelper
  before_action :set_event, only: [:show, :edit, :update, :destroy]

  def list
		@today = Event.find(1)
		if params[:month].blank?
			@month = Month.find(@today.month)
		else
			month_names = Month.all.map { |m| m.name.downcase }
			month_index = month_names.index(params[:month])
			month_index ||= params[:month].to_i - 1
			@month = Month.find(month_index+1)
		end
		@year = params[:year] ? params[:year].to_i : @today.year
		@events = Event.where(month: @month.order, year: @year).reject { |e| e.id == 1 || e.public != true }
  end

  # GET /events
  # GET /events.json
	def index
		@day = params[:day]
		@events = Event.order(:year,:month,:day)
	end

  # GET /events/1
  # GET /events/1.json
  def show
  end

  # GET /events/new
  def new
    @event = Event.new
  end

  # GET /events/1/edit
  def edit
  end

  # POST /events
  # POST /events.json
  def create
    if event_params[:content].titleize == 'Today'
      @today = Event.find 1
      @today.update(event_params)
      @today.save
      @event = @today
    else
      @event = Event.new(event_params)
    end

    respond_to do |format|
      if @event.save
        format.html { redirect_to root_path, notice: 'Event was successfully created.' }
        format.json { render :show, status: :created, location: @event }
      else
        format.html { render :new }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /events/1
  # PATCH/PUT /events/1.json
  def update
    respond_to do |format|
      if @event.update(event_params)
        format.html { redirect_to @event, notice: 'Event was successfully updated.' }
        format.json { render :show, status: :ok, location: @event }
      else
        format.html { render :edit }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /events/1
  # DELETE /events/1.json
  def destroy
    @event.destroy
    respond_to do |format|
      format.html { redirect_to events_url, notice: 'Event was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_event
      @event = Event.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def event_params
      params.require(:event).permit(:day, :month, :year, :content)
    end
end
