<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; }
use Elementor\Widget_Base;
use Sifency\Traits\Sifency_Addons_Master_Typography;
use Sifency\Traits\Sifency_Addons_Icon_Style_Controls;
use Sifency\Traits\Sifency_Addons_Item_Style_Controls;
use Sifency\Traits\Sifency_Addons_Flex_Controls;
use Sifency\Traits\Sifency_Addons_Help_Controls;
use Sifency\Traits\Sifency_Addons_Label_Content_Controls;

class Sifency_Universal_Menu_Widget extends Widget_Base {
	use Sifency_Addons_Master_Typography;
	use Sifency_Addons_Icon_Style_Controls;
	use Sifency_Addons_Item_Style_Controls;
	use Sifency_Addons_Flex_Controls;
	use Sifency_Addons_Label_Content_Controls;
	use Sifency_Addons_Help_Controls;

	public function __construct( $data = array(), $args = null ) {
		parent::__construct( $data, $args );
		$this->wg_config = array(
			'name'           => 'sifency_universal_menu',
			'title'          => 'Nav Menu',
			'icon'           => 'eicon-nav-menu',
			'category'       => array( 'sifency-addons-widgets' ),
			'style_depends'  => array( 'sifency-nav-menu-style', 'sifency-typo-style' ),
			'script_depends' => array( 'sifency-nav-menu-script' ),
		);}

	protected function register_controls() {
		$this->start_controls_section(
			'section_layout',
			array(
				'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',
				array(
					'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',
				array(
					'type' => \Elementor\Controls_Manager::RAW_HTML,
					'raw'  => '<strong>No menus found.</strong>',
				)
			);
		}

		$this->add_control(
			'mobile_breakpoint',
			array(
				'label'   => esc_html__( 'Mobile Breakpoint', 'sifency-addons' ),
				'type'    => \Elementor\Controls_Manager::SELECT,
				'default' => '768',
				'options' => array(
					'1024' => 'Tablet (1024px)',
					'768'  => 'Mobile (768px)',
					'480'  => 'Mobile Small (480px)',
					'none' => 'None',
				),
			)
		);
		$this->add_responsive_control(
			'alignment',
			array(
				'label'     => esc_html__( 'Alignment', 'sifency-addons' ),
				'type'      => \Elementor\Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Left',
						'icon'  => 'eicon-text-align-left',
					),
					'center'     => array(
						'title' => 'Center',
						'icon'  => 'eicon-text-align-center',
					),
					'flex-end'   => array(
						'title' => 'Right',
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .sifency-nav-container' => 'justify-content: {{VALUE}};',
				),
			)
		);
		$this->add_sifency_addons_flex_alignment_controls(
			array(
				'prefix'   => 'menu_item_flex_',
				'label'    => 'Menu Alignment',
				'selector' => '.sifency-nav-menu',
			)
		);
		$this->end_controls_section();
		$this->add_sifency_addons_label_content_controls();
		$this->add_sifency_addons_help_controls();
		// --- SECTION: DESKTOP STYLE ---
		$this->start_controls_section(
			'section_style_desktop',
			array(
				'label' => esc_html__( 'Desktop Menu', 'sifency-addons' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_responsive_control(
			'desktop_spacing',
			array(
				'label'     => 'Item Spacing',
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'max' => 100 ) ),
				'default'   => array( 'size' => 20 ),
				'selectors' => array(
					'{{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',
			array(
				'label' => esc_html__( 'Mobile Full Screen', 'sifency-addons' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'overlay_bg_color',
			array(
				'label'     => 'Overlay Background',
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .sifency-nav-menu.is-active' => 'background-color: {{VALUE}};',
				),
			)
		);
		$this->add_control(
			'overlay_backdrop_filter',
			array(
				'label'     => 'Glassmorphism Blur (px)',
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'range'     => array( 'px' => array( 'max' => 20 ) ),
				'selectors' => array(
					'{{WRAPPER}} .sifency-nav-menu.is-active' => 'backdrop-filter: blur({{SIZE}}px); -webkit-backdrop-filter: blur({{SIZE}}px);',
				),
			)
		);
		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_toggle',
			array(
				'label' => esc_html__( 'Hamburger Toggle', 'sifency-addons' ),
				'tab'   => \Elementor\Controls_Manager::TAB_STYLE,
			)
		);
		$this->add_control(
			'toggle_color',
			array(
				'label'     => 'Icon Color',
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-menu-toggle i' => 'color: {{VALUE}};' ),
			)
		);
		$this->add_control(
			'toggle_bg_color',
			array(
				'label'     => 'Background Color',
				'type'      => \Elementor\Controls_Manager::COLOR,
				'selectors' => array( '{{WRAPPER}} .sifency-menu-toggle' => 'background-color: {{VALUE}}; padding: 10px; border-radius: 5px;' ),
			)
		);
		$this->add_control(
			'toggle_size',
			array(
				'label'     => 'Size',
				'type'      => \Elementor\Controls_Manager::SLIDER,
				'selectors' => array( '{{WRAPPER}} .sifency-menu-toggle i' => 'font-size: {{SIZE}}{{UNIT}};' ),
			)
		);
		$this->end_controls_section();

		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'menu_style',
				'label'    => 'Menu Item',
				'selector' => '.menu-item a',
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'   => 'submenu_style',
				'label'    => 'Sub Menu Item',
				'selector' => 'ul.sub-menu li.menu-item a',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'submenu_item_style',
				'label'    => 'Submenu Wrapper',
				'selector' => '.menu-item-has-children .sub-menu',
			)
		);
		$this->add_sifency_addons_item_style_controls(
			array(
				'prefix'   => 'mobile_menu_wrapper_style',
				'label'    => 'Mobile Menu Wrapper',
				'selector' => 'ul#menu-demo-main.is-active.sifency-nav-menu',
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'   => 'main_toggle_style',
				'label'    => 'Mobile Toggle Icon',
				'selector' => '.sifency-menu-toggle i',
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'dropdown_toggle_style',
				'label'           => 'Dropdown Toggle Icon',
				'selector'        => 'span.menu-dropdown-toggle',
				'show_dimensions' => true,
			)
		);
		$this->add_sifency_addons_icon_style_controls(
			array(
				'prefix'          => 'close_btn_style',
				'label'           => 'Close Button',
				'selector'        => '.sifency-mobile-close-btn i',
				'show_dimensions' => true,
			)
		);
		$this->add_sifency_addons_master_typography_controls(
			array(
				'prefix'    => 'label_style',
				'label'     => esc_html__( 'Label Style', 'sifency-addons' ),
				'selector'  => '.sifency-label-style',
				'exclude'   => array( 'alignment', 'tabs', 'transition' ),
				'condition' => array( 'show_label' => 'yes' ),
			)
		);
	}

	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;
		if ( 'yes' === $settings['show_label'] && ! empty( $settings['label_text'] ) ) : ?>
			<span class="sifency-label-style">
				<?php echo esc_html( $settings['label_text'] ); ?>
			</span>
			<?php
		endif;
		?>
		
		<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">
					<i class="icon-align-right"></i>
				</div>

				<!-- Menu Container -->
				<div class="sifency-menu-inner">
					<?php
					wp_nav_menu(
						array(
							'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">
						<i class="icon-cancel"></i>
						</div>%3$s</ul>',
						)
					);
					?>
				</div>
			</div>
		</div>
		<?php
	}

	private function get_available_menus() {
		$menus   = wp_get_nav_menus();
		$options = array();
		foreach ( $menus as $menu ) {
			$options[ $menu->slug ] = $menu->name;
		}
		return $options;
	}
}
