<?php
if ( ! defined( 'ABSPATH' ) ) exit;
use Sifency\Classes\Sifency_Addons_Widget_Base;
use Elementor\Widget_Base;
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Button_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Custom_CSS_Controls;
class Sifency_OnePage_Nav extends Sifency_Addons_Widget_Base {
    use Sifency_Addons_Icon_Style_Controls;
    use Sifency_Addons_Item_Style_Controls;
    use Sifency_Addons_Master_Typography;
    use Sifency_Addons_Button_Style_Controls;
    use Sifency_Addons_Flex_Controls;
    use Sifency_Addons_Custom_CSS_Controls;
    use Sifency_Addons_Help_Controls;
	public function __construct( $data = [], $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = [
        'name'       => 'sifency_onepage_nav',
        'title'      => 'Sifency One Page Nav',
        'icon'       => 'eicon-navigator',
        'category' => [ 'sifency-addons-widgets' ],
        'script_depends'    => [ 'sifency-one-page-nav-script' ],
        'style_depends'     => [ 'sifency-helper-style' ],
        'keywords'      => ['one page nav', 'full page navigation', 'scroll spy', 'section navigation', 'snap scroll menu'],
    ];}
    protected function register_controls() {
        $this->start_controls_section('section_menu', [
            'label' => esc_html__( 'Navigation Dots', 'sifency-addons' ),
        ]);
        $repeater = new Repeater();
        $repeater->add_control('menu_label', [
            'label' => esc_html__( 'Menu Title (Tooltip)', 'sifency-addons' ),
            'type' => Controls_Manager::TEXT,
            'default' => 'Section Name',
        ]);

        $repeater->add_control('section_id', [
            'label' => esc_html__( 'Section CSS ID', 'sifency-addons' ),
            'type' => Controls_Manager::TEXT,
            'placeholder' => 'my-section-id',
            'description' => 'Enter the ID without # (e.g. "about"). Make sure the target section has this ID.',
        ]);

        $repeater->add_control('menu_icon', [
            'label' => esc_html__( 'Icon', 'sifency-addons' ),
            'type' => Controls_Manager::ICONS,
        ]);

        $this->add_control('menu_items', [
            'label' => esc_html__( 'Navigation Items', 'sifency-addons' ),
            'type' => Controls_Manager::REPEATER,
            'fields' => $repeater->get_controls(),
            'default' => [
                [ 'menu_label' => 'Home', 'section_id' => 'home' ],
                [ 'menu_label' => 'About', 'section_id' => 'about' ],
            ],
            'title_field' => '{{{ menu_label }}}',
        ]);
        $this->end_controls_section();

        $this->start_controls_section('section_settings', [
            'label' => esc_html__( 'Display Settings', 'sifency-addons' ),
        ]);

        $this->add_control('layout_type', [
            'label' => esc_html__( 'Layout Mode', 'sifency-addons' ),
            'type' => Controls_Manager::SELECT,
            'default' => 'dots',
            'options' => [
                'dots'  => esc_html__( 'Minimal Dots', 'sifency-addons' ),
                'icons' => esc_html__( 'Icon Menu', 'sifency-addons' ),
            ],
        ]);

        $this->add_control('enable_snap', [
            'label' => esc_html__( 'Full Page Scroll Snap (100vh)', 'sifency-addons' ),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'no',
            'description' => 'Forces the page to snap perfectly to sections.',
        ]);

        $this->add_control('header_offset', [
            'label' => esc_html__( 'Header Offset (px)', 'sifency-addons' ),
            'type' => Controls_Manager::SLIDER,
            'size_units' => [ 'px' ],
            'range' => [
                'px' => [ 'min' => 0, 'max' => 200, 'step' => 1 ],
            ],
            'default' => [ 'unit' => 'px', 'size' => 0 ],
            'condition' => [ 'enable_snap' => 'yes' ],
        ]);

        $this->add_control('show_tooltip', [
            'label' => esc_html__( 'Show Tooltip on Hover', 'sifency-addons' ),
            'type' => Controls_Manager::SWITCHER,
            'default' => 'yes',
        ]);

        $this->add_control('nav_position', [
            'label' => esc_html__( 'Position', 'sifency-addons' ),
            'type' => Controls_Manager::SELECT,
            'options' => [
                'top-left' => esc_html__( 'Top Left', 'sifency-addons' ),
                'top-center' => esc_html__( 'Top Center', 'sifency-addons' ),
                'top-right' => esc_html__( 'Top Right', 'sifency-addons' ),
                'left'  => esc_html__( 'Middle Left', 'sifency-addons' ),
                'right' => esc_html__( 'Middle Right', 'sifency-addons' ),
                'bottom-left' => esc_html__( 'Bottom Left', 'sifency-addons' ),
                'bottom-center' => esc_html__( 'Bottom Center', 'sifency-addons' ),
                'bottom-right' => esc_html__( 'Bottom Right', 'sifency-addons' ),
            ],
            'default' => 'right',
        ]);
        $this->add_responsive_control( 'gap', [ 
            'label' => 'Gap', 
            'type' => Controls_Manager::SLIDER, 
            'range' => ['px' => ['min' => 0, 'max' => 100]],
            'selectors' => [ 
                '{{WRAPPER}} .sifency-one-page-nav-list' => 'gap: {{SIZE}}{{UNIT}};' 
            ],
        ]);
        $this->add_sifency_addons_flex_alignment_controls( [
            'selector' => '.sifency-one-page-nav-list',
        ]);
        $this->end_controls_section();
        $this->add_sifency_addons_help_controls();
        $this->start_controls_section(
            'section_nav_position',
            [
                'label' => esc_html__( 'Navigation Position', 'sifency-addons' ),
                'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_responsive_control(
            'nav_vertical_position',
            [
                'label' => esc_html__( 'Vertical Position (%)', 'sifency-addons' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ '%', 'px', 'vh' ],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                    'px' => [
                        'min' => 0,
                        'max' => 1000,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-one-page-nav-container' => 'top: {{SIZE}}{{UNIT}}; transform: translateY(-{{SIZE}}{{UNIT}});',
                ],
                'condition' => [
                    'nav_position' => ['left', 'right'],
                ],
            ]
        );
        $this->add_responsive_control(
            'nav_horizontal_position',
            [
                'label' => esc_html__( 'Horizontal Position (%)', 'sifency-addons' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ '%', 'px', 'vw' ],
                'range' => [
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                    ],
                    'px' => [
                        'min' => 0,
                        'max' => 1000,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-one-page-nav-container' => 'left: {{SIZE}}{{UNIT}}; transform: translateY(-{{SIZE}}{{UNIT}});',
                ],
                'condition' => [
                    'nav_position' => ['left', 'right'],
                ],
            ]
        );

        $this->add_responsive_control(
            'nav_corner_y_offset',
            [
                'label' => esc_html__( 'Vertical Offset', 'sifency-addons' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ 'px', '%', 'vh' ],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 500,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-top-left' => 'top: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-top-right' => 'top: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-top-center' => 'top: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-bottom-left' => 'bottom: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-bottom-right' => 'bottom: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-bottom-center' => 'bottom: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'nav_position!' => ['left', 'right'],
                ],
            ]
        );

        $this->add_responsive_control(
            'nav_corner_x_offset',
            [
                'label' => esc_html__( 'Horizontal Offset', 'sifency-addons' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'size_units' => [ 'px', '%', 'vw' ],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 500,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-top-left' => 'left: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-bottom-left' => 'left: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-top-right' => 'right: {{SIZE}}{{UNIT}};',
                    '{{WRAPPER}} .sifency-one-page-nav-container.pos-bottom-right' => 'right: {{SIZE}}{{UNIT}};',
                ],
                'condition' => [
                    'nav_position' => ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
                ],
            ]
        );

        $this->end_controls_section();
        $this->add_sifency_addons_item_style_controls([
            'selector'        => '.sifency-one-page-nav-list',
            'exclude'         => [ 'tabs', 'transition' ],
        ]);
        $this->add_sifency_addons_master_typography_controls([
            'selector'        => '.sifency-nav-tooltip',
            'exclude'         => [ 'tabs', 'transition', 'stroke', 'shadow', 'alignment' ],
            'color_only' => true,
        ]);
        $this->add_Sifency_Addons_Button_Style_Controls([
            'selector'        => '.sifency-nav-link',
            'show_dimensions' => true,
            'hover_selector'  => 'li.sifency-one-page-nav-item.is-active .sifency-nav-link',
			'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
            'condition' => [
                'layout_type' => 'icons',],
        ]);
        $this->add_sifency_addons_icon_style_controls([
            'selector'        => '.sifency-nav-icon',
            'hover_selector'  => 'li.sifency-one-page-nav-item.is-active .sifency-nav-link .sifency-nav-icon',
			'hover_tab_label' => esc_html__( 'Active', 'sifency-addons' ),
            'condition' => [
                'layout_type' => 'icons',],
        ]);
        $this->add_sifency_addons_custom_css_controls();
    }
    protected function render_dom( array $context ) {
        $this->sifency_addons_render_custom_css();
        $wrapper_id = esc_attr( $context['dom_id'] );
        $settings = $this->get_settings_for_display();
        $items    = $settings['menu_items'];
        if ( empty( $items ) ) {
            return;
        }
        $snap_enabled = ( $settings['enable_snap'] === 'yes' );
        if ( $snap_enabled ) : 
            $offset = $settings['header_offset']['size'] ?? 0;
            ?>
            <style>
                html {
                    scroll-snap-type: y mandatory;
                    scroll-padding-top: <?php echo esc_attr( $offset ); ?>px;
                    scroll-behavior: smooth;
                }
                <?php foreach ( $items as $item ) : if ( empty( $item['section_id'] ) ) continue; ?>
                    #<?php echo esc_attr( $item['section_id'] ); ?> {
                        scroll-snap-align: start;
                        scroll-snap-stop: always;
                        min-height: 100vh;
                    }
                <?php endforeach; ?>
            </style>
        <?php endif; ?>
        <nav id="<?php echo esc_attr ( $wrapper_id ); ?>" class="sifency-one-page-nav-container pos-<?php echo esc_attr( $settings['nav_position'] ); ?> layout-<?php echo esc_attr( $settings['layout_type'] ); ?> <?php echo $snap_enabled ? 'sifency-snap-enabled' : ''; ?>"
            aria-label="One Page Navigation"
            data-snap="<?php echo $snap_enabled ? 'true' : 'false'; ?>" >
            <ul class="sifency-one-page-nav-list" role="list">
                <?php foreach ( $items as $index => $item ) : if ( empty( $item['section_id'] ) ) continue; ?>
                    <li class="sifency-one-page-nav-item <?php echo $index === 0 ? 'is-active' : ''; ?>"
                        data-section="<?php echo esc_attr( $item['section_id'] ); ?>"
                        role="listitem" >
                        <button 
                            type="button"
                            class="sifency-nav-link"
                            aria-label="<?php echo esc_attr( $item['menu_label'] ); ?>"
                        >
                            <span class="sifency-nav-icon" aria-hidden="true">
                                <?php if ( $settings['layout_type'] === 'icons' && ! empty( $item['menu_icon']['value'] ) ) : ?>
                                    <?php \Elementor\Icons_Manager::render_icon( $item['menu_icon'] ); ?>
                                <?php else : ?>
                                    <span class="dot-inner"></span>
                                <?php endif; ?>
                            </span>

                            <?php if ( $settings['show_tooltip'] === 'yes' ) : ?>
                                <span class="sifency-nav-tooltip">
                                    <?php echo esc_html( $item['menu_label'] ); ?>
                                </span>
                            <?php endif; ?>
                        </button>
                    </li>
                <?php endforeach; ?>
            </ul>
        </nav>
        <?php
}}