<?php

namespace GriffinForms\Admin\App\Html;

class Format extends \GriffinForms\Admin\App\Format
{
    protected $utility;
    protected $inline_icons;
    protected $buttons;
    protected $response;

    final protected function setUtility()
    {
        $this->utility = \GriffinForms\Admin\App\Utility::getInstance();
    }

    final protected function setButtons()
    {
        $this->buttons = \GriffinForms\Admin\Html\Elements\Buttons::getInstance();
    }

    final protected function setInlineIcons()
    {
        $this->inline_icons = \GriffinForms\Admin\App\Html\Icons\InlineIcons::getInstance();
    }

    protected function getHtmlId($affix = '')
    {
        if (!empty($affix)) {
            $affix = '-' . $affix;
        }

        return strtolower('griffinforms-app-' . $this->meta . $affix);
    }

    protected function mIcon($icon, $size = '24')
    {
        $icon_class = 'material-symbols-outlined griffinforms-material-symbol align-text-bottom';
        echo '<span class="' . esc_attr($icon_class) . '"';
        echo' style="font-size: ' . absint($size) . 'px">';
        echo esc_attr($icon);
        echo '</span>';
    }

    protected function getElement(string $name)
    {
        $element_class = '\GriffinForms\Admin\App\Html\Elements\\' . $name;
        $element = $element_class::getInstance();
        return $element;
    }

    protected function tableHeader(array $headings = array())
    {
        echo '<thead><tr>';

        foreach ($headings as $heading) {
            echo '<th scope="col">' . esc_html($heading) . '</th>';
        }
        
        echo '</tr></thead>';
    }

    protected function propRow($name, $value, $allow_html = false)
    {
        echo '<div class="row mb-1 small">';
        echo '<div class="col-md-4 mb-1">';
        echo esc_html($name);
        echo '</div>';
        echo '<div class="col-md-8">';
        $this->propValue($value, $allow_html);
        echo '</div>';
        echo '</div>';
    }

    protected function propValue($value, $allow_html = false)
    {
        if ($value === '') {
            echo '<span class="text-muted fst-italic">';
            echo esc_html('--', 'griffinforms-form-builder');
            echo '</span>';
        } else if (!$allow_html && strlen($value) > 150) {
            echo esc_html(substr($value, 0, 150) . '...');
        } else if ($allow_html) {
            echo wp_kses_post($value);
        } else {
            echo esc_html($value);
        }
    }

    final protected function checkRequest($nonce)
    {
        $nonce_status = isset($_POST['nonce']) ? wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), $nonce) : false;
        
        if ($nonce_status === false) {
            if (is_object($this->log)) {
                $this->log->add(
                    'error',
                    'request',
                    0,
                    (int) get_current_user_id(),
                    sprintf('Nonce validation failed for "%s".', sanitize_text_field($nonce)),
                    'request_validation'
                );
            }

            if (!is_array($this->response)) {
                $this->response = [];
            }

            $this->response['success'] = false;
            $this->response['msg'] = __('Nonce error', 'griffinforms-form-builder');
            wp_die();
        }

        $required_capability = property_exists($this, 'required_capability') ? $this->required_capability : 'manage_options';

        if (!empty($required_capability) && !(current_user_can($required_capability))) {
            if (is_object($this->log)) {
                $this->log->add(
                    'error',
                    'request',
                    0,
                    (int) get_current_user_id(),
                    sprintf('Capability check failed for "%s".', sanitize_text_field($nonce)),
                    'request_validation'
                );
            }

            if (!is_array($this->response)) {
                $this->response = [];
            }

            $this->response['success'] = false;
            $this->response['msg'] = __('You do not have sufficient privileges to do this.', 'griffinforms-form-builder');
            wp_die();
        }
    }
}
