<div id="package-add-modal" tabindex="-1" role="dialog" class="modal fade">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <form id="package-add-form" onsubmit="return false">
                <div class="modal-header">
                    <h4 class="modal-title">Add new packages</h4>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="close"></button>
                </div>
                <div class="modal-body">
                    <div class="form-group row">
                        <label for="package-add-repository-input" class="col-3 col-form-label">repository</label>
                        <div class="col-9">
                            <select id="package-add-repository-input" class="form-control" required>
                                {% for repository in repositories %}
                                    <option value="{{ repository.id }}" data-repository="{{ repository.repository }}" data-architecture="{{ repository.architecture }}">{{ repository.repository }} ({{ repository.architecture }})</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="package-add-input" class="col-3 col-form-label">package</label>
                        <div class="col-9">
                            <input id="package-add-input" type="text" list="package-add-known-packages-dlist" autocomplete="off" class="form-control" placeholder="AUR package" required>
                            <datalist id="package-add-known-packages-dlist"></datalist>
                        </div>
                    </div>
                    <div class="form-group row">
                        <div class="col-12">
                            <button id="package-add-variable-button" type="button" class="form-control btn btn-light rounded" onclick="packageAddVariableInputCreate()"><i class="bi bi-plus"></i> add environment variable </button>
                        </div>
                    </div>
                    <div id="package-add-variables-div" class="form-group row"></div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary" onclick="packagesAdd()"><i class="bi bi-play"></i> add</button>
                    <button type="submit" class="btn btn-success" onclick="packagesRequest()"><i class="bi bi-plus"></i> request</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    const packageAddModal = $("#package-add-modal");
    const packageAddForm = $("#package-add-form");

    const packageAddInput = $("#package-add-input");
    const packageAddRepositoryInput = $("#package-add-repository-input");
    const packageAddKnownPackagesList = $("#package-add-known-packages-dlist");

    const packageAddVariablesDiv = $("#package-add-variables-div");

    function packageAddVariableInputCreate() {
        const variableInput = document.createElement("div");
        variableInput.classList.add("input-group");
        variableInput.classList.add("package-add-variable");

        const variableNameInput = document.createElement("input");
        variableNameInput.type = "text";
        variableNameInput.classList.add("form-control");
        variableNameInput.classList.add("package-add-variable-name");
        variableNameInput.placeholder = "name";
        variableNameInput.ariaLabel = "variable name";

        const variableSeparator = document.createElement("span");
        variableSeparator.classList.add("input-group-text")
        variableSeparator.textContent = "=";

        const variableValueInput = document.createElement("input");
        variableValueInput.type = "text";
        variableValueInput.classList.add("form-control");
        variableValueInput.classList.add("package-add-variable-value");
        variableValueInput.placeholder = "value";
        variableValueInput.ariaLabel = "variable value";

        const variableButtonRemove = document.createElement("button");
        variableButtonRemove.type = "button";
        variableButtonRemove.classList.add("btn");
        variableButtonRemove.classList.add("btn-outline-danger");
        variableButtonRemove.innerHTML = "<i class=\"bi bi-trash\"></i>";
        variableButtonRemove.onclick = _ => { return variableInput.remove(); };

        // bring them together
        variableInput.appendChild(variableNameInput);
        variableInput.appendChild(variableSeparator);
        variableInput.appendChild(variableValueInput);
        variableInput.appendChild(variableButtonRemove);

        packageAddVariablesDiv.append(variableInput);
    }

    function patchesParse() {
        const patches = packageAddVariablesDiv.find(".package-add-variable").map((_, element) => {
            const richElement = $(element);
            return {
                key: richElement.find(".package-add-variable-name").val(),
                value: richElement.find(".package-add-variable-value").val(),
            };
        }).filter((_, patch) => patch.key).get();
        return {patches: patches};
    }

    function packagesAdd(packages, patches, repository) {
        packages = packages ?? packageAddInput.val();
        patches = patches ?? patchesParse();
        repository = repository ?? getRepositorySelector(packageAddRepositoryInput);

        if (packages) {
            packageAddModal.modal("hide");
            const onSuccess = update => `Packages ${update} have been added`;
            const onFailure = error => `Package addition failed: ${error}`;
            doPackageAction("/api/v1/service/add", [packages], repository, onSuccess, onFailure, patches);
        }
    }

    function packagesRequest(packages, patches) {
        packages = packages ?? packageAddInput.val();
        patches = patches ?? patchesParse();
        const repository = getRepositorySelector(packageAddRepositoryInput);

        if (packages) {
            packageAddModal.modal("hide");
            const onSuccess = update => `Packages ${update} have been requested`;
            const onFailure = error => `Package request failed: ${error}`;
            doPackageAction("/api/v1/service/request", [packages], repository, onSuccess, onFailure, patches);
        }
    }

    $(_ => {
        packageAddModal.on("shown.bs.modal", _ => {
            $(`#package-add-repository-input option[value="${repository.architecture}-${repository.repository}"]`).prop("selected", true);
        });
        packageAddModal.on("hidden.bs.modal", _ => {
            packageAddVariablesDiv.empty();
            packageAddForm.trigger("reset");
        });

        packageAddInput.keyup(_ => {
            clearTimeout(packageAddInput.data("timeout"));
            packageAddInput.data("timeout", setTimeout($.proxy(_ => {
                const value = packageAddInput.val();

                if (value.length >= 3) {
                    $.ajax({
                        url: "/api/v1/service/search",
                        data: {"for": value},
                        type: "GET",
                        dataType: "json",
                        success: response => {
                            const options = response.map(pkg => {
                                const option = document.createElement("option");
                                option.value = pkg.package;
                                option.innerText = `${pkg.package} (${pkg.description})`;
                                return option;
                            });
                            packageAddKnownPackagesList.empty().append(options);
                        },
                    });
                }
            }, this), 500));
        });
    });
</script>
