<!doctype html>
<html lang="en">
<head>
    <title>Volmit Software - Damage calculator</title>

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet"
          href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-beta3/css/bootstrap.min.css"
          integrity="sha512-N415hCJJdJx+1UBfULt+i+ihvOn42V/kOjOpp1UTh4CZ70Hx5bDlKryWaqEKfY/8EYOu/C2MuyaluJryK1Lb5Q=="
          crossorigin="anonymous"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"
          integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w=="
          crossorigin="anonymous"/>
    <link rel="stylesheet" href="dist/css/beautify.css">
</head>
<body>
<div class="container">
    <div class="jumbotron mb-4">
        <h1 class="display-4">Volmit Software - Damage calculator</h1>
        <p class="lead">Simply calculate the damage values for your texture pack.</p>
        <hr class="my-4">
        <p>You indicate which item you want to use and where the models are for each damage value. This tool then
            creates the .json file for you.</p>
    </div>
    <form id="calculatorForm">
        <div class="mb-3 w-50">
            <label for="item">The item to use:</label>
            <select class="form-control" id="item" name="item" required>
                <option value="" selected>Select an item...</option>
                <option value="leather_boots">Leather Boots</option>
            </select>
        </div>
        <div id="formItems" style="display: none;">
            <div class="mb-3 w-50">
                <label for="itemcount">The amount of models:</label>
                <div class="input-group">
                    <button type="button" class="btn btn-outline-secondary btn-number" disabled
                            data-type="minus" data-field="quant[1]">
                        <span class="fas fa-minus"></span>
                    </button>
                    <input type="text" id="itemcount" name="quant[1]" class="form-control input-number" value="1" min="1"
                           max="2" readonly>
                    <button type="button" class="btn btn-outline-secondary btn-number" data-type="plus"
                            data-field="quant[1]">
                        <span class="fas fa-plus"></span>
                    </button>
                </div>
            </div>
            <div id="items">
                <div class="mb-3 w-50">
                    <label for="item1">Model for damage 1:</label>
                    <input type="text" class="form-control" id="item1" name="item1" placeholder="cars/blue_car" required>
                </div>
            </div>
            <button type="submit" class="btn btn-primary">Calculate</button>
        </div>
    </form>
    <hr class="my-4">
    <h6>Output JSON:</h6>
    <pre id="content" class="mb-3 w-50">Fill in the form above to get an output.</pre>
    <button id="copyButton" class="btn btn-primary mb-3 mr-3">Copy</button>
    <a id="downloadButton" class="btn btn-secondary mb-3" href="#">Download</a>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
        integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
        crossorigin="anonymous"></script>
<script type="application/javascript" src="dist/js/calculator.js"></script>
<script type="application/javascript" src="dist/js/beautify.js"></script>
<script>
    $(document).ready(function () {
        const form = $("#calculatorForm");
        const formOptions = $("#formItems");

        const item = $("#item");
        const itemCount = $("#itemcount");
        const itemCountButton = $('.btn-number');
        const items = $("#items");

        const output = $("#content");

        const copyButton = $("#copyButton");
        const downloadButton = $("#downloadButton");

        itemCountButton.click(function (e) {
            e.preventDefault();

            let fieldName = $(this).attr('data-field');
            let type = $(this).attr('data-type');
            const input = $("input[name='" + fieldName + "']");
            const currentVal = parseInt(input.val());

            if (!isNaN(currentVal)) {
                if (type === 'minus') {
                    if (currentVal > input.attr('min')) {
                        input.val(currentVal - 1).change();
                    }

                    if (input.val() == input.attr('min')) {
                        $(this).attr('disabled', true);
                        return;
                    }

                    $("#iteminput" + currentVal).remove();
                } else if (type === 'plus') {
                    if (currentVal < input.attr('max')) {
                        input.val(currentVal + 1).change();
                    }

                    if (input.val() == input.attr('max')) {
                        $(this).attr('disabled', true);
                        return;
                    }

                    items.append("<div id=\"iteminput" + (currentVal + 1) + "\" class=\"mb-3 w-50\"><label for=\"item" + (currentVal + 1) + "\">Model for damage " + (currentVal + 1) + ":</label><input type=\"text\" class=\"form-control\" id=\"item" + (currentVal + 1) + "\" name=\"item" + (currentVal + 1) + "\" placeholder=\"cars/" + randomModel() + "\" required></div>");
                }
            } else {
                input.val(0);
            }
        });

        item.change(function () {
            if (this.value !== "") { //If value is not empty, show other options ...
                formOptions.show();

                //... and set max value of item count.
                const max = itemDamages[this.value];
                itemCount.prop('max', max);
            } else { //Or if empty, hide other options.
                formOptions.hide();
            }
        });

        form.on("submit", function(e){
            e.preventDefault();

            //Get selected item
            const selectedItem = item.val();

            //Get amount of items
            const fieldName = $('.btn-number').attr('data-field');
            const input = $("input[name='" + fieldName + "']");
            const currentVal = parseInt(input.val());

            //Build models array
            const models = [];
            for (let i = 0; i < currentVal; i++) {
                models[i] = $("#item" + (i + 1)).val();
            }

            //Convert to JSON, and set in content field (with syntax highlighting)
            const json = toJSON(selectedItem, models);
            output.html(syntaxHighlight(json));

            //And fix the Download knop
            const dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(json);
            downloadButton.attr("href", dataStr);
            downloadButton.attr("download", selectedItem + ".json");

            //Return false to not reload the page
            return false;
        });

        copyButton.click(function (e) {
            e.preventDefault();

            //Get the content.
            const copyText = output.text();

            //Create temporary textarea, select, copy, and remove it again.
            const textArea = document.createElement("textarea");
            textArea.textContent = copyText;
            document.body.append(textArea);
            textArea.select();
            document.execCommand("copy");
            textArea.remove();
        });

        /**
         * Get a random model.
         *
         * @return {string} The random model.
         */
        function randomModel() {
            const months = ["red_car", "blue_car", "green_car", "orange_car", "blue_bicycle", "red_bicycle", "green_bicycle", "orange_bicycle"];
            return months[Math.floor(Math.random() * months.length)];
        }
    });
</script>
</html>