<!--
Copyright 2019 EveryUP srl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an  BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

{% extends "descriptor_view_base.html" %}

{% load staticfiles %}


{% block title_header_big %}
{{ block.super }}
Edit {{ descriptor_type|upper }} Descriptor
{% endblock %}


{% block left_sidebar %}
{% include 'osm/osm_project_left_sidebar.html' %}
{% endblock %}

{% block breadcrumb_body %}
{{ block.super }}
{% if user.is_admin %}
        <li><a href="{% url 'projects:projects_list' %}">Projects</a></li>
{% else%}
        <li><a href="#">Projects</a></li>
{% endif %}
<li><a href="{% url 'projects:open_project' %}">{{project_name}}</a></li>
<li><a>{{descriptor_id}}</a></li>
{% endblock %}

{% block tab_pane_button_list %}
{{ block.super }}
<li class="active" id="yaml_li"><a href="#yaml" data-toggle="tab"><i class="fa fa-file-code-o"></i> YAML</a></li>
<li id="json_li"><a href="#json" data-toggle="tab"><i class="fa fa-file-code-o"></i> JSON</a></li>
{% endblock %}

{% block nav_buttons_list %}
    {{ block.super }}
    <li class="pull-right"><button id="save" type="button" class="btn btn-block btn-primary btn-sm"  onclick="update(this.id)" ><i class="fa fa-save"></i> Update</button></li>
    <li class="pull-right"><button id="save_show_graph" type="button" class="btn btn-block btn-primary btn-sm"  onclick="update(this.id, 'true')"><i class="fa fa-save"></i> Update and Show Graph</button></li>
    <li class="pull-right"><button type="button" class="btn btn-block btn-primary btn-sm"  onclick="goToGraph()"><i class="fa fa-sitemap"></i> Show Graph</button></li>

{% endblock %}

{% block tab_pane_list %}
{{ block.super }}

<!-- /.tab-pane -->
<div class="active tab-pane" id="yaml">
    <textarea id="code_editor_yaml">
    </textarea>
</div>
<!-- /.tab-pane-->

<div class="tab-pane" id="json">
    <textarea id="code_editor_json">
    </textarea>
</div>

{% endblock %}

{% block resource_block %}
{{ block.super }}


<script>
    var editorJSON;
    var editorYaml;

    $(document).ready(function () {
        //var cmjsoneditor = CodeMirror.fromTextArea(document.getElementById("code_editor"), json_editor_settings);
        var myJsonTextArea = document.getElementById("code_editor_json");
        editorJSON = CodeMirror(function (elt) {
            myJsonTextArea.parentNode.replaceChild(elt, myJsonTextArea);
        }, json_editor_settings);
        editorJSON.setValue(JSON.stringify({{descriptor_strings.descriptor_string_json | safe}}, null, "\t"));
        editorJSON.setOption("autoRefresh", true);
        editorJSON.setSize("auto", "auto");

        var myYamlTextArea = document.getElementById("code_editor_yaml");
        editorYaml = CodeMirror(function (elt) {
            myYamlTextArea.parentNode.replaceChild(elt, myYamlTextArea);
        }, yaml_editor_settings);
        var des_strings = {{descriptor_strings | safe}};
        editorYaml.setValue(des_strings.descriptor_string_yaml);
        editorYaml.setSize("auto", "auto");
    });


    function update(e, show_graph) {
        var dialog = bootbox.dialog({
            message: '<div class="text-center"><i class="fa fa-spin fa-spinner"></i> Loading...</div>',
            closeButton: false
        });

        var id = $('.nav-tabs .active').attr('id');
        var type, text;
        switch (id) {
            case 'yaml_li':
                type = 'yaml';
                text = editorYaml.getValue();
                break;
            case 'json_li':
                type = 'json';
                text = editorJSON.getValue();
                break;
        }
        $.ajax({
            url: "/projects/descriptors/{{descriptor_type}}/{{descriptor_id}}/",
            type: 'POST',
            dataType: 'json',
            data: {
                'csrfmiddlewaretoken': '{{csrf_token}}',
                'type': type,
                'text': text
            },
            success: function (result) {
                if(show_graph){
                    goToGraph();
                }
                dialog.modal('hide');
                $("#success-alert").fadeTo(2000, 500).slideDown(500, function(){
                    setTimeout(function () {
                        $("#success-alert").slideUp(500);
                    }, 2000);

                });
            },
            error: function (result) {
                dialog.modal('hide');
                var data = result.responseJSON;
                var title = "Error " + (data && data.code ? data.code : 'unknown');
                var message = data && data.detail ? data.detail : 'No detail available.';
                bootbox.alert({
                    title: title,
                    message: message
                });
            }
        });
    }

</script>
{% endblock %}


{% block footer %}
    {% include "footer.html" %}
{% endblock %}
