class Admin::WordHistoriesController < AdminController
  include ExecuteSearch

  before_action :set_word_history, only: [:show, :edit, :update, :destroy]

  autocomplete :person, :english_name, full: true

  # GET /word_histories
  # GET /word_histories.json
  def index
    @word_histories = execute_search(WordHistory)
    render locals: { word_histories: @word_histories }
  end

  # GET /word_histories/1
  # GET /word_histories/1.json
  def show
    respond_to do |format|
      format.html
      format.json { render json: @word_history }
    end
  end

  # GET /word_histories/new
  def new
    @word_history = WordHistory.new
  end

  # GET /word_histories/1/edit
  def edit
  end

  # POST /word_histories
  # POST /word_histories.json
  def create
    @word_history = WordHistory.new(word_history_params)

    respond_to do |format|
      if @word_history.save
        format.html { redirect_to [:admin, :word_histories], notice: 'WordHistory was successfully created.' }
        format.json { render json: @word_history }
      else
        format.html { render :new }
        format.json { render json: @word_history.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /word_histories/1
  # PATCH/PUT /word_histories/1.json
  def update
    respond_to do |format|
      if @word_history.update(word_history_params)
        format.html { redirect_to [:admin, :word_histories], notice: 'WordHistory was successfully updated.' }
        format.json { render json: @word_history }
      else
        format.html { render :edit }
        format.json { render json: @word_history.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /word_histories/1
  # DELETE /word_histories/1.json
  def destroy
    @word_history.destroy
    respond_to do |format|
      format.html { redirect_to admin_word_histories_url, notice: 'WordHistory was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def form
    @word_history = WordHistory.find_or_initialize_by(id: params[:id])
    @remote = (params[:remote] == 'true')
    @format = @remote ? :json : :html
    respond_to do |format|
      format.html { render :form, layout: false }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_word_history
      @word_history = WordHistory.find(params[:id])
      authorize @word_history
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def word_history_params
      params[:word_history].permit(
        :id, :form, :gloss, :public_note, :note, :date, :word_history_type, :language,
        :location, :source_id, :type
      )
    end
end
