<?php
namespace IcreatorCookieConsent;

if ( ! defined( 'ABSPATH' ) ) exit;

class Banner {
    protected array $opts;

    public function __construct(array $override = []) {
        $this->opts = $this->load_options($override);
    }

    private function load_options(array $override): array {
        // Allowed values for class-safe output
        $positions = ['top','bottom','center','corner-left','corner-right'];
        $layouts   = ['full','centered','compact','minimal'];
        $btnStyle  = ['filled','outline','subtle'];
        $radius    = ['sharp','rounded','pill'];
        $fontFam   = ['inherit','system','serif','sans'];
        $fontSize  = ['small','normal','large'];

        // Fallback helpers (keep translations working)
        $default_accept = __('Accept all', 'consentpress');
        $default_decline = __('Reject all', 'consentpress');
        $default_save = __('Save preferences', 'consentpress');
        $default_manage = __('Manage preferences', 'consentpress');
        $default_title = __('We use cookies', 'consentpress');
        $default_text  = __('We use cookies to improve your experience. Manage your preferences below.', 'consentpress');
        $default_learn = __('Learn more', 'consentpress');

        $learn_page = get_option('iccb_learn_more_page', get_option('wp_page_for_privacy_policy'));

        $preset = get_option('iccb_actions_preset', 'balanced');

        $orders = [
        'balanced'       => ['reject','accept','manage'],
        'accept_first'   => ['accept','reject','manage'],
        'manage_between' => ['reject','manage','accept'],
        'stacked'        => ['reject','accept','manage'],
        ];

        $o = [
            // Appearance
            'accent_color'     => get_option('iccb_accent_color', '#000000'),
            'banner_title'     => get_option('iccb_banner_title', '' ) ?: $default_title,
            'banner_text'      => get_option('iccb_banner_text', '' ) ?: $default_text,
            'position'         => 'bottom',
            'layout'           => 'full',
            'button_style'     => 'filled',
            'button_radius'    => 'sharp',
            'font_family'      => 'inherit',
            'font_size'        => 'normal',
            'overlay_mode'     => 'none',
            'preset'           => 'balanced',
            'orders'           => $orders,
            // Labels (only stored if truly customized)
            'accept_label'     => get_option('iccb_accept_text', '' ) ?: $default_accept,
            'decline_label'    => get_option('iccb_decline_text', '' ) ?: $default_decline,
            'save_label'       => get_option('iccb_save_preference_text', '' ) ?: $default_save,
            'manage_label'     => get_option('iccb_manage_preference_text', '' ) ?: $default_manage,
            'learn_text'       => get_option('iccb_learn_more_text', '' ) ?: $default_learn,
            'learn_page'       => $learn_page,
            'learn_url'        => $learn_page ? get_permalink($learn_page) : get_privacy_policy_url(),

            // Defaults (seed prefs)
            'default_categories' => (array) get_option('iccb_default_categories', []),

            // Behavior
            'show_revoke'      => true,
        ];

        // Sanitize to allowed sets for CSS class usage
        $o['position']      = in_array($o['position'], $positions, true) ? $o['position'] : 'bottom';
        $o['layout']        = in_array($o['layout'], $layouts, true) ? $o['layout'] : 'full';
        $o['button_style']  = in_array($o['button_style'], $btnStyle, true) ? $o['button_style'] : 'filled';
        $o['button_radius'] = in_array($o['button_radius'], $radius, true) ? $o['button_radius'] : 'rounded';
        $o['font_family']   = in_array($o['font_family'], $fontFam, true) ? $o['font_family'] : 'inherit';
        $o['font_size']     = in_array($o['font_size'], $fontSize, true) ? $o['font_size'] : 'normal';

        // Allow devs to override programmatically
        $o = wp_parse_args($override, $o);
        return apply_filters('iccb_banner_options', $o);
    }

