<?php
declare(strict_types=1);


/**
 * Elementor Breadcrumbs Widget
 *
 * @package ProRank\Integrations\Elementor\Widgets
 */

namespace ProRank\SEO\Integrations\Elementor\Widgets;

use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Group_Control_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
use Elementor\Core\Kits\Documents\Tabs\Global_Colors;

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




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

/**
 * Breadcrumbs widget for Elementor
 */
class BreadcrumbsWidget extends Widget_Base {

    /**
     * Get widget name
     */
    public function get_name(): string {
        return 'prorank-breadcrumbs';
    }

    /**
     * Get widget title
     */
    public function get_title(): string {
        return __('Breadcrumbs', 'prorank-seo');
    }

    /**
     * Get widget icon
     */
    public function get_icon(): string {
        return 'eicon-navigation-horizontal';
    }

    /**
     * Get widget categories
     */
    public function get_categories(): array {
        return ['prorank-seo'];
    }

    /**
     * Get widget keywords
     */
    public function get_keywords(): array {
        return ['breadcrumbs', 'navigation', 'seo', 'schema', 'prorank'];
    }

    /**
     * Register widget controls
     */
    protected function register_controls(): void {
        // Content Section
        $this->start_controls_section(
            'section_content',
            [
                'label' => __('Breadcrumbs Settings', 'prorank-seo'),
                'tab'   => Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'separator_style',
            [
                'label'   => __('Separator Style', 'prorank-seo'),
                'type'    => Controls_Manager::SELECT,
                'default' => 'arrow',
                'options' => [
                    'arrow'   => __('Arrow (>)', 'prorank-seo'),
                    'slash'   => __('Slash (/)', 'prorank-seo'),
                    'pipe'    => __('Pipe (|)', 'prorank-seo'),
                    'chevron' => __('Chevron (»)', 'prorank-seo'),
                    'dot'     => __('Dot (•)', 'prorank-seo'),
                ],
            ]
        );

        $this->add_control(
            'show_home',
            [
                'label'        => __('Show Home', 'prorank-seo'),
                'type'         => Controls_Manager::SWITCHER,
                'label_on'     => __('Yes', 'prorank-seo'),
                'label_off'    => __('No', 'prorank-seo'),
                'return_value' => 'yes',
                'default'      => 'yes',
            ]
        );

        $this->add_control(
            'home_text',
            [
                'label'     => __('Home Text', 'prorank-seo'),
                'type'      => Controls_Manager::TEXT,
                'default'   => __('Home', 'prorank-seo'),
                'condition' => [
                    'show_home' => 'yes',
                ],
            ]
        );

        $this->add_control(
            'show_current',
            [
                'label'        => __('Show Current Page', 'prorank-seo'),
                'type'         => Controls_Manager::SWITCHER,
                'label_on'     => __('Yes', 'prorank-seo'),
                'label_off'    => __('No', 'prorank-seo'),
                'return_value' => 'yes',
                'default'      => 'yes',
            ]
        );

        $this->add_control(
            'enable_schema',
            [
                'label'        => __('Enable Schema Markup', 'prorank-seo'),
                'type'         => Controls_Manager::SWITCHER,
                'label_on'     => __('Yes', 'prorank-seo'),
                'label_off'    => __('No', 'prorank-seo'),
                'return_value' => 'yes',
                'default'      => 'yes',
                'description'  => __('Adds BreadcrumbList structured data for rich search results', 'prorank-seo'),
            ]
        );

        $this->end_controls_section();

        // Style Section - General
        $this->start_controls_section(
            'section_style_general',
            [
                'label' => __('General', 'prorank-seo'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'alignment',
            [
                'label'   => __('Alignment', 'prorank-seo'),
                'type'    => Controls_Manager::CHOOSE,
                'options' => [
                    'left'   => [
                        'title' => __('Left', 'prorank-seo'),
                        'icon'  => 'eicon-text-align-left',
                    ],
                    'center' => [
                        'title' => __('Center', 'prorank-seo'),
                        'icon'  => 'eicon-text-align-center',
                    ],
                    'right'  => [
                        'title' => __('Right', 'prorank-seo'),
                        'icon'  => 'eicon-text-align-right',
                    ],
                ],
                'default'   => 'left',
                'selectors' => [
                    '{{WRAPPER}} .prorank-breadcrumbs' => 'text-align: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name'     => 'typography',
                'selector' => '{{WRAPPER}} .prorank-breadcrumbs',
                'global'   => [
                    'default' => Global_Typography::TYPOGRAPHY_TEXT,
                ],
            ]
        );

        $this->add_responsive_control(
            'item_spacing',
            [
                'label'      => __('Item Spacing', 'prorank-seo'),
                'type'       => Controls_Manager::SLIDER,
                'size_units' => ['px', 'em'],
                'range'      => [
                    'px' => [
                        'min' => 0,
                        'max' => 50,
                    ],
                    'em' => [
                        'min' => 0,
                        'max' => 3,
                    ],
                ],
                'default'    => [
                    'size' => 8,
                    'unit' => 'px',
                ],
                'selectors'  => [
                    '{{WRAPPER}} .prorank-breadcrumbs .separator' => 'margin-left: {{SIZE}}{{UNIT}}; margin-right: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Links
        $this->start_controls_section(
            'section_style_links',
            [
                'label' => __('Links', 'prorank-seo'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->start_controls_tabs('link_colors');

        $this->start_controls_tab(
            'link_normal',
            [
                'label' => __('Normal', 'prorank-seo'),
            ]
        );

        $this->add_control(
            'link_color',
            [
                'label'     => __('Link Color', 'prorank-seo'),
                'type'      => Controls_Manager::COLOR,
                'global'    => [
                    'default' => Global_Colors::COLOR_PRIMARY,
                ],
                'selectors' => [
                    '{{WRAPPER}} .prorank-breadcrumbs a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'link_hover',
            [
                'label' => __('Hover', 'prorank-seo'),
            ]
        );

        $this->add_control(
            'link_hover_color',
            [
                'label'     => __('Link Hover Color', 'prorank-seo'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .prorank-breadcrumbs a:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->end_controls_tabs();

        $this->end_controls_section();

        // Style Section - Current
        $this->start_controls_section(
            'section_style_current',
            [
                'label' => __('Current Page', 'prorank-seo'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'current_color',
            [
                'label'     => __('Current Page Color', 'prorank-seo'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .prorank-breadcrumbs .current' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();

        // Style Section - Separator
        $this->start_controls_section(
            'section_style_separator',
            [
                'label' => __('Separator', 'prorank-seo'),
                'tab'   => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'separator_color',
            [
                'label'     => __('Separator Color', 'prorank-seo'),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .prorank-breadcrumbs .separator' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Render widget output
     */
    protected function render(): void {
        $settings = $this->get_settings_for_display();

        // Get separator character
        $separators = [
            'arrow'   => '>',
            'slash'   => '/',
            'pipe'    => '|',
            'chevron' => '»',
            'dot'     => '•',
        ];
        $separator = $separators[$settings['separator_style']] ?? '>';

        // Build shortcode attributes
        $atts = [
            'separator'    => $separator,
            'show_home'    => $settings['show_home'] === 'yes' ? 'true' : 'false',
            'home_text'    => $settings['home_text'] ?: __('Home', 'prorank-seo'),
            'show_current' => $settings['show_current'] === 'yes' ? 'true' : 'false',
            'schema'       => $settings['enable_schema'] === 'yes' ? 'true' : 'false',
        ];

        // Build shortcode string
        $shortcode_atts = '';
        foreach ($atts as $key => $value) {
            $shortcode_atts .= sprintf(' %s="%s"', $key, esc_attr($value));
        }

        // Output breadcrumbs
        echo do_shortcode('[prorank_breadcrumbs' . $shortcode_atts . ']');
    }

    /**
     * Render widget output in editor
     */
    protected function content_template(): void {
        ?>
        <#
        var separators = {
            'arrow': '>',
            'slash': '/',
            'pipe': '|',
            'chevron': '»',
            'dot': '•'
        };
        var separator = separators[settings.separator_style] || '>';
        var homeText = settings.home_text || 'Home';
        #>
        <nav class="prorank-breadcrumbs" aria-label="Breadcrumb">
            <# if ( settings.show_home === 'yes' ) { #>
                <a href="#">{{{ homeText }}}</a>
                <span class="separator">{{{ separator }}}</span>
            <# } #>
            <a href="#">Category</a>
            <span class="separator">{{{ separator }}}</span>
            <# if ( settings.show_current === 'yes' ) { #>
                <span class="current">Current Page</span>
            <# } #>
        </nav>
        <?php
    }
}
