<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class Sifency_Universal_Menu_Widget extends \Elementor\Widget_Base {

	public function get_name() {
		return 'sifency_universal_menu';
	}

	public function get_title() {
		return esc_html__( 'Nav Menu', 'sifency-addons' );
	}

	public function get_icon() {
		return 'eicon-nav-menu';
	}

	public function get_categories() {
		return [ 'general' ];
	}

	public function get_style_depends() {
		return [ 'sifency-nav-menu-style' ];
	}

	public function get_script_depends() {
		return [ 'sifency-nav-menu-script' ];
	}

	protected function register_controls() {
		$this->start_controls_section(
			'section_layout',
			[
				'label' => esc_html__( 'Layout & Menu', 'sifency-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
			]
		);

		$menus = $this->get_available_menus();
		if ( ! empty( $menus ) ) {
			$this->add_control(
				'menu_slug',
				[
					'label' => esc_html__( 'Select Menu', 'sifency-addons' ),
					'type' => \Elementor\Controls_Manager::SELECT,
					'options' => $menus,
					'default' => array_keys( $menus )[0],
					'save_default' => true,
				]
			);
		} else {
			$this->add_control(
				'menu_error',
				[ 'type' => \Elementor\Controls_Manager::RAW_HTML, 'raw' => '<strong>No menus found.</strong>' ]
			);
		}

		$this->add_control(
			'mobile_breakpoint',
			[
				'label' => esc_html__( 'Mobile Breakpoint', 'sifency-addons' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'default' => '768',
				'options' => [
					'1024' => 'Tablet (1024px)',
					'768' => 'Mobile (768px)',
					'480' => 'Mobile Small (480px)',
				],
			]
		);

		$this->add_responsive_control(
			'alignment',
			[
				'label' => esc_html__( 'Alignment', 'sifency-addons' ),
				'type' => \Elementor\Controls_Manager::CHOOSE,
				'options' => [
					'flex-start' => [ 'title' => 'Left', 'icon' => 'eicon-text-align-left' ],
					'center' => [ 'title' => 'Center', 'icon' => 'eicon-text-align-center' ],
					'flex-end' => [ 'title' => 'Right', 'icon' => 'eicon-text-align-right' ],
				],
				'default' => 'flex-start',
				'selectors' => [
					'{{WRAPPER}} .sifency-nav-container' => 'justify-content: {{VALUE}};',
				],
			]
		);

		$this->end_controls_section();

		// --- SECTION: DESKTOP STYLE ---
		$this->start_controls_section(
			'section_style_desktop',
			[
				'label' => esc_html__( 'Desktop Menu', 'sifency-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'desktop_typography',
				'selector' => '{{WRAPPER}} .sifency-nav-menu > li > a',
			]
		);

		$this->start_controls_tabs( 'tabs_desktop_style' );

		// Normal
		$this->start_controls_tab( 'tab_desktop_normal', [ 'label' => 'Normal' ] );
		$this->add_control(
			'desktop_color',
			[
				'label' => 'Text Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'default' => '#333',
				'selectors' => [ '{{WRAPPER}} .sifency-nav-menu > li > a' => 'color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();

		// Hover
		$this->start_controls_tab( 'tab_desktop_hover', [ 'label' => 'Hover' ] );
		$this->add_control(
			'desktop_color_hover',
			[
				'label' => 'Text Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'default' => '#0073e6',
				'selectors' => [ '{{WRAPPER}} .sifency-nav-menu > li > a:hover' => 'color: {{VALUE}};' ],
			]
		);
		$this->end_controls_tab();
		$this->end_controls_tabs();

		$this->add_responsive_control(
			'desktop_spacing',
			[
				'label' => 'Item Spacing',
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'max' => 100 ] ],
				'default' => [ 'size' => 20 ],
				'selectors' => [
					'{{WRAPPER}} .sifency-nav-menu' => 'gap: {{SIZE}}{{UNIT}};',
				],
				'separator' => 'before',
			]
		);

		$this->end_controls_section();

		// --- SECTION: MOBILE OVERLAY STYLE ---
		$this->start_controls_section(
			'section_style_mobile_overlay',
			[
				'label' => esc_html__( 'Mobile Full Screen', 'sifency-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'overlay_bg_color',
			[
				'label' => 'Overlay Background',
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .sifency-nav-menu.is-active' => 'background-color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'overlay_backdrop_filter',
			[
				'label' => 'Glassmorphism Blur (px)',
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [ 'px' => [ 'max' => 20 ] ],
				'selectors' => [
					'{{WRAPPER}} .sifency-nav-menu.is-active' => 'backdrop-filter: blur({{SIZE}}px); -webkit-backdrop-filter: blur({{SIZE}}px);',
				],
			]
		);

		$this->add_group_control(
			\Elementor\Group_Control_Typography::get_type(),
			[
				'name' => 'mobile_typography',
				'label' => 'Mobile Typography',
				'selector' => '{{WRAPPER}}.sifency-mobile-active .sifency-nav-menu > li > a',
			]
		);

		$this->add_control(
			'mobile_item_color',
			[
				'label' => 'Mobile Item Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'default' => '#333',
				'selectors' => [
					'{{WRAPPER}}.sifency-mobile-active .sifency-nav-menu > li > a' => 'color: {{VALUE}};',
				],
			]
		);

		$this->add_control(
			'close_btn_color',
			[
				'label' => 'Close Button Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .sifency-mobile-close-btn i' => 'color: {{VALUE}};',
				],
				'separator' => 'before',
			]
		);

		$this->end_controls_section();

		// --- SECTION: TOGGLE BUTTON ---
		$this->start_controls_section(
			'section_style_toggle',
			[
				'label' => esc_html__( 'Hamburger Toggle', 'sifency-addons' ),
				'tab' => \Elementor\Controls_Manager::TAB_STYLE,
			]
		);

		$this->add_control(
			'toggle_color',
			[
				'label' => 'Icon Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-menu-toggle i' => 'color: {{VALUE}};' ],
			]
		);

		$this->add_control(
			'toggle_bg_color',
			[
				'label' => 'Background Color',
				'type' => \Elementor\Controls_Manager::COLOR,
				'selectors' => [ '{{WRAPPER}} .sifency-menu-toggle' => 'background-color: {{VALUE}}; padding: 10px; border-radius: 5px;' ],
			]
		);

		$this->add_control(
			'toggle_size',
			[
				'label' => 'Size',
				'type' => \Elementor\Controls_Manager::SLIDER,
				'selectors' => [ '{{WRAPPER}} .sifency-menu-toggle i' => 'font-size: {{SIZE}}{{UNIT}};' ],
			]
		);

		$this->end_controls_section();
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		$menu_slug = $settings['menu_slug'];
		$breakpoint = $settings['mobile_breakpoint'];

		if ( ! $menu_slug ) {
			return;
		}

		$breakpoint_class = 'sifency-bp-' . $breakpoint;

		?>
		<div class="sifency-nav-wrapper <?php echo esc_attr( $breakpoint_class ); ?>" data-breakpoint="<?php echo esc_attr( $breakpoint ); ?>">
			<div class="sifency-nav-container">
				
				<!-- Mobile Toggle -->
				<div class="sifency-menu-toggle">
					<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
						<path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" />
					</svg>
				</div>

				<!-- Menu Container -->
				<div class="sifency-menu-inner">
					<?php
					wp_nav_menu( [
						'menu' => $menu_slug,
						'container' => false,
						'menu_class' => 'sifency-nav-menu',
						'fallback_cb' => '__return_empty_string',
						'add_li_class'  => 'sifency-menu-item',
						'items_wrap' => '<ul id="%1$s" class="%2$s"><div class="sifency-mobile-close-btn">
						<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-6">
						<path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12" />
						</svg>
						</div>%3$s</ul>', 
					] );
					?>
				</div>
			</div>
		</div>
		<?php
	}
	
	private function get_available_menus() {
		$menus = wp_get_nav_menus();
		$options = [];
		foreach ( $menus as $menu ) {
			$options[ $menu->slug ] = $menu->name;
		}
		return $options;
	}
}