<?php
/**
 * Biscotti Oxygen Builder Elements
 *
 * Registers Oxygen Builder elements for all Biscotti shortcodes.
 * Each element extends OxyEl and renders via do_shortcode().
 *
 * @package Biscotti_CMP
 * @since 3.1.0
 */

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

// Guard: only load if Oxygen Builder is active
if (!class_exists('OxyEl')) {
    return;
}

class Biscotti_Oxygen_Elements
{
    private static $instance = null;

    public static function get_instance()
    {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function __construct()
    {
        add_action('oxygen_add_plus_sections', [$this, 'add_section']);
        add_action('init', [$this, 'register_elements']);
    }

    public function add_section()
    {
        if (function_exists('ct_new_component_category')) {
            ct_new_component_category('Biscotti CMP');
        }
    }

    public function register_elements()
    {
        // Each element class auto-registers when instantiated
        if (class_exists('OxyEl')) {
            new Biscotti_Oxy_Cookie_Table();
            new Biscotti_Oxy_Consent_Link();
            new Biscotti_Oxy_Settings_Link();
            new Biscotti_Oxy_Data_Export();
            new Biscotti_Oxy_Do_Not_Sell();
            new Biscotti_Oxy_Impressum();
            new Biscotti_Oxy_Privacy_Policy();
            new Biscotti_Oxy_Cookie_Policy();
        }
    }
}

// =============================================================================
// BASE ELEMENT
// =============================================================================
class Biscotti_Oxy_Base extends OxyEl
{
    protected $shortcode_tag = '';

    public function afterInit()
    {
        $this->removeApplyParamsButton();
    }

    public function icon()
    {
        return plugin_dir_url(BISCOTTI_PLUGIN_FILE) . 'assets/biscotti-logo.png';
    }

    public function render($options, $defaults, $content)
    {
        $atts = $this->build_shortcode_atts($options);
        // Shortcode output is escaped within each shortcode's render method
        echo do_shortcode('[' . esc_attr($this->shortcode_tag) . $atts . ']');
    }

    protected function build_shortcode_atts($options)
    {
        return '';
    }

    public function customCSS($original, $selector)
    {
        return $original;
    }

    protected function get_lang_options()
    {
        return Biscotti_Languages::select_options( true, __( 'Auto (from WordPress)', 'biscotti-cmp' ) );
    }
}

// =============================================================================
// COOKIE TABLE
// =============================================================================
class Biscotti_Oxy_Cookie_Table extends Biscotti_Oxy_Base
{
    protected $shortcode_tag = 'biscotti_cookies';

    public function name()
    {
        return __('Biscotti Cookie Table', 'biscotti-cmp');
    }

    public function slug()
    {
        return 'biscotti-cookie-table';
    }

    public function controls()
    {
        $this->addOptionControl([
            'type' => 'dropdown',
            'name' => __('Category', 'biscotti-cmp'),
            'slug' => 'bsc_category',
            'default' => 'all',
        ])->setValue(['all', 'essential', 'functional', 'analytics', 'marketing']);

        $this->addOptionControl([
            'type' => 'dropdown',
            'name' => __('Show Duration', 'biscotti-cmp'),
            'slug' => 'bsc_show_duration',
            'default' => 'true',
        ])->setValue(['true', 'false']);

        $this->addOptionControl([
            'type' => 'dropdown',
            'name' => __('Show Provider', 'biscotti-cmp'),
            'slug' => 'bsc_show_provider',
            'default' => 'true',
        ])->setValue(['true', 'false']);
    }

    protected function build_shortcode_atts($options)
    {
        $atts = ' category="' . esc_attr($options['bsc_category'] ?? 'all') . '"';
        $atts .= ' show_duration="' . esc_attr($options['bsc_show_duration'] ?? 'true') . '"';
        $atts .= ' show_provider="' . esc_attr($options['bsc_show_provider'] ?? 'true') . '"';
        return $atts;
    }
}

// =============================================================================
// CONSENT LINK
// =============================================================================
class Biscotti_Oxy_Consent_Link extends Biscotti_Oxy_Base
{
    protected $shortcode_tag = 'biscotti_consent_link';

