 # Created by Robert Xue on 1/18/15.
-bang = {}
+bang = null
+bangUri = null
+queryResult = null
 render = ->
   console.log "Bang will make your life with JSON easier!"
   root = d3.select("body").text("").append("div").attr("class", "container")
   renderHeader root.append("div").attr("class", "navbar navbar-default")
   queryRow = root.append("div").attr("class", "row")
-  navigatorRow = root.append("div").attr("class", "row")
   responseRow = root.append("div").attr("class", "row")
   renderQuery queryRow.append("div").attr("class", "col-lg-6 col-md-6 col-sm-12 col-xs-12").append("div").attr("class", "panel panel-default").attr("id", "queryPanel")
-  renderResult queryRow.append("div").attr("class", "col-lg-6 col-md-6 col-sm-12 col-xs-12").append("div").attr("class", "panel panel-default").attr("id", "resultPanel")
-  renderNavigator navigatorRow.append("div").attr("class", "col-lg-12 col-md-12 col-sm-12 col-xs-12").append("div").attr("class", "panel panel-success").attr("id", "navigatorPanel")
+  renderNavigator queryRow.append("div").attr("class", "col-lg-6 col-md-6 col-sm-12 col-xs-12").append("div").attr("class", "panel panel-success").attr("id", "navigatorPanel")
   renderResponse responseRow.append("div").attr("class", "col-lg-12 col-md-12 col-sm-12 col-xs-12").append("div").attr("class", "panel panel-success")
   $(".panel-heading").css({cursor: "pointer", "word-break": "break-all"}).click (ev)->
     $(ev.currentTarget).siblings(".panel-body").toggle()

  """

renderResponse = (root)->
  {data, uri} = bang
  header = root.append("div").attr("class", "panel-heading").html("Response from <code>#{uri}</code> stored into bang.data")
  root.append("div").attr("class", "panel-body").append("pre").text(JSON.stringify(data, null, 4))

renderQuery = (root)->
  root.append("div").attr("class", "panel-heading").text("Query")
  renderQueryForm root.append("div").attr("class", "panel-body")

renderResult = (root)->
  root.append("div").attr("class", "panel-heading").text("Result")
  root.append("div").attr("class", "panel-body").append("pre").attr("id", "queryResult")

renderNavigator = (root)->
  root.append("div").attr("class", "panel-heading").text("Response Navigator")
  panelBody = root.append("div").attr("class", "panel-body")
  panelBody.append("ul").attr("class", "breadcrumb")
  panelBody.append("div").attr("class", "form-inline")
  panelBody.append("pre")
  root.append("ul").attr("class", "list-group")
  root.append("table").attr("class", "table")
  updateNavigator ["bang.data"]

didRunQuery = ->
  query = $("#query").val()
  runQuery query

runQuery = (query)->
  try
    $("#query").val query
    result = eval query
    if result is undefined
      $("#queryResult").text "(undefined)"
      return {error: "(undefined)"}
    else
      $("#queryResult").text JSON.stringify(result, null, 4)
      return {result}
  catch ex
    $("#queryResult").text ex
    return {error: ex}

updateNavigator = (path)->

  </div>
</div>
""")
  if bang instanceof Array
    $("#query").val("_.size(bang)")
  else
    $("#query").val("_.keys(bang)")

  $("#runQuery").click didRunQuery

