class RegistrationsController < ApplicationController
  before_action :set_registration, only: [:show, :edit, :update, :destroy]

  # GET /registrations
  def index
    @registrations = Registration.all
  end

  # GET /registrations/1
  def show
  end

  # GET /registrations/new
  def new
    @registration = Registration.new
    @address = @registration.build_address
    @county = @address.build_county
  end

  # GET /registrations/1/edit
  def edit
    @address = @registration.address || @registration.build_address
    @county = @address.county || @address.build_county
  end

  # POST /registrations
  def create
    @registration = Registration.new(registration_params)
    respond_to do |format|
      if @registration.save
        format.html { redirect_to @registration, notice: 'Registration was successfully created.' }
        flash.now[:notice] = 'Registration was successfully created.'
        format.js { render :success }
      else
        # format.html { render :new }
        emphasize_invalids(@registration, format)
      end
    end
  end

  # PATCH/PUT /registrations/1
  def update
    respond_to do |format|
      if @registration.update(registration_params)
        format.html { redirect_to @registration, notice: 'Registration was successfully updated.' }
        flash.now[:notice] = 'Registration was successfully updated.'
        format.js { render :success }
      else
        # render :edit
        emphasize_invalids(@registration, format)
      end
    end
  end

  # DELETE /registrations/1
  def destroy
    @registration.destroy
    redirect_to registrations_url, notice: 'Registration was successfully destroyed.'
  end

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

    # Only allow a trusted parameter "white list" through.
    def registration_params
      params.require(:registration).permit(:first_name, :middle_initial, :last_name, :email, :age, :date_of_birth, :accept_terms, :sex, :favorite_color, { prog_language_ids: [] }, address_attributes: [ :id, :street, :city, :state, :zip, county_attributes: [:id, :name] ])
    end
end