    public function name() { return __('Biscotti Consent Link', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-consent-link'; }

    public function controls()
    {
        $this->addOptionControl([
            'type' => 'textfield',
            'name' => __('Link Text', 'biscotti-cmp'),
            'slug' => 'bsc_text',
            'default' => __('Change cookie settings', 'biscotti-cmp'),
        ]);
    }

    protected function build_shortcode_atts($options)
    {
        return !empty($options['bsc_text']) ? ' text="' . esc_attr($options['bsc_text']) . '"' : '';
    }
}

// =============================================================================
// SETTINGS LINK
// =============================================================================
class Biscotti_Oxy_Settings_Link extends Biscotti_Oxy_Base
{
    protected $shortcode_tag = 'biscotti_settings_link';

    public function name() { return __('Biscotti Settings Link', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-settings-link'; }

    public function controls()
    {
        $this->addOptionControl([
            'type' => 'textfield',
            'name' => __('Link Text', 'biscotti-cmp'),
            'slug' => 'bsc_text',
            'default' => __('Cookie Settings', 'biscotti-cmp'),
        ]);
    }

    protected function build_shortcode_atts($options)
    {
        return !empty($options['bsc_text']) ? ' text="' . esc_attr($options['bsc_text']) . '"' : '';
    }
}

// =============================================================================
// DATA EXPORT
// =============================================================================
class Biscotti_Oxy_Data_Export extends Biscotti_Oxy_Base
{
    protected $shortcode_tag = 'biscotti_data_export';

    public function name() { return __('Biscotti Data Export', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-data-export'; }
    public function controls() {}
}

// =============================================================================
// DO NOT SELL
// =============================================================================
class Biscotti_Oxy_Do_Not_Sell extends Biscotti_Oxy_Base
{
    protected $shortcode_tag = 'biscotti_do_not_sell';

    public function name() { return __('Biscotti Do Not Sell (CCPA)', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-do-not-sell'; }

    public function controls()
    {
        $this->addOptionControl([
            'type' => 'textfield',
            'name' => __('Link Text', 'biscotti-cmp'),
            'slug' => 'bsc_text',
            'default' => __('Do Not Sell My Data', 'biscotti-cmp'),
        ]);
    }

    protected function build_shortcode_atts($options)
    {
        return !empty($options['bsc_text']) ? ' text="' . esc_attr($options['bsc_text']) . '"' : '';
    }
}

// =============================================================================
// LEGAL DOCUMENT ELEMENTS
// =============================================================================
class Biscotti_Oxy_Legal_Base extends Biscotti_Oxy_Base
{
    public function controls()
    {
        $this->addOptionControl([
            'type' => 'dropdown',
            'name' => __('Enable Cache', 'biscotti-cmp'),
            'slug' => 'bsc_cache',
            'default' => 'true',
        ])->setValue(['true', 'false']);

        $this->addOptionControl([
            'type' => 'dropdown',
            'name' => __('Language', 'biscotti-cmp'),
            'slug' => 'bsc_lang',
            'default' => 'auto',
        ])->setValue(array_keys($this->get_lang_options()));
    }

    protected function build_shortcode_atts($options)
    {
        $atts = ' cache="' . esc_attr($options['bsc_cache'] ?? 'true') . '"';
        $lang = $options['bsc_lang'] ?? 'auto';
        if ($lang !== 'auto') {
            $atts .= ' lang="' . esc_attr($lang) . '"';
        }
        return $atts;
    }
}

class Biscotti_Oxy_Impressum extends Biscotti_Oxy_Legal_Base
{
    protected $shortcode_tag = 'biscotti_impressum';
    public function name() { return __('Biscotti Impressum', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-impressum'; }
}

class Biscotti_Oxy_Privacy_Policy extends Biscotti_Oxy_Legal_Base
{
    protected $shortcode_tag = 'biscotti_privacy_policy';
    public function name() { return __('Biscotti Privacy Policy', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-privacy-policy'; }
}

class Biscotti_Oxy_Cookie_Policy extends Biscotti_Oxy_Legal_Base
{
    protected $shortcode_tag = 'biscotti_cookie_policy';
    public function name() { return __('Biscotti Cookie Policy', 'biscotti-cmp'); }
    public function slug() { return 'biscotti-cookie-policy'; }
}

// Initialize
Biscotti_Oxygen_Elements::get_instance();
