class IngredientsController < ApplicationController

  before_action :set_ingredient, only: [:edit, :update, :destroy]

  before_action :ensure_valid_user, except: [:index]

  # GET /ingredients
  # GET /ingredients.json
  def index
    @ingredients = Ingredient.all.order(:name)
  end

  # GET /ingredients/new
  def new
    @ingredient = Ingredient.new
  end

  # GET /ingredients/1/edit
  def edit
    ensure_owner @ingredient
  end

  # POST /ingredients
  # POST /ingredients.json
  def create
    @ingredient = Ingredient.new(ingredient_params)
    @ingredient.user = current_user
    if @ingredient.ndbn.present?
      @ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
    end

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

  # PATCH/PUT /ingredients/1
  def update
    ensure_owner @ingredient do
      @ingredient.assign_attributes(ingredient_params)
      if @ingredient.ndbn.present?
        @ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
      end

      respond_to do |format|
        if @ingredient.save
          format.html { redirect_to ingredients_path, notice: 'Ingredient was successfully updated.' }
          format.json { render :show, status: :ok, location: @ingredient }
        else
          format.html { render :edit }
          format.json { render json: @ingredient.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  # DELETE /ingredients/1
  # DELETE /ingredients/1.json
  def destroy
    ensure_owner @ingredient do
      @ingredient.destroy
      respond_to do |format|
        format.html { redirect_to ingredients_url, notice: 'Ingredient was successfully destroyed.' }
        format.json { head :no_content }
      end
    end
  end

  def select_ndbn
    if params[:id].present?
      @ingredient = Ingredient.find(params[:id])
    else
      @ingredient = Ingredient.new
    end

    @ingredient.assign_attributes(ingredient_params)

    if @ingredient.ndbn.present?
      @ingredient.set_usda_food(UsdaFood.find_by_ndbn(@ingredient.ndbn))
    end

    respond_to do |format|
      format.js {}
    end
  end

  def prefetch
    @ingredients = Ingredient.all.order(:name)
    render :search
  end

  def search
    @ingredients = Ingredient.search(params[:query]).order(:name)
  end

  def convert
    @conversion = Conversion.new(conversion_params)

    if @conversion.valid?
      @output_quantity = @conversion.output_quantity
      @conversion = Conversion.new
    else
      @output_quantity = ''
    end
  end

  def usda_food_search
    @foods = UsdaFood.search(params[:query]).limit(50).order(:long_description)

    respond_to do |format|
      format.html { render :layout => false }
      format.json { }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def ingredient_params
      params.require(:ingredient).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :calcium, :sodium, :vit_k, :ingredient_units_attributes => [:name, :gram_weight, :id, :_destroy])
    end

    def conversion_params
      params.require(:conversion).permit(:input_quantity, :input_units, :scale, :output_units, :ingredient_id)
    end
end
