<?php

namespace CleantalkSP\SpbctWP\Scanner\ScanningLog;

use CleantalkSP\Common\TextPlate;
use CleantalkSP\Variables\Server;

/**
 * Template class for scan logs.
 */
class Template
{
    use TextPlate;

    /**
     * Render the scanning log
     *
     * @param array $data
     * @param int $limit
     * @return string
     */
    public static function render(array $data, int $limit): string
    {
        $_text = '
            <div id="spbcscan-results-log-module">
                <div id="spbcscan_results_log_body" class="panel-body" data-offset="0" data-stop-load="{{stop_load}}">
                    {{log_rows}}
                </div>
            </div>
            {{log_actions}}
';
        $_plate = [
            'log_rows' => self::generateRows($data),
            'log_actions' => self::renderLogActionsHTML(),
            'stop_load' => Repository::getTotalCount() <= $limit ? '1' : '0',
        ];

        return self::textPlateRender($_text, $_plate);
    }

    /**
     * @return string
     */
    public static function renderLogActionsHTML(): string
    {
        global $spbc;
        $_text = '
            <div id="spbc_log_actions_wrapper">
                <span id="spbcscan_results_log_preloader">
                    <img src="{{preloader_src}}" style="width: 15px; margin-right: 5px"/>{{loading_logs_text}}
                </span>
                <div class="spbc_log_actions_links {{log_actions_hidden_class}}">
                    <div style="padding: 0 5px">
                        <p id="spbc_scanner_copy_log_to_clipboard" class="spbc_hint spbc_hint--link spbc_hint--top_right">
                            {{copy_log_text}}
                        <span id="spbc_scanner_copy_log_to_clipboard_hint"></span>
                        </p>
                        <img class="spbc_preloader" src="{{preloader_src}}" />
                    </div>
                    <div style="padding: 0 5px">
                        <p id="spbc_scanner_clear" class="spbc_hint spbc_hint-send_security_log spbc_hint--link spbc_hint--top_right">
                            {{clear_log_text}}
                        </p>
                    </div>
                </div>
            </div>
        ';

        $log_actions_hidden = (
            ! empty($spbc->data['scanner']['last_scan']) ||
            in_array(Server::getDomain(), ['lc', 'loc', 'local', 'lh', 'wordpress'], true)
        );

        $_plate = [
            'clear_log_text' => __('Clear scan results', 'security-malware-firewall'),
            'loading_logs_text' => __('Loading logs..', 'security-malware-firewall'),
            'copy_log_text' => __('Copy to clipboard', 'security-malware-firewall'),
            'preloader_src' => esc_url(SPBC_PATH . '/images/preloader.gif'),
            'log_actions_hidden_class' => $log_actions_hidden ? '' : 'hidden',
        ];

        return self::textPlateRender($_text, $_plate);
    }

    /**
     * @param array $data
     * @return string
     */
    public static function generateRows(array $data): string
    {
        global $spbc;

        $rows_html = '';
        $prev_item_content = '';

        foreach ($data as $item) {
            if ($prev_item_content === $item['content']) {
                continue;
            }

            $_text = '<p class="spbc_log-line">{{date}}&nbsp;{{content}}</p>';
            $_plate = [
                'date' => date("M d Y H:i:s", $item['timestamp'] + $spbc->data['site_utc_offset_in_seconds']),
                'content' => wp_kses($item['content'], ['b' => array()]),
            ];

            $rows_html .= self::textPlateRender($_text, $_plate);

            $prev_item_content = $item['content'];
        }

        return $rows_html;
    }
}
