<!DOCTYPE html>
<!-- Copyright (C) 2019 igoz <igoz@igoz.net>
     See end of file for extended copyright and license notice. -->
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="robots" content="index, follow">
    <title>wexedit</title>
    <meta name="description" content="Wex Dust configuration editor">

    <style>
    strong {
        border-bottom: 1px solid #444444;
    }
    </style>
</head>
<body>
    <label>Import configuration: <input type="file" id="import" accept=".txt"></label>
    <br>
    <input type="checkbox" id="invert"> <label>Invert (selections are unblocked, everything else is blocked)</label>
    <br><br>

    <strong><input type="checkbox" name="superoption" value="40"> Cemetery of Ash</strong><br>
    <input type="checkbox" name="option" value="400001"> <label>Untended Graves</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="300"> High Wall of Lothric</strong><br>
    <input type="checkbox" name="option" value="300001"> <label>Top High Wall</label><br>
    <input type="checkbox" name="option" value="300002"> <label>Darkwraith Chamber</label><br>
    <input type="checkbox" name="option" value="300003"> <label>Middle High Wall</label><br>
    <input type="checkbox" name="option" value="300004"> <label>Lower High Wall</label><br>
    <input type="checkbox" name="option" value="300020"> <label>Consumed King's Garden</label><br>
    <input type="checkbox" name="option" value="300021"> <label>Consumed King's Garden Main Area</label><br>
    <input type="checkbox" name="option" value="300022"> <label>Pre-Oceiros</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="31"> Undead Settlement</strong><br>
    <input type="checkbox" name="option" value="310000"> <label>Foot of the High Wall</label><br>
    <input type="checkbox" name="option" value="310001"> <label>Dilapidated Bridge</label><br>
    <input type="checkbox" name="option" value="310002"> <label>Cliff Underside</label><br>
    <input type="checkbox" name="option" value="310003"> <label>Irina</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="33"> Road of Sacrifices</strong><br>
    <input type="checkbox" name="option" value="330001"> <label>Halfway Fortress</label><br>
    <input type="checkbox" name="option" value="330002"> <label>Black Knight</label><br>
    <input type="checkbox" name="option" value="330011"> <label>Keep Ruins</label><br>
    <input type="checkbox" name="option" value="330012"> <label>Swamp</label><br>
    <input type="checkbox" name="option" value="330013"> <label>Pre-Abyss Watchers</label><br>
    <input type="checkbox" name="option" value="330014"> <label>Farron Keep Perimeter</label><br>
    <input type="checkbox" name="option" value="330015"> <label>Old Wolf of Farron</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="35"> Cathedral of the Deep</strong><br>
    <input type="checkbox" name="option" value="350001"> <label>Cleansing Chapel</label><br>
    <input type="checkbox" name="option" value="350002"> <label>Below Chapel</label><br>
    <input type="checkbox" name="option" value="350003"> <label>Outside Cathedral Door</label><br>
    <input type="checkbox" name="option" value="350004"> <label>Cathedral of the Deep</label><br>
    <input type="checkbox" name="option" value="350005"> <label>Pre-Deacons</label><br>
    <input type="checkbox" name="option" value="350011"> <label>Pre-Rosaria</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="38"> Catacombs of Carthus</strong><br>
    <input type="checkbox" name="option" value="380001"> <label>Post-Abyss Watchers</label><br>
    <input type="checkbox" name="option" value="380002"> <label>Catacombs of Carthus</label><br>
    <input type="checkbox" name="option" value="380012"> <label>Abandoned Tomb</label><br>
    <input type="checkbox" name="option" value="380013"> <label>Horace's Cave</label><br>
    <input type="checkbox" name="option" value="380014"> <label>Giant Avelyn</label><br>
    <input type="checkbox" name="option" value="380015"> <label>Old King's Antechamber</label><br>
    <input type="checkbox" name="option" value="380016"> <label>Unknown</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="37"> Irithyll of the Boreal Valley</strong><br>
    <input type="checkbox" name="option" value="370001"> <label>Central Irithyll</label><br>
    <input type="checkbox" name="option" value="370002"> <label>Church of Yorshka</label><br>
    <input type="checkbox" name="option" value="370003"> <label>Distant Manor</label><br>
    <input type="checkbox" name="option" value="370004"> <label>Siegward's Fireplace</label><br>
    <input type="checkbox" name="option" value="370005"> <label>Pre-Pontiff</label><br>
    <input type="checkbox" name="option" value="370010"> <label>Pontiff Hotspot</label><br>
    <input type="checkbox" name="option" value="370011"> <label>Darkmoon Tomb</label><br>
    <input type="checkbox" name="option" value="370012"> <label>Anor Londo</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="39"> Irithyll Dungeon</strong><br>
    <input type="checkbox" name="option" value="390000"> <label>Irithyll Dungeon</label><br>
    <input type="checkbox" name="option" value="390001"> <label>Sleeping Giant</label><br>
    <input type="checkbox" name="option" value="390002"> <label>Lower Dungeon</label><br>
    <input type="checkbox" name="option" value="390003"> <label>Profaned Capital</label><br>
    <input type="checkbox" name="option" value="390004"> <label>Pre-Yhorm</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="LC"> Lothric Castle</strong><br>
    <input type="checkbox" name="option" value="301000"> <label>Lothric Castle</label><br>
    <input type="checkbox" name="option" value="301001"> <label>Dragon Barracks</label><br>
    <input type="checkbox" name="option" value="301002"> <label>Lower Barracks</label><br>
    <input type="checkbox" name="option" value="301003"> <label>Altar of Sunlight</label><br>
    <input type="checkbox" name="option" value="341000"> <label>Grand Archives</label><br>
    <input type="checkbox" name="option" value="341001"> <label>Grand Archives Rooftops</label><br>
    <input type="checkbox" name="option" value="341002"> <label>Post-Trio</label><br>
    <input type="checkbox" name="option" value="341003"> <label>Grand Archives Rooftop</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="32"> Archdragon Peak</strong><br>
    <input type="checkbox" name="option" value="320001"> <label>Archdragon Peak</label><br>
    <input type="checkbox" name="option" value="320010"> <label>Dragon-kin Mausoleum</label><br>
    <input type="checkbox" name="option" value="320012"> <label>Second Wyvern</label><br>
    <input type="checkbox" name="option" value="320013"> <label>Great Belfry</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="41"> Kiln of the First Flame</strong><br>
    <input type="checkbox" name="option" value="410001"> <label>Kiln of the First Flame</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="45"> The Painted World of Ariandel</strong><br>
    <input type="checkbox" name="option" value="450001"> <label>Snowfield</label><br>
    <input type="checkbox" name="option" value="450002"> <label>Corvian Settlement</label><br>
    <input type="checkbox" name="option" value="450003"> <label>Snowy Mountain Pass</label><br>
    <input type="checkbox" name="option" value="450004"> <label>Unknown</label><br>
    <input type="checkbox" name="option" value="450005"> <label>Ariandel Chapel</label><br>
    <input type="checkbox" name="option" value="450006"> <label>Unknown</label><br>
    <input type="checkbox" name="option" value="450011"> <label>Depths of the Painting</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="50"> The Dreg Heap</strong><br>
    <input type="checkbox" name="option" value="500001"> <label>The Dreg Heap</label><br>
    <input type="checkbox" name="option" value="500002"> <label>Earthen Peak Ruins</label><br>
    <input type="checkbox" name="option" value="500003"> <label>Within the Earthen Peak Ruins</label><br>
    <br>
    <strong><input type="checkbox" name="superoption" value="51"> The Ringed City</strong><br>
    <input type="checkbox" name="option" value="510001"> <label>Mausoleum Lookout</label><br>
    <input type="checkbox" name="option" value="510002"> <label>Ringed Inner Wall</label><br>
    <input type="checkbox" name="option" value="510003"> <label>Ringed City Streets</label><br>
    <input type="checkbox" name="option" value="510004"> <label>Shared Grave</label><br>
    <input type="checkbox" name="option" value="510005"> <label>After Bridge</label><br>
    <input type="checkbox" name="option" value="510011"> <label>Pre-Midir</label><br>
    <br>

    <input type="checkbox" id="ask_confirmation"> <label>Ask Confirmation</label><br>
    <br>

    <button onclick="make_config()">Download</button>
    <br>

    <p>© 2019 igoz.</p>

    <script>
    const superoptions = document.querySelectorAll('input[name=superoption]');
    const matching = (sval, val) => sval === 'LC' 
        ? val.slice(0, 3) === '301' || val.slice(0, 3) === '341'
        : val.slice(0, sval.length) === sval;
    const options = document.querySelectorAll('input[name=option]');
    superoptions.forEach(sopt => {
        sopt.addEventListener('change', () => {
            options.forEach(opt => {
                if (matching(sopt.value, opt.value)) {
                    opt.checked = sopt.checked;
                }
            });
        });
    });
    options.forEach(opt => {
        opt.addEventListener('change', () => {
            const sopt = Array.from(superoptions).find(
                o => matching(opt.value, o.value));
            if (!sopt) return;
            const opts = Array.from(options).filter(
                o => matching(opt.value, o.value));

            const all_checked = opts.every(cb => cb.checked);
            const none_checked = opts.every(cb => !cb.checked);

            sopt.checked = all_checked;
            sopt.indeterminate = !all_checked && !none_checked;
        });
    });

    document.getElementById('import').addEventListener('change', 
       function (event) {
        const file = event.target.files[0];
        if (!file) return;

        const reader = new FileReader();
        reader.onload = function (e) {
            try {
                const json = JSON.parse(e.target.result);
                if (!Array.isArray(json.area_blocklist)) {
                    alert("Invalid file: 'area_blocklist' must be an array");
                    return;
                }

                const selected = new Set(json.area_blocklist);
                options.forEach(opt => {
                    opt.checked = selected.has(Number(opt.value));
                    opt.dispatchEvent(new Event('change'));
                });

                if (typeof json.ask_confirmation === 'boolean') {
                    document.getElementById('ask_confirmation').checked = 
                        json.ask_confirmation;
                } else {
                    alert("Invalid file: 'ask_confirmation' must be a boolean");
                    return;
                }
            } catch (err) {
                alert("Error reading file: " + err.message);
            }
        };
        reader.readAsText(file);
    });

    function make_config() {
        const inverted = document.getElementById('invert').checked;
        const checkboxes = inverted 
            ? document.querySelectorAll('input[name="option"]:not(:checked)')
            : document.querySelectorAll('input[name="option"]:checked')
        const values = Array.from(checkboxes).map(cb => `${cb.value}`);

        const config_str = 
            `{\r\n  "area_blocklist": [${values.join(', ')}],\r\n  ` +
            `"ask_confirmation": ` +
            `${document.getElementById('ask_confirmation').checked}\r\n}`;

        const blob = new Blob([config_str], { type: "application/json" });
        const url = URL.createObjectURL(blob);

        const a = document.createElement('a');
        a.href = url;
        a.download = 'wex_dust_config.txt';
        a.click();

        URL.revokeObjectURL(url);
    }
    </script>
</body>
</html>

<!--
Copyright (C) 2019 igoz <igoz@igoz.net>

This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>. 
-->