function Recipe(attributes) {
  this.name = attributes.name;
  this.content = attributes.content;
  this.id = attributes.id;
  this.user_id = attributes.user_id;
  this.ingredients = attributes.ingredients;
  this.comments = attributes.comments;
}

Recipe.prototype.renderDisplay = function() {
  return Recipe.template(this)
}

Recipe.setTemplate = function() {
  Recipe.templateSource = $('#recipe-template').html()
  Recipe.template = Handlebars.compile(Recipe.templateSource);
}

Handlebars.registerHelper('list', function(ingredients, options) {
  var out = "<ul>";
  for(var i=0, l=ingredients.length; i<l; i++) {
    out = out + "<li>" + options.fn(ingredients[i]) + "</li>";
  }
  return out + "</ul>";
});

Handlebars.registerHelper('recipeComments', function(comments, options) {
  var out = ""
  for(var i=0, l=comments.length; i<l; i++) {
    out = out + "<p>" + options.fn(comments[i]) + "</p>";
  }
  return out;
});

Recipe.success = function(data) {
  var recipe = new Recipe(data["recipe"]);
  var recipeDisplay = recipe.renderDisplay()

  $('#recipeResults').append(recipeDisplay)
}

Recipe.error = function(response) {
  console.log("Error!", response)
}

Recipe.formSubmit = function(event) {
  event.preventDefault();
  Recipe.setTemplate()
  var $form = $(this);
  var action = $form.attr("action");
  var params = $form.serialize();
  var posting = $.post(action, params);

    posting.success(Recipe.success)
    .error(Recipe.error)
  }

Recipe.nextRecipe = function() {
      var nextId = parseInt($(".js-next").attr("data-id")) + 1;
      $.get("/recipes/" + nextId + ".json", function(data) {
        Recipe.setTemplate()
        var recipe = new Recipe(data["recipe"]);
        var recipeDisplay = recipe.renderDisplay()
        $('#recipeResults').html(recipeDisplay)

         $(".comments").text("");
         var recipeId = data["recipe"]["id"]
        $(".commentLink").html(`<a href="/recipes/${recipeId}/comments/new">Add comment</a>`)

        $(".js-next").attr("data-id", recipe["id"]);
      });
    }


Recipe.more = function() {
        var id = $(this).data("id")

        $.get("/recipes/" + id + ".json", function(data) {
          Recipe.setTemplate()
          var recipe = new Recipe(data["recipe"]);
          var recipeDisplay = recipe.renderDisplay()
          $('#recipeResults-' + id).html(recipeDisplay)

          // var recipe = data["recipe"]
          // $('#recipe-' + id).html("<p>Content: " + recipe["content"] + "</p>")
          //
          // recipe["ingredients"].forEach(function(i) {
          //   var ingredientsText = "Item: " + i["item"]["name"] + " Quantity: " + i["quantity"] + " " + i["unit"] + "<br>"
          //   $("#recipe-" + id + "-ingredients").append(ingredientsText);
          // })
          //
          // recipe["comments"].forEach(function(c) {
          //   var commentText = "<p>"
          //   commentText += c.content
          //   commentText += "~by <strong>" + c.user.email + "</strong> <br>"
          //   commentText += "</p>"
          //   $("#recipe-" + id + "-comments").append(commentText);
          // })
        })
      }

$(function () {
  $('form#new_recipe').submit(Recipe.formSubmit)
  $(".js-next").on("click", Recipe.nextRecipe)
  $('.js-more').on("click", Recipe.more)
})

@@ -2354,766 +2354,8 @@
 lay)
-%0A%0A          // var recipe = data%5B%22recipe%22%5D%0A          // $('#recipe-' + id).html(%22%3Cp%3EContent: %22 + recipe%5B%22content%22%5D + %22%3C/p%3E%22)%0A          //%0A          // recipe%5B%22ingredients%22%5D.forEach(function(i) %7B%0A          //   var ingredientsText = %22Item: %22 + i%5B%22item%22%5D%5B%22name%22%5D + %22 Quantity: %22 + i%5B%22quantity%22%5D + %22 %22 + i%5B%22unit%22%5D + %22%3Cbr%3E%22%0A          //   $(%22#recipe-%22 + id + %22-ingredients%22).append(ingredientsText);%0A          // %7D)%0A          //%0A          // recipe%5B%22comments%22%5D.forEach(function(c) %7B%0A          //   var commentText = %22%3Cp%3E%22%0A          //   commentText += c.content%0A          //   commentText += %22~by %3Cstrong%3E%22 + c.user.email + %22%3C/strong%3E %3Cbr%3E%22%0A          //   commentText += %22%3C/p%3E%22%0A          //   $(%22#recipe-%22 + id + %22-comments%22).append(commentText);%0A          // %7D)
 %0A   

delete comments