{% extends "base.html" %}
{% set active_page = "shares_calc" %}

{% block title %}Spoil Shares Calculator{% endblock %}
{% block content %}
<div class="uk-flex uk-flex-center uk-margin-bottom uk-margin-top">
    <h1 class="uk-text-center"><strong>Legends of Palisma</strong>Spoil Shares Calculator</h1>
</div>

<div class="uk-grid-divider" uk-grid>
    <div class="uk-width-1-3">
        <h3>Session Info</h3>
        <form class="uk-form-horizontal uk-margin-large" autocomplete=off>
            <div class="uk-margin">
                <label class="uk-form-label" for="player_count">Number of players</label>
                <div class="uk-form-controls">
                    <select name="player_count" id="player_count" class="uk-select">
                        <option value="1">1</option>
                        <option value="2">2</option>
                        <option value="3">3</option>
                        <option value="4">4</option>
                        <option value="5">5</option>
                        <option value="6">6</option>
                        <option value="7">7</option>
                        <option value="8">8</option>
                    </select>
                </div>
            </div>

            <div class="uk-margin">
                <label class="uk-form-label" for="hench_count">Number of henchmen</label>
                <div class="uk-form-controls">
                    <input class="uk-input" id="hench_count" name="hench_count" type="number" value="0">
                </div>
            </div>

            <div class="uk-margin">
                <label class="uk-form-label" for="gp_recovered">GP value recovered</label>
                <div class="uk-form-controls">
                    <input class="uk-input" id="gp_recovered" name="gp_recovered" type="number" value="0">
                </div>
            </div>

            <div class="uk-margin">
                <label class="uk-form-label" for="xp_earned">XP earned from monsters</label>
                <div class="uk-form-controls">
                    <input class="uk-input" id="xp_earned" name="xp_earned" type="number" value="0">
                </div>
            </div>
            <button type="button" id="share_calc_button" class="uk-button uk-button-primary">Calculate</button>
        </form>
    </div>
    <div class="uk-width-1-3">
        <h4>Henchmen XP Shares</h4>
        <table id="hench_shares" class="uk-table">
            <thead>
                <tr>
                    <th>Bonus XP Rate</th>
                    <th>Share Per-Henchman</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>
    </div>
    <div class="uk-width-1-3">
        <h4>Player XP Shares</h4>
        <table id="player_shares" class="uk-table">
            <thead>
                <tr>
                    <th>Bonus XP Rate</th>
                    <th>Share Per-Player</th>
                </tr>
            </thead>
            <tbody></tbody>
        </table>

        <h4>Player GP Share</h4>
        <span id="pc_gp_share"></span>

        <div id="share_button_container" class="uk-margin-large-top uk-hidden">
            <button type="button" class="uk-button uk-button-secondary uk-button-small" id="export_button" title="Will copy a chat message to be pasted into Roll20.">Share</button>
            <button type="button" class="uk-button uk-button-default uk-button-small" id="clear_button">Reset</button>
        </div>
    </div>
</div>
<textarea id="export-data" class="uk-textarea uk-hidden"></textarea>
{% endblock %}

{% block head %}
<style>
</style>
{% endblock %}

