{% load i18n %}
{% load common_tags %}

{% comment %}
<!--
  Copyright (C) 2011 University of Dundee & Open Microscopy Environment.
  All rights reserved.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Affero General Public License as
  published by the Free Software Foundation, either version 3 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
{% endcomment %}


<script>

    if (typeof OME === "undefined"){ OME = {}}
    OME.showActivities = function() {
        $("#activities_panel").show();
        OME.displayStatus(-1);  // reset new_results
        OME.refreshActivities();
    }

    // this is called by the setInterval loop below
    OME.activitiesUpdate = function() {
        var i = OME.activitiesInterval;
        $.get("{% url 'activities' %}", function(data) {
            $('#activities_spinner').hide();
            var inprogress = $("#inprogress", data).text();
            var new_results = $("#new_results", data).text();
            var failure = $("#failure", data).text();
            
            // if we've got no jobs still running, stop checking
            if ((typeof inprogress == 'undefined') || (inprogress.length == 0)) {
                if (i) clearInterval(i);
                OME.activitiesInterval = undefined;
                showJobCount(0);
                //return;
            }
            inprogress = parseInt( inprogress );
            if (inprogress==0) {
                if (i != undefined) {
                    clearInterval(i);
                    OME.activitiesInterval = undefined;
                }
            }
            
            // display what we recieved, bind events etc
            $("#activities_content").empty();
            $("#activities_content").append( $("#jobsTable", $(data)) );
            $("#jobsTable").alternateRowColors();
            
            // bind events
            $("#activities_content a").on('click', function() {
                var href = $(this).attr('href');
                if (href) {
                    window.location.href = href;
                }
            });
            // show chgrp errors in new window
            $(".chgrp_error a").on('click', function(event) {
                var error_msg = $(this).next().html();
                var newWindow=window.open('','','height=500,width=500,scrollbars=yes, top=50, left=100');
                newWindow.document.write(error_msg);
                newWindow.document.close();
                return false;
            });

            // If we've got new errors - show Activities
            if ($("#new_errors", data).length > 0) {
                $("#activities_panel").show();
                new_results = -1;   // hide notification number
            }
            OME.displayStatus(new_results, inprogress);

        }).fail(function() {
            // this requires jQuery 1.5 or later
            clearInterval(i);
        });
    }

    // Add new results to the count, unless we have the Activities panel open
    OME.displayStatus = function(newresults, inprogress) {
        // update inprogress 'spinner'
        if (typeof inprogress != 'undefined') {
            if ((inprogress == '0') || (inprogress == 0)) {
                $("#runningStatus").hide();
            } else {
                $("#runningStatus").show();
            }
        }
        
        // update result count
        var results_count = parseInt($('#jobstatus').text()) || 0;
        if (typeof newresults != 'undefined') {
            var new_results = parseInt(newresults);
            if (new_results < 0) results_count = 0  // clear
            else results_count += new_results
        }
        // only display if we're not showing activities
        if ((results_count > 0) && !$("#activities_panel").is(":visible")) {
            $('#jobstatus').show();
        } else {
            results_count = 0;
            $('#jobstatus').hide()
        }
        $('#jobstatus').text(results_count + '');
        
        // if neither flag is showing, fade out the main icon
        if ($('#jobstatus').is(":visible") || $('#runningStatus').is(":visible")) {
            // turning off, see #9154 $("#launch_activities").css('opacity', 1.0);
        } else {
            // turning off, see #9154 $("#launch_activities").css('opacity', 0.5);
        }
    }

    OME.refreshActivities = function() {
        if (OME.activitiesInterval) {
            return;
        }
        OME.activitiesInterval = setInterval(function (){
                OME.activitiesUpdate();
        }, 5000);
        OME.activitiesUpdate();
    }
    $(document).ready(function() {
        OME.displayStatus(-1);  // reset new_results
        OME.refreshActivities();
        $("#activities_panel").hide();
        
        // add click handler to entire body, to close the activities panel 
        $('body').on('click', function(event) {
            var id = event.target.id;
            //$(".info_panel").hide();
            if ((id == 'launch_activities') || (id == 'jobstatus') || (id == 'runningStatus')) {
                OME.showActivities();
            } else {
                $("#activities_panel .new_result").removeClass('new_result');
                $("#activities_panel").hide();
            }

        }); // BUT also stop any events bubbling up from panel itself
        $("#activities_panel").on('click', function(event) {
            return false;
        });
        
        $("#clear_activities").on('click', function() {
            $.post("{% url 'activities_update' action='clean' %}");
            $("#jobsTable>tbody>tr").filter(":not(.in_progress)").remove();
        });

        // close panels on 'ESCAPE'
        $("body").on('keyup', function(event){
            if (event.keyCode === $.ui.keyCode.ESCAPE) {
                $(".info_panel").hide();
            }
        });

        // If a chgrp fails due to filesets, we provide a 'move all' button:
        $("#activities_content").on("click", ".chgrp_move_all", function(event){
            var $this = $(this);
            if ($this.prop('disabled')){
                return false;
            }
            $this.prop('disabled', true);
            $.get($this.attr('url'), function(){
                OME.refreshActivities();
            });
            event.preventDefault();
            return false;
        });
    });
</script>


        <li id="queue">
			
            <span class="toolbar_button" id="launch_activities" title="Activities">
                <span id="jobstatus" class="notifier">0</span>
                <span id="runningStatus" style="display:none;"></span>
            </span>
            
            <div id="activities_panel" class="info_panel">

			<div class="info_panel_title">
			
                <h3>
                    Activities
                    <img id='activities_spinner' src="{% static "webgateway/img/spinner.gif" %}"/>
                    
                </h3>
             <button type="button" id="clear_activities">Clear List</button>
			 </div>
				
                <div id="activities_content">
                </div>
                
            </div>
        </li>
