class NutritionData

  NUTRIENTS = {
      protein: 'g Protein',
      lipids: 'g Fat',
      kcal: 'Calories',
      fiber: 'g Fiber',
      sugar: 'g Sugar',
      carbohydrates: 'g Carbohydrates',
      calcium: nil,
      iron: nil,
      magnesium: nil,
      phosphorus: nil,
      potassium: nil,
      sodium: 'mg Sodium',
      zinc: nil,
      copper: nil,
      manganese: nil,
      vit_c: nil,
      vit_b6: nil,
      vit_b12: nil,
      vit_a: nil,
      vit_e: nil,
      vit_d: nil,
      vit_k: 'µg Vitamin K',
      cholesterol: nil
  }

  attr_reader :errors
  attr_reader *NUTRIENTS.keys

  def initialize(recipe_ingredients)
    @errors = []

    NUTRIENTS.keys.each do |n|
      self.instance_variable_set("@#{n}".to_sym, 0.0)
    end

    valid_ingredients = []

    recipe_ingredients.each do |i|
      if i.ingredient_id.nil?
        @errors << "#{i.name} has no nutrition data"
      elsif !i.can_convert_to_grams?
        @errors << "#{i.name} can't be converted to grams"
      else
        valid_ingredients << i
      end
    end

    valid_ingredients.each do |i|
      grams = i.to_grams
      missing = []

      NUTRIENTS.each do |k, n|
        value = i.ingredient.send(k)
        if value.present?
          value = value.to_f
          running_total = self.instance_variable_get("@#{k}".to_sym)
          delta = (grams / 100.0) * value
          self.instance_variable_set("@#{k}".to_sym, running_total + delta)
        else
          missing << k
        end
      end

      missing = missing.map { |k| NUTRIENTS[k] }.compact
      unless missing.empty?
        @errors << "#{i.name} missing the following nutrients: #{missing.join(', ')}"
      end
    end

    NUTRIENTS.each do |k, n|
      v = self.instance_variable_get("@#{k}".to_sym)
      self.instance_variable_set("@#{k}".to_sym, v.round(2))
    end

  end

end