<?php

namespace Bdp\Modules\LimitLoginAttempts\Controllers;


class OptionsPage
{
    public function __construct()
    {
        add_options_page(BDP_LV_PLUGIN_SLUG . '-limit-login-attempts',
            'Protect Login',
            'site-health.php',
            BDP_LV_PLUGIN_SLUG . '-limit-login-attempts',
            [$this, 'limit_login_option_page'],2048);
    }

    public function releaseIp($ip)
    {
        $allIps = get_option('kompass_limit_login_lockouts', []);
        unset($allIps[$ip]);
        update_option('kompass_limit_login_lockouts', $allIps);
    }

    public function getBlockedIps()
    {
        $ips = '';
        foreach (get_option('kompass_limit_login_lockouts', []) as $ip => $blockedUntil) {
            $ips .= '<tr>' .
                        '<td style="padding-right: 10px;">' . $ip . '</td>' .
                        '<td style="padding-right: 10px;">' . date('d.m.Y H:i', $blockedUntil) . ' Uhr</td>' .
                        '<td>
                            <a href="admin.php?page=bdp-kompass-limit-login-attempts&tab=tab4&action=release&ip=' .
                                        base64_encode($ip) . '">Freigeben</a></td>' .
                    '</tr>';
        };

        return $ips;
    }

    public function limit_login_option_page() {
        global $errors;

        $showMessage = null;

        if (isset($_POST['update_options'])) {
            update_settings($_POST);
            $showMessage = 'Die Einstellungen wurden gespeichert';
        }
        if (isset($_GET['action']) && $_GET['action'] == 'release') {
            $showMessage = 'Die IP-Adresse wurde freigegeben.';
        }

        if(isset($_POST['save_kompass_balist_list_type'])) {
            $showMessage = 'Die Liste wurde gespeichert.';
        }

        if (null !== $showMessage && $errors === false) {
            echo '<div class="notice notice-success" style="padding: 5px 10px;">';
            echo $showMessage;
            echo '</div>';
        }

        if ($errors) {
            echo '<div class="notice notice-error" style="padding: 5px 10px;">';
            echo 'Beim Durchführen der Aktion ist ein Fehler aufgetreten.';
            echo '</div>';
        }

        $tab = isset($_GET['tab']) ? $_GET['tab'] : 'tab1';
        ?>

        <div class="wrap">
            <h1 class="wp-heading-inline">Protect Login - Einstellungen</h1>
            <hr class="wp-header-end">
            <?= kompass_print_tab_header($tab); ?>

            <div class="tab-content">
                <?php
                switch ($tab) {
                    case 'tab1':
                        echo '<form action="admin.php?page=bdp-kompass-limit-login-attempts&tab=tab1" method="post">';
                        do_settings_sections(BDP_LV_PLUGIN_SLUG . '-limit-login-attempts');
                        submit_button();
                        echo '</form>';
                        break;
                    case 'tab2':
                        echo '<h2>Blocklist</h2>';
                        echo '<form action="admin.php?page=bdp-kompass-limit-login-attempts&tab=tab2" method="post">';
                        kompass_print_block_allow_form('blocklist');
                        submit_button();
                        echo '</form>';
                        break;
                    case 'tab3':
                        echo '<h2>Allowlist</h2>';
                        echo '<form action="admin.php?page=bdp-kompass-limit-login-attempts&tab=tab3" method="post">';
                        kompass_print_block_allow_form('allowlist');
                        submit_button();
                        echo '</form>';
                        break;
                    case 'tab4':
                        if (isset($_GET['action']) && $_GET['action'] == 'release') {
                            $this->releaseIp(base64_decode($_GET['ip']));
                        }
                        $blockedIps = $this->getBlockedIps();
                        ?>
                        <h3>Gesperrte IPs</h3>
                        <?php
                            if (strlen($blockedIps) == 0) {
                                echo '<div class="protect-login-no-blocked-ips">';
                                echo 'Derzeit sind keine Adressen gesperrt.';
                                echo '</div>';
                            } else { ?>
                                <table>
                                    <tr>
                                        <th>IP</th>
                                        <th>Gesperrt bis</th>
                                        <th>Aktion</th>
                                    </tr>
                                    <?= $blockedIps ?>
                                </table>
                            <?php
                            }
                    break;
                }
                ?>
            </div>
        </div>
        <?php
    }
}