<?php

namespace Storzen;

defined('ABSPATH') || exit;

class Renderer
{

    public static function page(Panel $panel, Menu $active_menu, $active_tab)
    {
        echo '<div class="wrap sz-wrap">';
        static::banner($panel);
        static::header($panel, $active_menu);
        echo '<div class="sz-body">';
        static::menu_bar($active_menu, $active_tab);
        static::pro_lock_notice($active_menu, $active_tab);
        if ($active_tab) {
            static::tab_content($active_menu, $active_tab);
        } else {
            echo '<p class="sz-empty">' . esc_html__('No tabs configured.', 'storzen') . '</p>';
        }
        echo '</div>';
        static::toast();
        echo '</div>';
    }

    public static function pro_lock_notice(Menu $menu, $active_tab)
    {
        $is_pro_active = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
        if ($is_pro_active) return;
        $is_locked = $menu->is_pro() || ($active_tab && $active_tab->is_pro());
        if (! $is_locked) return;
        echo '<div class="sz-pro-lock-notice">';
        echo '<span>🔒</span>';
        echo '<span>' . esc_html__('This section requires a PRO license.', 'storzen');
        echo ' <a href="https://storzen.webtend.net/pricing/" target="_blank" rel="noopener">' . esc_html__('Upgrade to PRO →', 'storzen') . '</a>';
        echo '</span></div>';
    }

    public static function banner(Panel $panel)
    {
        $b = $panel->get_banner();
        if (! $b) return;
        echo '<div class="sz-banner" id="pkBanner">';
        echo '<span>' . esc_html($b['text']) . '</span>';
        if ($b['btn_url']) {
            echo '<a href="' . esc_url($b['btn_url']) . '" class="sz-banner__btn" target="_blank" rel="noopener">' . esc_html($b['btn_label']) . '</a>';
        }
        echo '<button class="sz-banner__close" onclick="this.closest(\'.sz-banner\').style.display=\'none\'">&#x2715;</button>';
        echo '</div>';
    }

