{% extends "admin/change_list.html" %}

{% load url from future %}

{% block object-tools %}
  <div>
    <span id="task_selector"></span>
    <form id="task_data"></form>
    <a href="#" id="task_launch">Launch task</a>
  </div>

  <script type="text/javascript">
    (function($){
      $(document).ready(function() {

        $.get("{% url "registered_tasks" %}", function (data) {
          var selector = $("<select/>");
          selector.append(
            $("<option/>").text("-------------").attr("selected", "selected")
          );
          $.each(data.tasks, function(task) {
            var t = data["tasks"][task];
            selector.append(
              $("<option/>").val(task).text(task)
            )
          });

          selector.change(function() {
            var task = data["tasks"][$(this).val()];
            $("#task_data").empty();

            if (task.args) {
              $.each(task.args, function(i) {
                var arg = task.args[i]
                  , arg_input = $("<input/>").attr("name", arg)
                  ,  default_arg;

                if (task.defaults) {
                  var default_arg_i = task.args.length - 1 - i;
                  if (task.defaults[default_arg_i] !== undefined) {
                    default_arg = task.defaults[default_arg_i];
                    arg_input.val(default_arg);

                    if (task.defaults_types && task.defaults_types[default_arg_i] === "bool") {
                      arg_input.attr("type", "checkbox").attr("checked", default_arg).val("true");
                    }
                    else {
                      return true;
                    }
                  }
                }

                $("#task_data").append(
                  $("<label/>").text(arg + ": ").attr("for", arg)
                ).append(
                  arg_input
                );
              });
            }
          })

          $("#task_selector").append(selector);
        });

        $("#task_launch").click(function() {
            var task_name = $("#task_selector").find("select").val();
            var data = $("#task_data").serialize();
            var url = "{% url "apply_task" "task_name" %}";

            $.post(url.replace("task_name", task_name), data, function() {
              alert("Submitted!");
              location.reload();
            });
        });

        setTimeout(location.reload, 30000);

      });
    })(django.jQuery);
  </script>
{% endblock %}
