{%- extends "base.html" -%}

{%- import 'macros.html' as macros -%}

{%- set active_page = "Upload" -%}
{%- set is_reanalysis = True if firmware else False -%}

{%- block styles -%}
<style>
    .search {
        text-align: right;
    }
    .btn-file {
        position: relative;
        overflow: hidden;
    }
</style>
{%- endblock -%}

{%- block body -%}

<div class="row justify-content-center">
    <div class="col-xl-6 col-lg-8 mt-4">
        {%- if is_reanalysis %}
            <h3 class="mb-3">{{ title }} of {{ firmware.uid | replace_uid_with_hid }}</h3>
        {%- else %}
            <h3 class="mb-3">Upload Firmware</h3>
        {%- endif %}

        <img id="map_img" src="{{ url_for('.static', filename='Pacman.gif') }}" style="display: none;"/>
        <script>
            function showImg() {
                document.getElementById("form").style.display = "";
            }
        </script>

        <div id="form">
            <form class="form-horizontal" action="" method=post enctype=multipart/form-data>

                {# File #}
                {%- if not is_reanalysis %}
                    {% call macros.upload_input("file", "File", error) %}
                        <input class="form-control-file" id="file" type="file" name=file required>
                    {% endcall %}
                {%- else %}
                    <input type="hidden" name="file_name" id="file_name" value="{{ firmware.file_name }}">
                {%- endif %}

                <script>
                    var device_names = {{ device_names | safe }};

                    function add_device_class_options(selected_device_class, selected_vendor, data) {
                        $('#device_name').empty();
                        if (data.hasOwnProperty(selected_device_class)) {
                            if (data[selected_device_class].hasOwnProperty(selected_vendor)) {
                                var device_classes = data[selected_device_class][selected_vendor];
                                device_classes.sort();
                                for (var key in device_classes) {
                                    if (device_classes.hasOwnProperty(key)) {
                                        $('#device_name').append('<option>' + device_classes[key] + '</option>');
                                    }
                                }
                            }
                        }
                        $('#device_name').append('<option>' + 'new entry' + '</option>');
                    }
                    function update_text_input(element, this_text_input) {
                        if (element.options[element.selectedIndex].value == 'new entry') {
                            this_text_input.style.display = 'initial';
                            this_text_input.value = ''
                        } else {
                            this_text_input.style.display = 'none';
                            this_text_input.value = element.options[element.selectedIndex].value
                        }
                    }
                    function update_device_names() {
                        var vendor_dropdown = document.getElementById('vendor');
                        var device_class_dropdown = document.getElementById('device_class');
                        if ((vendor_dropdown.selectedIndex != -1) && (device_class_dropdown.selectedIndex != -1)) {
                            document.getElementById('device_name').disabled = false;
                            var selected_device_class = device_class_dropdown.options[device_class_dropdown.selectedIndex].value;
                            var selected_vendor = vendor_dropdown.options[vendor_dropdown.selectedIndex].value;
                            add_device_class_options(selected_device_class, selected_vendor, device_names);
                        }
                    }
                </script>

                {# Device Class #}
                {%- call macros.upload_input("device_class", "Device Class", error) %}
                    <select class="form-control" id="device_class" name='device_class_dropdown' size="4" required
                            onchange="update_text_input(this, document.getElementById('class_text')); update_device_names();">
                    {%- if is_reanalysis %}
                        <option value='{{ firmware.device_class }}' selected>{{ firmware.device_class }}</option>
                    {%- endif -%}
                    {%- for class in device_classes | sort %}
                        <option value='{{ class }}'>{{ class }}</option>
                    {%- endfor %}
                        <option value='new entry'>new entry</option>
                    </select>
                    <input type="text" name="device_class" class="form-control" id="class_text" placeholder="New Device Class" style="display: none;">
                {%- endcall %}

                {# Vendor #}
                {%- call macros.upload_input("vendor", "Vendor", error) %}
                    <select class="form-control" id="vendor" name='vendor_dropdown' size="4" required
                            onchange="update_text_input(this, document.getElementById('vendor_text')); update_device_names()">
                    {%- if is_reanalysis %}
                        <option value='{{ firmware.vendor }}' selected>{{ firmware.vendor }}</option>
                    {%- endif -%}
                    {% for vendor in vendors | sort %}
                        <option value='{{ vendor }}'>{{ vendor }}</option>
                    {% endfor %}
                        <option value='new entry'>new entry</option>
                    </select>
                    <input type="text" name="vendor" class="form-control" id="vendor_text" placeholder="New Vendor" style="display: none;">
                {%- endcall %}

                {# Device Name #}
                {%- call macros.upload_input("device_name", "Device Name", error) %}
                    <select class="form-control" id="device_name" name='device_name_dropdown' size="4" required
                            {%- if not is_reanalysis %} disabled {% endif -%}
                            onchange="update_text_input(this, document.getElementById('device_name_text'));
                                      if (this.selectedIndex != -1){document.getElementById('device_part').disabled = false;}">
                        {% if not is_reanalysis %}
                            <option value='new entry'>Please select device class and vendor</option>
                        {% else %}
                            <option value='{{ firmware.device_name }}' selected>{{ firmware.device_name }}</option>
                            <option value='new entry'>new entry</option>
                        {% endif %}
                    </select>
                    <input type="text" name="device_name" class="form-control" id="device_name_text" placeholder="New Device Name" style="display: none;">
                {%- endcall %}

                {# Device Part #}
                {%- call macros.upload_input("device_part", "Device Part", error) %}
                    <select class="form-control" id="device_part" name='device_part_dropdown' size="4" required
                            {%- if not is_reanalysis %} disabled {% endif -%}
                            onchange="update_text_input(this, document.getElementById('device_part_text'));">
                        {%- if not is_reanalysis %}
                            <option value='complete' selected>complete</option>
                            <option value='kernel'>kernel</option>
                            <option value='bootloader'>bootloader</option>
                            <option value='root-fs'>root-fs</option>
                        {%- else %}
                            <option value='{{ firmware.part }}' selected>{{ firmware.part }}</option>
                        {%- endif %}
                        <option value='new entry'>new entry</option>
                    </select>
                    <input type="text" name="device_part" class="form-control" id="device_part_text" placeholder="New Device Part" style="display: none;">
                {%- endcall %}

                {# Version #}
                {%- call macros.upload_input("version", "Version", error) %}
                    <input type="text" name="version" class="form-control" id="version" required
                           {%- if is_reanalysis %} value="{{ firmware.version }}" {% else %} placeholder="Version" {% endif -%}>
                {%- endcall %}

                {# Release Date #}
                {%- call macros.upload_input("release_date", "Release Date", error, additional_classes='input-group') %}
                    <div class="input-group-prepend">
                        <span class="input-group-text"><i class="fas fa-calendar-alt"></i></span>
                    </div>
                    <input type="text" name="release_date" class="form-control" id="release_date"
                           {%- if is_reanalysis %} value="{{ firmware.release_date }}"{% else %} placeholder="Release Date" {% endif %}>
                    <script type="text/javascript">
                        $(function () {
                            $('#release_date').datepicker({
                                format: 'yyyy-mm-dd',
                                todayHighlight: true
                            });
                        });
                    </script>
                {%- endcall %}

                {# Tags #}
                <label class="control-label" for="tags">Tags:</label>
                <div class="form-group">
                    <div>
                        <input type="text" name="tags" class="form-control" id="tags"
                               {%- if is_reanalysis %} value="{{ ','.join(firmware.tags.keys()) }}"{% else %} placeholder="tags"{% endif %}>
                        <span class="help-block">Optional: Comma separated list (e.g: flashdump,partial)</span>
                    </div>
                </div>

                {# Analysis Preset #}
                <label class="control-label">Analysis Preset:</label>
                <div class="form-group">
                    {%- if not is_reanalysis %}
                        <script>
                            var plugin_dict = {{ analysis_plugin_dict | dict_to_json | safe }};

                            function change_selected_plugins(selected_theme) {
                                for (var plugin in plugin_dict) {
                                    if (plugin_dict.hasOwnProperty(plugin)) {
                                        plugin_checkbox = document.getElementById(plugin);
                                        if (plugin_checkbox != null) {
                                            if (plugin_dict[plugin][2][selected_theme] == true) {
                                                plugin_checkbox.firstElementChild.firstElementChild.checked = true;
                                            } else {
                                                plugin_checkbox.firstElementChild.firstElementChild.checked = false;
                                            }
                                        }
                                    }
                                }
                            }
                        </script>
                        <div class="col-lg-12 px-0">
                            <select class="form-control" name='analysis_dropdown' id="analysis" onchange="change_selected_plugins(this.value);">
                                {% for preset in analysis_presets %}
                                    <option value="{{ preset }}">{{ preset }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    {%- endif %}
                    <div class="col-lg-12 mt-4 px-0" style="column-count: 2;column-rule: 1px hidden black; column-gap:50px">
                        {% for system in analysis_plugin_dict.keys() | sort %}
                            {% if not analysis_plugin_dict[system][1] %}
                                {# show if plug-in is not mandatory #}
                                <div id="{{ system }}">
                                    <label class="checkbox-inline" data-toggle="tooltip" title="{{ analysis_plugin_dict[system][0] | safe }}">
                                        {% if analysis_plugin_dict[system][2]['default'] %}
                                            <input type=checkbox name="analysis_systems" value="{{ system }}" checked>
                                        {% endif %}
                                        {% if not analysis_plugin_dict[system][2]['default'] %}
                                            <input type=checkbox name="analysis_systems" value="{{ system }}" unchecked>
                                        {% endif %}
                                        <font face="courier">{{ system | replace_underscore }}</font>
                                        <br>
                                    </label>
                                </div>
                            {%- endif %}
                        {%- endfor %}
                    </div>
                </div>

                <button type="submit" value=submit class="btn btn-primary" id="input_submit" onclick='showImg()'>
                    {%- if is_reanalysis %}
                        <i class="fas fa-redo-alt"></i> {{ title }}
                    {%- else %}
                        <i class="fas fa-upload"></i> Submit
                    {%- endif %}
                </button>
            </form>
        </div>
    </div>
</div>

{%- endblock -%}
