{{/*
This file is part of SystemTestPortal.
Copyright (C) 2017  Institute of Software Technology, University of Stuttgart

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

SystemTestPortal 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with SystemTestPortal.  If not, see <http://www.gnu.org/licenses/>.
*/}}

{{define "tab-content"}}
<div class="modal fade" id="helpModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Test Case Start</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span>&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <table class="help-table">
                    <!-- Will be generated by js -->
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<div class="tab-card card" id="tabTestCases">
    <nav class="navbar navbar-light action-bar p-3">

        <button class="btn btn-danger mr-2" id="buttonAbort" title="Stop execution and discard protocol.">
            <i class="fa fa-times" aria-hidden="true"></i>
            <span class="d-none d-sm-inline"> Abort</span>
        </button>
        {{ if and .isInSequence (not .isSequence) }}
        <button class="btn btn-secondary mr-2 disabled" data-toggle="tooltip" data-placement="bottom" 
            id="buttonBackward" title="This feature will be available in a future version">
            <i class="fa fa-step-backward" aria-hidden="true"></i>
            <span class="d-none d-sm-inline"> Previous</span>
        </button>
        {{end}}
        <button class="btn btn-success mr-4" id="buttonExecuteFirstTestStep"
                title="Start {{if .isSequence}}testsequence{{else}}testcase{{end}} execution with first
                                {{if .isSequence}}testcase{{else}}teststep{{end}}.">
            <i class="fa fa-step-forward" aria-hidden="true"></i>
            <span class="d-none d-sm-inline"> Next</span>
        </button>
        <div class="progress mr-4" style="flex-grow: 100;">
            <div class="progress-bar" role="progressbar" style="width: {{ .ProgressBarPercent }}%;"
                 aria-valuenow="{{ .ProgressBarPercent }}" aria-valuemin="0" aria-valuemax="100">
            {{if ne .ProgressBarPercent 0}}{{ .ProgressBarCurrent }}/{{ .ProgressBarTotal }}{{end}}
            </div>
        </div>
        <span class="navbar-text" id="timeTracker">
            <span id="timeHours">{{ .hours }}</span>:<span id="timeMinutes">{{ .minutes }}</span>:<span
                id="timeSeconds">{{ .seconds }}</span>
            </span>
        <button class="btn btn-sm btn-secondary ml-2" id="buttonPause" title="Resume/Pause the execution timer.">
            <i class="fa fa-pause buttonPauseIcon" aria-hidden="true" id="buttonPauseIcon"></i>
        </button>
    </nav>
    <div class="row tab-side-bar-row">
        <div class="col-md-9 p-3">
            <h4 class="mb-3"> {{ .TestObject.Name }} </h4>
            <div class="form-group">
                <label><strong>Description</strong></label>
                <p class="text-muted">
                {{ with .TestObjectVersion.Description }}
                        {{ . }}
                    {{ else }}
                    No Description
                {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Make sure that the following preconditions are met</strong></label>
                <p class="text-muted">
                {{ with .TestObjectVersion.Preconditions }}
                    {{ . }}
                    {{ else }}
                    No Conditions
                {{ end }}
                </p>
            </div>
            <div class="form-group">
                <div class="form-group">
                    <label><strong>Conditions</strong></label>
                {{ if .isSequence }}
                {{ if .TestObjectVersion.SequenceInfo.Variants }}
                    <div class="form-group">
                        <div class="form-group">
                            <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                <label for="inputTestCaseSUTVariants">
                                    <strong>Variants</strong>
                                </label>
                            </div>
                            <div class="col-10 col-sm-11 col-md-12 col-lg-12">
                                <select class="custom-select mb-2" id="inputTestObjectSUTVariants"
                                        title="Select the variant of the system you're testing with.">
                                </select>
                            </div>
                            <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                <label for="inputTestCaseSUTVersions">
                                    <strong>Versions</strong>
                                </label>
                            </div>
                            <div class="form-group col-10 col-sm-11 col-md-12 col-lg-12">
                                <select class="custom-select mb-2" id="inputTestObjectSUTVersions"
                                        title="Select the version of the system you're testing with.">
                                </select>
                            </div>
                        </div>
                        <input type="hidden" class="" id="inputTestCaseNumber" value="{{ .case }}">
                    </div>
                {{ else }}
                    <div class="alert alert-warning alert-dismissible">
                        <a class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Warning!</strong> This test is not applicable to any system-under-test versions!
                    </div>
                {{ end }}
                {{ else  if .TestObjectVersion.Variants }}
                    <div class="form-group">
                        <div class="form-group">
                            {{ if ge .case 0 }}
                                <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                    <label for="inputTestCaseSUTVariants">
                                        <strong>Selected Variant</strong>
                                    </label>
                                </div>
                                <p class="text-muted col-10 col-sm-11 col-md-12 col-lg-12">
                                    {{ .inputSUTVariant }}
                                </p>
                                <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                    <label for="inputTestCaseSUTVersions">
                                        <strong>Selected Version</strong>
                                    </label>
                                </div>
                                <p class="text-muted col-10 col-sm-11 col-md-12 col-lg-12">
                                    {{ .inputSUTVersion }}
                                </p>
                            {{ else }}
                                <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                    <label for="inputTestCaseSUTVariants">
                                        <strong>Variants</strong>
                                    </label>
                                </div>
                                <div class="col-10 col-sm-11 col-md-12 col-lg-12">
                                    <select class="custom-select mb-2" id="inputTestObjectSUTVariants"
                                            title="Select the version of the system you're testing with.">
                                    </select>
                                </div>
                                <div class="col-2 col-sm-1 col-md-12 col-lg-12">
                                    <label for="inputTestCaseSUTVersions">
                                        <strong>Versions</strong>
                                    </label>
                                </div>
                                <div class="form-group col-10 col-sm-11 col-md-12 col-lg-12">
                                    <select class="custom-select mb-2" id="inputTestObjectSUTVersions"
                                            title="Select the version of the system you're testing with.">
                                    </select>
                                </div>
                            {{ end }}
                        </div>
                        <input type="hidden" class="" id="inputTestCaseNumber" value="{{ .case }}">
                    </div>
                {{ else }}
                    <div class="alert alert-warning alert-dismissible">
                        <a class="close" data-dismiss="alert" aria-label="close">&times;</a>
                        <strong>Warning!</strong> This test is not applicable to any system-under-test versions!
                    </div>
                {{ end }}
                </div>
            </div>
        </div>
        <div class="col-md-3 p-3 tab-side-bar">
            <div class="form-group">
                <label><strong>Estimated Test Duration</strong></label>
                <p class="text-muted">
                {{ if and (eq .EstimatedHours 0) (eq .EstimatedMin 0) }}
                    No Test Duration
                {{ else }}
                {{ if gt .EstimatedHours 0 }}
                {{ .EstimatedHours }}
                {{ if eq .EstimatedHours 1 }} Hour {{ else }} Hours {{ end }}
                {{ end }}
                {{ if gt .EstimatedMin 0 }}
                {{ .EstimatedMin }}
                {{ if eq .EstimatedMin 1 }}
                    Minute
                {{ else }}
                    Minutes
                {{ end }}
                {{ end }}
                {{ end }}
                </p>
            </div>
            <div class="form-group">
                <label><strong>Number of Test {{ if .isSequence }}Cases{{ else }}Steps{{ end }}</strong></label>
                <p class="text-muted">
                {{ if .isSequence }}
                    {{ len .TestObjectVersion.Cases }}
                    {{ else }}
                    {{ len .TestObjectVersion.Steps }}
                    {{ end }}
                </p>
            </div>
        {{ with .TestObject }}
            <div class="form-group">
                <label><strong>Labels</strong></label>
                <p>
                {{ range .Labels }}
                    <span class="badge badge-primary">
                    {{ .Name }}
                    </span>
                {{ else }}
                    <span class="text-muted">No Labels</span>
                {{ end }}
                </p>
            </div>
        {{ end }}
        </div>
    </div>
    <script src="/static/js/project/execute.js"></script>
    <script src="/static/js/project/sut-versions-show.js"></script>
    <script src="/static/js/util/common.js"></script>
    <script src="/static/js/util/generate_help.js"></script>
    <script>
        //Initializes the Execution buttons
        initializeExecutionClickListener();

        // Listen to changes in the drop down menu of the variants
        setVariantDropdownOnChangeListener({{ if .isSequence }}true{{ else }} false{{ end }});


        // Send a request to get the sut variants and versions
        var url = getTestURL().appendSegment("json").toString();

        // Request test case or test sequence to get the sut versions from it
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, true);
        xmlhttp.send();
        xmlhttp.onreadystatechange = function () {
            if (this.readyState === 4 && this.status === 200) {
                testObject = JSON.parse(this.responseText);
                tovn = ({{ .TestObjectVersion.VersionNr }});
            {{ if .isSequence }}
                testObjectVersionData = {{ .TestObjectVersion.SequenceInfo.Variants }}
            {{ else }}
                tolength = (testObject.TestCaseVersions).length;
                testObjectVersionIndex = tovn - tolength;
                testObjectVersionData = testObject.TestCaseVersions[testObjectVersionIndex].Variants;
            {{ end }}

                // Fill the drop down menu with the variants
                fillVariants(testObjectVersionData, "#inputTestObjectSUTVariants");
                // Update the version list with the versions of the selected variant
            updateVersionDropdown(testObjectVersionData, "#inputTestObjectSUTVariants", {{ if .isSequence }}true{{ else }} false{{ end }})
                ;
            }
        };

        // Generate help
        $( document ).ready(function() {
            generateHelp($("#tabTestCases"));
        });
    </script>
</div>
{{ end }}