class PhonesController < ApplicationController

  # GET /phones
  # GET /phones.json
  def index
    authorize! :read, Phone, :message => 'Not authorized to view phone numbers.'
    if !params[:person_id].blank?
      # addresses for a specific person
      @person = Person.find(params[:person_id])
      @phones = @person.phones

      respond_to do |format|
        format.html { render partial: 'phones/people/index', locals: { phones: @phones } } # ./people/_index.html.erb
        format.json { render json: @phones }
      end
    else
      # on it's own
      #if params[:q].present? 
        @search = Phone.search(params[:q])
        @phones = @search.result.paginate(:page => params[:page])
      #else
      #  @phones = Phone.paginate(:page => params[:page])
      #end

      respond_to do |format|
        format.html # index.html.erb
        format.json { render json: @phones }
      end
    end
  end

  # GET /phones/1
  # GET /phones/1.json
  def show
    @phone = Phone.find(params[:id])
    authorize! :read, @phone, :message => 'Not authorized to view this phone number.'

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @phone }
    end
  end

  # GET /phones/new
  # GET /phones/new.json
  def new
    if !params[:person_id].blank?
      # if this is a new address under the person page, make sure we prep for it and send back that form
      @person = Person.find(params[:person_id])
      @phone = @person.phones.build()
      authorize! :create, @phone, :message => 'Not authorized to manage phone numbers.'

      respond_to do |format|
        format.html { render partial: 'phones/people/form' } # ./people/_form.html.erb
        format.json { render json: @phone }
      end
    else
      @phone = Phone.new
      authorize! :create, @phone, :message => 'Not authorized to manage phone numbers.'

      respond_to do |format|
        format.html # new.html.erb
        format.json { render json: @phone }
      end
    end
  end

  # GET /phones/1/edit
  def edit
    @phone = Phone.find(params[:id])
    authorize! :update, @phone, :message => 'Not authorized to manage phone numbers.'

    if !params[:person_id].blank?
      @person = Person.find(params[:person_id])
      respond_to do |format|
        format.html { render partial: 'phones/people/form' } # ./people/_form.html.erb
        format.json { render json: @phone }
      end
    else
      respond_to do |format|
        format.html # edit.html.erb
        format.json { render json: @phone }
      end
    end
 end

  # POST /phones
  # POST /phones.json
  def create
    @phone = Phone.new(params[:phone])
    authorize! :create, @phone, :message => 'Not authorized to manage phone numbers.'

    if !params[:person_id].blank?
      # nested under people
      # if this is a new phone under the person page, make sure we prep for it and send back that form
      @person = Person.find(params[:person_id])
      if @phone.valid? and @person.phones.create(params[:phone])
        respond_to do |format|
          format.html { redirect_to @phone, notice: 'Phone was successfully created.' }
          format.js { } # create.js.erb
          format.json { render json: @phone, status: :created, location: @phone }
        end
      else
        respond_to do |format|
          format.html { render action: "new" }
          format.js { } # create.js.erb
          format.json { render json: @phone.errors, status: :unprocessable_entity }
        end
      end
    else
      # on it's own
      respond_to do |format|
        if @phone.save
          format.html { redirect_to phones_path, notice: 'Phone was successfully created.' }
          format.json { render json: @phone, status: :created, location: @phone }
        else
          format.html { render action: "new" }
          format.json { render json: @phone.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # PUT /phones/1
  # PUT /phones/1.json
  def update
    @phone = Phone.find(params[:id])
    authorize! :update, @phone, :message => 'Not authorized to manage phone numbers.'

    if !params[:person_id].blank?
      @person = Person.find(params[:person_id])
    end

    respond_to do |format|
      if @phone.update_attributes(params[:phone])
        format.html { redirect_to phones_path, notice: 'Phone was successfully updated.' }
        format.js { }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.js { }
        format.json { render json: @phone.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /phones/1
  # DELETE /phones/1.json
  def destroy
    @phone = Phone.find(params[:id])
    authorize! :destroy, @phone, :message => 'Not authorized to manage phone numbers.'
    @phone.destroy

    respond_to do |format|
      format.html { redirect_to phones_url }
      format.json { head :no_content }
    end
  end
end
