class FoodsController < ApplicationController

  before_action :set_food, only: [:show, :update, :destroy]

  before_action :ensure_valid_user, except: [:index, :show]

  # GET /foods
  # GET /foods.json
  def index
    @foods = Food.all.order(:name).page(params[:page]).per(params[:per])
    if params[:name].present?
      @foods = @foods.matches_tokens(:name, params[:name].split.take(4))
    end
    render json: FoodSummarySerializer.for(@foods, collection_name: 'foods')
  end

  def show
    render json: FoodSerializer.for(@food)
  end

  # POST /foods
  # POST /foods.json
  def create
    @food = Food.new(food_params)
    @food.user = current_user
    if @food.ndbn.present?
      @food.set_usda_food(UsdaFood.find_by_ndbn(@food.ndbn))
    end

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

  # PATCH/PUT /foods/1
  def update
    ensure_owner @food do
      @food.assign_attributes(food_params)
      if @food.ndbn.present?
        @food.set_usda_food(UsdaFood.find_by_ndbn(@food.ndbn))
      end

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

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

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

    @food.assign_attributes(food_params)

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

    render json: FoodSerializer.for(@food)
  end

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

    render json: UsdaFoodSerializer.for(@foods)
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def food_params
      params.require(:food).permit(:name, :notes, :ndbn, :density, :water, :protein, :lipids, :carbohydrates, :kcal, :fiber, :sugar, :calcium, :sodium, :vit_k, :ash, :iron, :magnesium, :phosphorus, :potassium, :zinc, :copper, :manganese, :vit_c, :vit_b6, :vit_b12, :vit_a, :vit_e, :vit_d, :cholesterol, :food_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