{% block scripts %}
<script type="text/javascript">
    var lastCalculatedShares = null;

    window.addEventListener('DOMContentLoaded', () => {
        var calc = document.querySelector('button#share_calc_button');
        calc.addEventListener("click", calculateShares);

        var share = document.querySelector('button#export_button');
        share.addEventListener("click", clipboardShare);

        var clear = document.querySelector('button#clear_button');
        clear.addEventListener("click", resetPage);
    });

    function calculateShares() {
        document.querySelector('div#share_button_container').classList.remove('uk-hidden');

        var player_shares = document.querySelector('table#player_shares > tbody');
        var hench_shares = document.querySelector('table#hench_shares > tbody');
        var pc_gp_share = document.querySelector('span#pc_gp_share');

        player_shares.innerHTML = '';
        hench_shares.innerHTML = '';
        pc_gp_share.innerText = '';

        var player_count = parseInt(document.querySelector('select#player_count').value);
        var hench_count = parseInt(document.querySelector('input#hench_count').value);
        
        var gp = parseInt(document.querySelector('input#gp_recovered').value);
        var xp = parseInt(document.querySelector('input#xp_earned').value);

        lastCalculatedShares = {pc: player_count, hc: hench_count, gp: gp, xp: xp};

        let total_xp = gp + xp;
        let pc_base_xp_share = total_xp / (player_count + hench_count / 2.0);

        for(let i = 0; i < 5; i ++) {
            // Build player rows
            const row = document.createElement("tr");
            const c1 = document.createElement("td");
            const c2 = document.createElement("td");

            c1.appendChild(document.createTextNode(`@ ${5 * i}%`));
            c2.appendChild(document.createTextNode(`${Math.floor(pc_base_xp_share * (1 + 0.05 * i))} xp`));

            row.appendChild(c1);
            row.appendChild(c2);

            player_shares.appendChild(row);

            // Build hench rows
            const hrow = document.createElement("tr");
            const hc1 = document.createElement("td");
            const hc2 = document.createElement("td");

            hc1.appendChild(document.createTextNode(`@ ${5 * i}%`));
            hc2.appendChild(document.createTextNode(`${Math.floor(pc_base_xp_share * (1 + 0.05 * i) / 2.0)} xp`));

            hrow.appendChild(hc1);
            hrow.appendChild(hc2);

            hench_shares.appendChild(hrow);
        }

        var pc_gp_share = document.querySelector('span#pc_gp_share');
        let gpshare = Math.floor(gp / (player_count + hench_count / 6.0))
        pc_gp_share.appendChild(document.createTextNode(`${gpshare} gold per player.`));
    }

    function clipboardShare() {
        if(!lastCalculatedShares) {
            return;
        }

        var btn = document.querySelector('button#export_button');
        btn.classList.remove('uk-animation-shake');
        
        var lcs = lastCalculatedShares;
        var [player_count, hench_count, gp, xp] = [lcs.pc, lcs.hc, lcs.gp, lcs.xp];

        let total_xp = gp + xp;
        let pc_base_xp_share = total_xp / (player_count + hench_count / 2.0);

        var [ps, hs] = [{}, {}];
        for(let i = 0; i < 5; i ++) {
            // Build player rows
            ps[`@ ${5 * i}%`] = `${Math.floor(pc_base_xp_share * (1 + 0.05 * i))} xp`;

            // Build hench rows
            hs[`@ ${5 * i}%`] = `${Math.floor(pc_base_xp_share * (1 + 0.05 * i) / 2.0)} xp`;
        }

        let gpshare = Math.floor(gp / (player_count + hench_count / 6.0))

        {% raw  %}
        let response = '&{template:acks}{{name=Spoil Shares}}';
        response += `{{**Player Gold Share** = [[${gpshare}]]}}`;
        response += '{{**Player XP Share**=\n'
        for(const [k, v] of Object.entries(ps)) {
            response += `${k} bonus: *${v}*\n`;
        }
        response += '}}';

        response += '{{**Henchmen XP Share**=\n'
        for(const [k, v] of Object.entries(hs)) {
            response += `${k} bonus: *${v}*\n`;
        }
        response += `}}{{desc=${player_count} players, ${hench_count} henchmen sharing ${gp} gold XP and ${xp} monster XP.}}`;
        {% endraw %}

        let export_data = document.querySelector('#export-data');
        export_data.value = response;
        export_data.classList.remove('uk-hidden');

        export_data.select();
        document.execCommand("copy");

        export_data.classList.add('uk-hidden');

        btn.classList.add('uk-animation-shake');
    }

    function resetPage() {
        document.querySelector('div#share_button_container').classList.add('uk-hidden');
        document.querySelector('table#player_shares > tbody').innerHTML = '';
        document.querySelector('table#hench_shares > tbody').innerHTML = '';
        document.querySelector('span#pc_gp_share').innerText = '';

        document.querySelector('select#player_count').value = 1;
        document.querySelector('input#hench_count').value = 0;

        document.querySelector('input#gp_recovered').value = 0;
        document.querySelector('input#xp_earned').value = 0;
    }
</script>
{% endblock %}
