class TestingTwentyninesController < ApplicationController
  before_action :set_testing_twentynine, only: [:edit, :update, :destroy, :full_edit]
  respond_to :html, :js

  def index
    #Define default page_opts in controller.
    @page_opts = TestingTwentynine.change_page_opts({:show_more => true}, TestingTwentynine.default_page_opts)
    
    respond_to do |format|
      format.html { render "testing_twentynines/html/index" }
      format.js { render "testing_twentynines/js/#{redir}" }
    end
  end
  
  def more
    @page_opts = TestingTwentynine.change_page_opts(params[:page_opts]) 
    @clear_view = params[:clear_view] ? params[:clear_view] : false
    
    results = TestingTwentynine.results_from_page_opts(@page_opts)
    
    @results_size = results[:total_size]
    @number_pages = results[:total_pages]
    @testing_twentynines = results[:results]
    
    respond_to do |format|
      format.js { render "testing_twentynines/js/more" }
      format.html { render "testing_twentynines/html/index" }
    end
  end
  
  def show
    @page_opts = TestingTwentynine.change_page_opts({:screen => 'view', :templates => {:list => 'plain', :display => 'full'}})
    id = params[:id] ? params[:id] : params[:testing_twentynine_id]
    @testing_twentynine = TestingTwentynine.find_with_step(id, params[:step])
    
    respond_to do |format|
      format.html { render "testing_twentynines/html/index" }
      format.js { render "testing_twentynines/js/show" }
    end
  end
  
  def clear_view
    respond_to do |format|
      format.js { render "testing_twentynines/js/clear_view" }
    end
  end
  
  def admin
    respond_to do |format|
      format.js { render "testing_twentynines/js/admin" }
    end
    
  end

  def new
    @testing_twentynine = TestingTwentynine.new
    @page_opts = TestingTwentynine.change_page_opts({:screen => 'view', :templates => {:list => 'plain', :display => 'full_form'}})
    respond_to do |format|
      format.html { render "testing_twentynines/html/new" }
      format.js { render "testing_twentynines/js/new" }
    end
  end

  def edit
    respond_to do |format|
      format.html { render "testing_twentynines/html/edit" }
      format.js { render "testing_twentynines/js/edit" }
    end
  end

  def full_edit
    @page_opts = TestingTwentynine.change_page_opts({:screen => 'view', :templates => {:list => 'plain', :display => 'full_form'}})
    respond_to do |format|
      format.js { render "testing_twentynines/js/full_edit" }
    end
  end

  def create
    @testing_twentynine = TestingTwentynine.new(testing_twentynine_params)
    @page_opts = TestingTwentynine.change_page_opts({:screen => 'view', :templates => {:list => 'plain', :display => 'full'}})
    @testing_twentynine.save
    respond_to do |format|
      format.html {redirect_to testing_twentynines_path}
      format.js { render "testing_twentynines/js/create" }
    end
  end

  def update
    @testing_twentynine.update(testing_twentynine_params)
    if testing_twentynine_params.size > 1
      redir_template = 'update_display'
      session[:updated] = true
    else
      @attribute = testing_twentynine_params.first[0].to_s
      @identifier = @testing_twentynine.identifier('attr', @attribute)
      redir_template = 'update_attr'
    end

    respond_to do |format|
      format.html { redirect_to @testing_twentynine }
      format.js { render "testing_twentynines/js/#{redir_template}" }
    end
  end

  def destroy
    @identifier = @testing_twentynine.identifier('id')
    @testing_twentynine.destroy
    respond_to do |format|
      format.html { }
      format.js { render "testing_twentynines/js/destroy" }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_testing_twentynine
      id = params[:id] ? params[:id] : params[:testing_twentynine_id]
      @testing_twentynine = TestingTwentynine.find(id)
    end

    # Only allow a trusted parameter "white list" through.
    def testing_twentynine_params
      params.require(:testing_twentynine).permit(:one, :two, :three)
    end
end