    public static function header(Panel $panel, Menu $active_menu)
    {
        $is_pro       = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
        $current_page = isset($_GET['page']) ? sanitize_key(wp_unslash($_GET['page'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended

        echo '<div class="sz-header">';
        echo '<div class="sz-header__brand"><span class="sz-header__dot"></span>' . esc_html($panel->get_title()) . '</div>';
        echo '<nav class="sz-header__nav">';

        // Panel menus (Elementor Widget, Page Templates).
        foreach ($panel->get_menus() as $menu) {
            $url    = admin_url('admin.php?page=' . $menu->get_page_slug());
            $active = ($menu->get_slug() === $active_menu->get_slug());
            $cls    = 'sz-header__tab' . ($active ? ' sz-header__tab--active' : '');
            echo '<a href="' . esc_url($url) . '" class="' . esc_attr($cls) . '">';
            if ($menu->get_icon()) echo '<span class="sz-header__tab-icon">' . esc_html($menu->get_icon()) . '</span>';
            echo esc_html($menu->get_title());
            if ($menu->is_pro() && ! $is_pro) {
                echo ' <span class="sz-pro-badge" style="font-size:.55rem;padding:1px 4px 2px">PRO</span>';
            }
            echo '</a>';
        }

        // Modules tab.
        $active = ('storzen-modules' === $current_page);
        echo '<a href="' . esc_url(admin_url('admin.php?page=storzen-modules')) . '" class="sz-header__tab' . ($active ? ' sz-header__tab--active' : '') . '">'
            . '<span class="sz-header__tab-icon">&#9889;</span>'
            . esc_html__('Modules', 'storzen')
            . '</a>';



        // Import Templates tab.
        if (class_exists('Storzen_Elementor_Template_Import')) {
            $active = ('storzen-import' === $current_page);
            echo '<a href="' . esc_url(admin_url('admin.php?page=storzen-import-templates')) . '" class="sz-header__tab' . ($active ? ' sz-header__tab--active' : '') . '">'
                . '<span class="sz-header__tab-icon">📥</span>'
                . esc_html__('Import Templates', 'storzen')
                . '</a>';
        }

        echo '</nav></div>';
    }

    public static function menu_bar(Menu $menu, $active_tab)
    {
        $tabs          = $menu->get_tabs();
        $is_pro_active = defined('STORZEN_IS_PRO') && STORZEN_IS_PRO === 'yes';
        $tab_locked    = $active_tab && $active_tab->is_pro() && ! $is_pro_active;
        $menu_locked   = $menu->is_pro() && ! $is_pro_active;
        $is_locked     = $tab_locked || $menu_locked;

        echo '<div class="sz-menubar">';
        echo '<div class="sz-menubar__top">';
        echo '<div class="sz-menubar__info">';
        echo '<h2>' . esc_html($menu->get_title());
        if ($menu->get_icon()) echo ' <span>' . esc_html($menu->get_icon()) . '</span>';
        echo '</h2>';
        if ($active_tab && $active_tab->get_description()) {
            echo '<p>' . esc_html($active_tab->get_description()) . '</p>';
        }
        echo '</div>';
        if ($active_tab && ! $is_locked) {
            echo '<button type="button" class="sz-btn sz-btn--primary js-sz-save"';
            echo ' data-menu="' . esc_attr($menu->get_slug()) . '"';
            echo ' data-tab="' . esc_attr($active_tab->get_slug()) . '">';
            esc_html_e('Save Settings', 'storzen');
            echo '</button>';
        }
        echo '</div>';

        if (count($tabs) > 1) {
            echo '<div class="sz-tab-pills">';
            foreach ($tabs as $tab) {
                $url    = admin_url('admin.php?page=' . $menu->get_page_slug() . '&tab=' . $tab->get_slug());
                $active = $active_tab && ($tab->get_slug() === $active_tab->get_slug());
                $cls    = 'sz-tab-pill' . ($active ? ' sz-tab-pill--active' : '');
                echo '<a href="' . esc_url($url) . '" class="' . esc_attr($cls) . '">';
                if ($tab->get_icon()) echo '<span>' . esc_html($tab->get_icon()) . '</span>';
                echo esc_html($tab->get_title());
                if ($tab->is_pro() && ! $is_pro_active) {
                    echo ' <span class="sz-pro-badge" style="font-size:.55rem;padding:1px 4px 2px">PRO</span>';
                }
                echo '</a>';
            }
            echo '</div>';
        }
        echo '</div>';
    }

    public static function tab_content(Menu $menu, Tab $tab)
    {
        $values = $tab->get_values();
        echo '<div class="sz-content" id="pkContent"';
        echo ' data-menu="' . esc_attr($menu->get_slug()) . '"';
        echo ' data-tab="' . esc_attr($tab->get_slug()) . '">';
        foreach ($tab->get_sections() as $section) {
            static::section($section, $values);
        }
        echo '</div>';
    }

    public static function section(Section $section, array $values)
    {
        if ($section->get_layout() === 'grid') {
            static::section_grid($section, $values);
        } else {
            static::section_settings($section, $values);
        }
    }

    /* ── Settings rows layout ── */
    public static function section_settings(Section $section, array $values)
    {
        echo '<div class="sz-section sz-section--settings" id="pks-' . esc_attr($section->get_slug()) . '">';
        if ($section->get_title()) {
            echo '<div class="sz-section__head">';
            echo '<span class="sz-section__label">' . esc_html($section->get_title()) . '</span>';
            if ($section->get_description()) {
                echo '<p>' . esc_html($section->get_description()) . '</p>';
            }
            echo '</div>';
        }
        echo '<div class="sz-section__rows">';
        foreach ($section->get_fields() as $field) {
            $val = isset($values[$field->get_slug()]) ? $values[$field->get_slug()] : $field->get_default();
            static::field_row($field, $val);
        }
        echo '</div></div>';
    }

    /* ── Grid card layout (original widget-manager style) ── */
    public static function section_grid(Section $section, array $values)
    {
        $cols = $section->get_columns();
        echo '<div class="sz-section sz-section--grid" id="pks-' . esc_attr($section->get_slug()) . '">';
        if ($section->get_title() || $section->get_description()) {
            echo '<div class="sz-section__head sz-section__head--grid">';
            if ($section->get_title()) echo '<span class="sz-section__label">' . esc_html($section->get_title()) . '</span>';
            if ($section->get_description()) echo '<p>' . esc_html($section->get_description()) . '</p>';
            echo '</div>';
        }
        echo '<div class="sz-grid sz-grid--cols-' . esc_attr($cols) . '">';
        foreach ($section->get_fields() as $field) {
            $val = isset($values[$field->get_slug()]) ? $values[$field->get_slug()] : $field->get_default();
            static::field_grid_card($field, $val);
        }
        echo '</div></div>';
    }

    /* ── Single settings row ── */
    public static function field_row(\Storzen\Contracts\Field $field, $value)
    {
        $type = $field->get_type();
        if ($type === 'notice') {
            echo '<div class="sz-row sz-row--notice">';
            $field->render($value);
            echo '</div>';
            return;
        }
        $cls = 'sz-row sz-row--' . $type . ($field->is_pro() ? ' sz-row--pro' : '');
        echo '<div class="' . esc_attr($cls) . '">';
        echo '<div class="sz-row__label">';
        echo '<label for="pk_' . esc_attr($field->get_slug()) . '">' . esc_html($field->get_label()) . '</label>';
        if ($field->get_description()) {
            echo '<p>' . esc_html($field->get_description()) . '</p>';
        }
        echo '</div>';
        echo '<div class="sz-row__control">';
        $field->render($value);
        echo '</div></div>';
    }

    /* ── Grid toggle card ── */
    public static function field_grid_card(\Storzen\Contracts\Field $field, $value)
    {
        $cls = 'sz-card' . ($field->is_pro() ? ' sz-card--pro' : '');
        echo '<div class="' . esc_attr($cls) . '">';
        if ($field->is_pro()) echo '<span class="sz-card__corner"></span>';
        echo '<span class="sz-card__label">' . esc_html($field->get_label());
        echo '</span>';
        $field->render($value);
        echo '</div>';
    }

    public static function toast()
    {
        echo '<div class="sz-toast" id="pkToast" role="alert" aria-live="polite" aria-atomic="true">';
        echo '<span class="sz-toast__dot"></span><span class="sz-toast__msg"></span>';
        echo '</div>';
    }
}