    protected function wrapper_classes(): string {
        $classes = [
            'iccb-banner',
            'iccb-hidden',
            'position-' . $this->opts['position'],
            'layout-' . $this->opts['layout'],
            'banner-style-' . $this->opts['button_style'],
            'radius-' . $this->opts['button_radius'],
            'fontfam-' . $this->opts['font_family'],
            'font-' . $this->opts['font_size'], // small/normal/large
        ];
        $classes = apply_filters('iccb_banner_classes', $classes, $this->opts);
        return implode(' ', array_map('sanitize_html_class', $classes));
    }

    protected function button_allowed_html(): array {
        return [
            'button' => [
                'id' => true,
                'class' => true,
                'type' => true,
            ],
        ];
    }

    public function output(): void {
        if ( ! apply_filters('iccb_should_render_banner', true) ) {
            return;
        }

        // data- attributes (read by JS)
        $data = [
            'accent'   => $this->opts['accent_color'],
            'defaults' => esc_attr( wp_json_encode( $this->opts['default_categories'] ) ),
        ];

        ?>
        <div id="iccb-overlay" class="iccb-overlay"></div>
        <div id="iccb-cookie-banner"
             class="<?php echo esc_attr( $this->wrapper_classes() ); ?>"
             <?php foreach ($data as $k=>$v): printf(' data-%s="%s"', esc_attr($k), esc_attr($v)); endforeach; ?>>

            <?php $this->render_summary(); ?>
            <?php $this->render_details(); ?>

        </div>

        <?php if ( $this->opts['show_revoke'] ) : ?>
            <button id="iccb-revoke" type="button" class="iccb-cookie-icon-btn iccb-hidden" aria-label="<?php esc_attr_e('Review cookie settings','consentpress'); ?>">
                <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="consentpress-icon" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
                    <g id="surface2">
                        <path class="circle" d="M 62 32 C 62 15.427083 48.572917 2 32 2 C 15.427083 2 2 15.427083 2 32 C 2 48.572917 15.427083 62 32 62 C 48.572917 62 62 48.572917 62 32 Z M 62 32 " transform="matrix(0.375,0,0,0.375,0,0)"/>
                        <path class="dot" d="M 9.75 9 C 9.75 8.171875 9.078125 7.5 8.25 7.5 C 7.421875 7.5 6.75 8.171875 6.75 9 C 6.75 9.828125 7.421875 10.5 8.25 10.5 C 9.078125 10.5 9.75 9.828125 9.75 9 Z M 9.75 9 "/>
                        <path class="dot" d="M 15.375 6.75 C 15.375 6.128906 14.871094 5.625 14.25 5.625 C 13.628906 5.625 13.125 6.128906 13.125 6.75 C 13.125 7.371094 13.628906 7.875 14.25 7.875 C 14.871094 7.875 15.375 7.371094 15.375 6.75 Z M 15.375 6.75 "/>
                        <path class="dot" d="M 17.625 12 C 17.625 10.964844 16.785156 10.125 15.75 10.125 C 14.714844 10.125 13.875 10.964844 13.875 12 C 13.875 13.035156 14.714844 13.875 15.75 13.875 C 16.785156 13.875 17.625 13.035156 17.625 12 Z M 17.625 12 "/>
                        <path class="dot" d="M 12 15.75 C 12 14.921875 11.328125 14.25 10.5 14.25 C 9.671875 14.25 9 14.921875 9 15.75 C 9 16.578125 9.671875 17.25 10.5 17.25 C 11.328125 17.25 12 16.578125 12 15.75 Z M 12 15.75 "/>
                    </g>
                </svg>
            </button>
        <?php endif; ?>
        <?php
    }

