{% extends 'layout.html' %}
{% block styles %}
<link rel="stylesheet" href="/static/highlight.js/styles/default.min.css">
{% endblock %}
{% block content %}
<div id="accordion">
    <div class="col-lg-12">
        {% include 'dynamic/widgets/deviceview.html' %}
        {% include 'dynamic/widgets/frida_appctl.html' %}
        {% include 'dynamic/widgets/proxyctl.html' %}
        {% include 'dynamic/widgets/devicelog.html' %}
    </div>
</div>
{% endblock content %}

{% block subscripts %}
<script src="/static/highlight.js/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
<script>
    function send_text() {
        var text_to_send = $('#send-text').val();
        $.getJSON("{{ url_for('adb_send_text', device_uuid=device_uuid) }}?text="+encodeURIComponent(text_to_send), function(data){

        });
    }
    function start_proxy() {
        $.getJSON('{{ url_for('dynamic_start_proxy', application=application.identifier) }}', function(data){
            if(!data.status) {
                alert("Unable to start proxy. Please consult the application logs for more details");
            } else { window.location.reload(); }
        });
    }
    function kill_proxy() {
        $.getJSON('{{ url_for('dynamic_stop_proxy', application=application.identifier) }}', function(data){
            if(!data.status) {
                alert("Unable to kill proxy. Please consult the application logs for more details");
            } else { window.location.reload(); }
        });
    }

    function screenshot() {
        $.getJSON('{{ url_for('dynamic_screenshot', application_id=application.identifier, device_uuid=device_uuid) }}', function(data){
            if(data.status) {
                alert("Screenshot saved.");
            } else { alert("Unable to create or save screenshot."); }
        });
    }

    function start_collector() {
        $.getJSON('{{ url_for('dynamic_start_collector', application=application.identifier) }}', function(data){
            if(!data.status) {
                alert("Unable to start collector. Please consult the application logs for more details");
            } else { window.location.reload(); }
        });
    }
    function kill_collector() {
        $.getJSON('{{ url_for('dynamic_stop_collector', application=application.identifier) }}', function(data){
            if(!data.status) {
                alert("Unable to kill collector. Please consult the application logs for more details");
            } else { window.location.reload(); }
        });
    }

    function install_certificate() {
        $.getJSON('{{ url_for('dynamic_install_cert', device_type=device_type, device_uuid=device_uuid) }}', function(data){
            if(!data.status) {
                alert("Error installing certificate, please check application logs");
            } else {
                if("{{ device_type }}" == "systemless") {
                    if (confirm('Systemless root requires the device to be rebooted after deploying a new certificate. Reboot now?')) {
                        setTimeout(function() {
                            $.getJSON('{{ url_for('adb_reboot', device_uuid=device_uuid) }}', function(data){
                                document.location = '{{ url_for('dynamic_rooted_setup', device_type=device_type) }}';
                            });
                        }, 100);
                    } else {
                        window.location.reload();
                    }
                }
             }
        });
    }

    function spawn_application() {
        var base_url = "{{ url_for('spawn_frida_application', device_uuid=device_uuid, application=application.identifier) }}";
        if($('#kafka-running').is(":checked")) {
            base_url += "?async=1";
        }
        $.getJSON(base_url, function(data){
            if(!data.status) {
                alert("Error installing certificate, please check application logs");
            } else {  }
        });
    }

    function kill_application() {
        var kill_url = '{{ url_for('frida_kill_application', device_uuid=device_uuid, application=application.name) }}';
        $.getJSON(kill_url, function(data){
            if(!data.status) {
                alert(data.error);
            } else {
                window.location.reload();
            }
        });
    }

    function remove_certificate() {
        $.getJSON('{{ url_for('dynamic_remove_cert', device_type=device_type, device_uuid=device_uuid) }}', function(data){
            if(!data.status) {
                alert("Error removing certificate, please check application logs");
            } else { window.location.reload(); }
        });
    }

    function enable_proxy() {
        $.getJSON('{{ url_for('dynamic_enable_device_proxy', device_type=device_type, device_uuid=device_uuid) }}', function(data){
            if(!data.status) {
                alert("Error enabling device proxy, please check application logs");
            } else { window.location.reload(); }
        });
    }
    function disable_proxy() {
        $.getJSON('{{ url_for('dynamic_disable_device_proxy', device_type=device_type, device_uuid=device_uuid) }}', function(data){
            if(!data.status) {
                alert("Error disabling device proxy, please check application logs");
            } else { window.location.reload(); }
        });
    }
    last_logs = [];
    function read_logs() {
        if(!document.hasFocus()) {
            setTimeout(read_logs, 4000);
            return;
        }
        $.getJSON("{{ url_for('get_logs_json') }}", function(data){
            $('#log-data').html("");
            $.each(data, function(key, value){
                $('#log-data').append('<tr><td>'+value.added+'</td><td>'+value.key+'</td><td>'+$('<div/>').text(value.data).html()+'</td></tr>');
            });
        });
        setTimeout(read_logs, 4000);
    }
    read_logs();
</script>
{% endblock %}