{% load i18n %}
{% load common_filters %}


{% 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 type="text/javascript">
        $(document).ready(function(){
                var unset = function() {
                    var v = $(this).val();
                    return (v.length == 0 || v == 'N/A');
                }
                // hide any unset fields. 
                var invalidRows = $(".metadata_details").find("input, select")
                    .filter(unset)
                    .parent().parent();
                invalidRows.hide();
                // set up toggling... add click button
                var show_text = "Show unset fields";
                var hide_text = "Hide unset fields";
                var show_hide_html = "<a href='#' class='show_hide_invalid'>"+ show_text +"</a>";
                $(".metadata_details").append($(show_hide_html));
                var toggleInvalidRows = function(event) {
                    var $target = $(event.target);
                    if ($target.attr('class') == "show_hide_invalid") {
                        // toggle text
                        if ($target.text() == show_text) $target.text(hide_text);
                        else $target.text(show_text);
                        // toggle fields
                        var invalidRows = $(this).find("input, select")
                            .filter(unset)
                            .parent().parent();
                        invalidRows.toggle();
                        return false;
                    };
                };
                $(".metadata_details").on('click', toggleInvalidRows);


                // Handle click on collapsible panes
                $('#metadata_tab .can-collapse').on('click', function () {
                    $(this).toggleClass('closed').next().slideToggle();
                    // remember which panes are expanded
                    var open = [];
                    $('#metadata_tab .can-collapse').each(function(){
                        var $this = $(this);
                        if (!$this.hasClass('closed') && $this.attr('data-name')) {
                            open.push($this.attr('data-name'));
                        }
                    });
                    $("#metadata_tab").data('open_panes', open);
                });
                // Expand any previously opened panes
                var open_panes = $("#metadata_tab").data('open_panes');
                if (open_panes === undefined) {
                    open_panes = [];
                }
                open_panes.forEach(function(name) {
                    // remove 'defclose' flag from open panes
                    $('#metadata_tab .can-collapse[data-name="' + name + '"]').removeClass('defclose');
                });
                // Any panes that still have 'defclose' flag, we close
                $('#metadata_tab .can-collapse.defclose').removeClass('defclose').addClass('closed').next().hide();


                // Load Original Metadata...
                var origMetadataLoaded = false;
                function loadOriginalMetadata () {
                    if (!origMetadataLoaded) {
                        origMetadataLoaded = true;
                        var loadingHtml = '<img alt="Loading" src="{% static "webgateway/img/spinner.gif" %}">'
                                        + 'Loading Original Metadata...'
                        var url = $("#orig_metadata_link").prop('href');
                        $("#orig_metadata")
                            .html(loadingHtml)
                            .load(url, function( response, status, xhr ) {
                                if (xhr.status == 408) {
                                    origMetadataLoaded = false;
                                    $("#orig_metadata").html("Timeout. <a href='#' class='load_original_metadata'>Try again</a>");
                                } else {
                                    // setup quick search fields (but not if MANY rows, since it crashes!)
                                    if ($("#global_metadata tr").length < 60000) {
                                        $('input#search_global').quicksearch('table#global_metadata tbody tr', {
                                            'delay': 300,
                                            'loader': 'span.loading_global'
                                        });
                                    } else {
                                        $("#quicksearch_global").hide();
                                    }
                                    if ($("#series_metadata tr").length < 60000) {
                                        $('input#search_series').quicksearch('table#series_metadata tbody tr', {
                                            'delay': 300,
                                            'loader': 'span.loading_series'
                                        });
                                    } else {
                                        $("#quicksearch_series").hide();
                                    }
                                }
                            });
                    }
                };
                // Load either when we first expand tab, or on retry
                $("h1.load_original_metadata").on('click', function() {
                    if (!$(this).hasClass('closed')){
                        loadOriginalMetadata();
                    }
                });
                $("#orig_metadata" ).on( "click", "a.load_original_metadata", loadOriginalMetadata);
            });
    </script>

    <!-- METADATA "Acquisition" TAB -->
	
		
            
        {% if manager.well or manager.image%}
            
			<div class="right_tab_inner">
			
            {% if manager.companion_files %}
            <h1 class="can-collapse defclose" data-name="companionfiles">
                {% trans "Companion Files" %}
            </h1>
                <div>
                    {% for fileann in manager.companion_files %}
                    <p>
                        <a href="{% url 'download_annotation' fileann.id %}">
                            {{ fileann.getFileName|shortening:40 }}
                        </a>
                        ({{ fileann.getFileSize|default:0|filesizeformat }})
                    </p>
                    {% endfor %}
                </div>
            {% endif %}
            
            {% if not share_id %}
            <!-- Original Metadata is loaded when tab expanded -->
            <h1 class="can-collapse defclose load_original_metadata" data-name="originalmetadata">
                {% trans "Original Metadata" %}
            </h1>
            <div id="orig_metadata">
            </div>
            <!-- hidden link provides url -->
            <a id="orig_metadata_link" style="display: none"
                    {% if manager.well %}
                        href="{% url 'load_original_metadata' manager.well.getWellSample.image.id %}"
                    {% elif manager.image %}
                        href="{% url 'load_original_metadata' manager.image.id %}"
                    {% endif %}
                >Load Original Metadata</a>
            {% endif %}

            <!-- Microscope -->
            {% if form_objective or form_filters or form_detectors or form_microscope %}
            <h1 class="can-collapse defclose" data-name="microscope">
                {% trans "Microscope" %}
            </h1>
            <div>
                {% if form_microscope %}
                <table class="metadata_details">
                    {% for field in form_microscope %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endif %}
                {% for form_objective in form_instrument_objectives %}
                <table class="metadata_details">
                <tr><th><br/>{% trans "Objective" %}:</th><th colspan="2"></th></tr>
                    {% for field in form_objective %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endfor %}
                {% for form in form_filters %}
                <table class="metadata_details">
                    <tr><th><br/>{% trans "Filter" %}:</th><th colspan="2"></th></tr>
                    {% for field in form %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endfor %}
                {% for form in form_dichroics %}
                <table class="metadata_details">
                    <tr><th><br/>{% trans "Dichroic" %}:</th><th colspan="2"></th></tr>
                    {% for field in form %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endfor %}
                {% for form in form_detectors %}
                <table class="metadata_details">
                    <tr><th><br/>{% trans "Detector" %}:</th><th colspan="2"></th></tr>
                    {% for field in form %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endfor %}
                {% for form in form_lasers %}
                <table class="metadata_details">
                    <tr><th><br/>{{ form.lightSourceType }}:</th><th colspan="2"></th></tr>
                    {% for field in form %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endfor %}
            </div>
            {% endif %}
            
            <!-- Image -->
            {% if form_objective or form_environment or form_stageLabel %}
            <h1 class="can-collapse defclose" data-name="image">
                {% trans "Image" %}
            </h1>
            <div><table class="metadata_details">
                {% if form_objective %}
                <tr><th><br/>{% trans "Objective" %}:</th><th colspan="2"></th></tr>
                    {% for field in form_objective %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                {% endif %}
                {% if form_environment %}
                <tr><th><br/>{% trans "Environment" %}:</th><th colspan="2"></th></tr>
                    {% for field in form_environment %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                {% endif %}
                {% if form_stageLabel %}
                <tr><th><br/>{% trans "Stage label" %}:</th><th colspan="2"></th></tr>
                    {% for field in form_stageLabel %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                {% endif %}
            </table></div>
            {% endif %}
            
            <!-- Channels -->
            {% for ch in form_channels %}
            <h1 class="can-collapse defclose" data-name="channel{{forloop.counter}}">
                {% if ch.color %}
                    <span style="padding:0 3px; color:#000; border:1px solid #000; background-color: #{{ ch.color }};">&nbsp&nbsp</span>
                {% endif %}
                {{ ch.label }}
            </h1>
            <div id="channel_{{ forloop.counter }}">
                <table class="metadata_details">
                    {% for field in ch.form %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                    {% if ch.form_emission_filter or ch.form_excitation_filter %}
                    {% for f in ch.form_emission_filter %}
                    <tr><th><br/>{% trans "Filter" %}:</th><th colspan="2"></th></tr>
                    {% for field in f %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                    {% endfor %}
                    {% for f in ch.form_excitation_filter %}
                    {% for field in f %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                    {% endfor %}
                    {% endif %}
                </table>
                {% if ch.form_detector_settings or ch.form_light_source %}
                <table class="metadata_details">
                    {% if ch.form_detector_settings %}
                    <tr><th><br/>{% trans "Detector" %}:</th><th colspan="2"></th></tr>
                    {% for field in ch.form_detector_settings %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                    {% endif %}                
                    {% if ch.form_light_source %}
                    <tr><th><br/>{{ ch.form_light_source.lightSourceType }}:</th><th colspan="2"></th></tr>
                    {% for field in ch.form_light_source %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                    {% endif %}
                </table>
                {% endif %}
                <!-- Light Path... -->
                {% if ch.form_excitation_filters %}
                    {% for form in ch.form_excitation_filters %}
                    <table class="metadata_details">
                        <tr><th><br/>{% trans "Excitation Filter" %}:</th><th colspan="2"></th></tr>
                        {% for field in form %}
                        <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                            <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                    {% endfor %}
                {% endif %}

                {% if ch.form_dichroic %}
                <table class="metadata_details">
                    <tr><th><br/>{% trans "Dichroic" %}:</th><th colspan="2"></th></tr>
                    {% for field in ch.form_dichroic %}
                    <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                        <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                    </tr>
                    {% endfor %}
                </table>
                {% endif %}

                {% if ch.form_emission_filters %}
                    {% for form in ch.form_emission_filters %}
                    <table class="metadata_details">
                        <tr><th><br/>{% trans "Emission Filter" %}:</th><th colspan="2"></th></tr>
                        {% for field in form %}
                        <tr><td>{{ field.label_tag }}{% if field.field.required %}*{% endif %}</td>
                            <td>{{ field }}</td><td>{% if field.errors %}{{ field.errors }}{% endif %}<br/>{{ field.help_text|safe }}</td>
                        </tr>
                        {% endfor %}
                    </table>
                    {% endfor %}
                {% endif %}

                {% if ch.plane_info %}
                <h2>Exposure times</h2>
                <div class="plane_info">
                <table class="exposureTimeData"><tr>
                    <td>
                        <div>t</div>
                        <div>Delta T</div>
                        <div style="white-space:nowrap;">Exposure</div>
                    </td>
                    {% for pi in ch.plane_info %}
                        <td>
                            <div>t={{ pi.theT|add:"1" }} </div>
                            <div>{{ pi.deltaT|timeformat }} </div>
                            <div>{{ pi.exposureTime|timeformat }}</div>
                        </td>
                    {% endfor %}
                </tr></table>
                </div>
                {% endif %}
            </div>
            {% endfor %}
			
			</div>

        {% endif %}
		
		