    protected function render_summary(): void {
        $locale_is_rtl = is_rtl();
        $orders = $this->opts['orders'];
        $preset = $this->opts['preset'];

        $order = $orders[$preset] ?? $orders['balanced'];
        if ($locale_is_rtl && $preset !== 'stacked') {
            $order = array_reverse($order); // mirror for RTL
        }

        $buttons = [
            'accept' => '<button id="iccb-accept-all" class="iccb-btn iccb-btn--primary">'.esc_html($this->opts['accept_label']).'</button>',
            'reject' => '<button id="iccb-reject-all" class="iccb-btn iccb-btn--secondary">'.esc_html($this->opts['decline_label']).'</button>',
            'manage' => '<button id="iccb-manage-preferences" class="iccb-btn iccb-btn--ghost">'.esc_html($this->opts['manage_label']).'</button>',
        ];
        ?>
        <div class="iccb-summary">
            <div class="iccb-text">
                <h3>
                    <span  class="iccb-cookie-icon">
                        <svg xmlns="http://www.w3.org/2000/svg" class="consentpress-icon" width="21" height="21" viewBox="0 0 21 21" role="img" aria-label="ConsentPress">
                            <g id="surface1">
                                <path class="circle" d="M 62 32 C 62 15.428571 48.571429 2 32 2 C 15.428571 2 2 15.428571 2 32 C 2 48.571429 15.428571 62 32 62 C 48.571429 62 62 48.571429 62 32 Z M 62 32 " transform="matrix(0.328125,0,0,0.328125,0,0)"/>
                                <path class="dot" d="M 8.53125 7.875 C 8.53125 7.148438 7.945312 6.5625 7.21875 6.5625 C 6.492188 6.5625 5.90625 7.148438 5.90625 7.875 C 5.90625 8.601562 6.492188 9.1875 7.21875 9.1875 C 7.945312 9.1875 8.53125 8.601562 8.53125 7.875 Z M 8.53125 7.875 "/>
                                <path class="dot" d="M 13.453125 5.90625 C 13.453125 5.363281 13.011719 4.921875 12.46875 4.921875 C 11.925781 4.921875 11.484375 5.363281 11.484375 5.90625 C 11.484375 6.449219 11.925781 6.890625 12.46875 6.890625 C 13.011719 6.890625 13.453125 6.449219 13.453125 5.90625 Z M 13.453125 5.90625 "/>
                                <path class="dot" d="M 15.421875 10.5 C 15.421875 9.59375 14.6875 8.859375 13.78125 8.859375 C 12.875 8.859375 12.140625 9.59375 12.140625 10.5 C 12.140625 11.40625 12.875 12.140625 13.78125 12.140625 C 14.6875 12.140625 15.421875 11.40625 15.421875 10.5 Z M 15.421875 10.5 "/>
                                <path class="dot" d="M 10.5 13.78125 C 10.5 13.054688 9.914062 12.46875 9.1875 12.46875 C 8.460938 12.46875 7.875 13.054688 7.875 13.78125 C 7.875 14.507812 8.460938 15.09375 9.1875 15.09375 C 9.914062 15.09375 10.5 14.507812 10.5 13.78125 Z M 10.5 13.78125 "/>
                            </g>
                        </svg>
                    </span>
                    <?php echo esc_html( $this->opts['banner_title']  ); ?>
                </h3>
                <p><?php echo esc_html( $this->opts['banner_text']  ); ?></p>
            </div>
            <div class="iccb-summary-actions">
                <?php 
                    if ($preset === 'stacked') {
                        echo '<div class="iccb-primary-actions">' . wp_kses($buttons[$order[0]], $this->button_allowed_html()) . wp_kses($buttons[$order[1]], $this->button_allowed_html()) . '</div>';
                        echo wp_kses($buttons[$order[2]], $this->button_allowed_html());
                    } else {
                        foreach ($order as $key) { echo wp_kses($buttons[$key], $this->button_allowed_html()); }
                    }
                ?>
            </div>
        </div>
        <?php
    }

    protected function render_details(): void {
        ?>
        <div class="iccb-details">
            <hr />
            <div id="iccb-banner-accordion"></div>
            <div class="iccb-actions">
                <button id="iccb-save-preferences">
                    <?php echo esc_html( $this->opts['save_label']  ); ?>
                </button>
            </div>
            <?php if ( $this->opts['learn_url'] ) : ?>
                <a class="iccb-learn-more" target="_blank" href="<?php echo esc_url( $this->opts['learn_url'] ); ?>">
                    <?php echo esc_html( $this->opts['learn_text'] ); ?>
                </a>
            <?php endif; ?>
        </div>
        <?php
    }
}
