<?php
/**
 * Elementor widget MenuAccordion.
 *
 * @package MadeByAura\Prime\Plugins\Elementor\Widgets
 * @author  MadeByAura.com
 */

namespace MadeByAura\Prime\Plugins\Elementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Typography;
use Elementor\Widget_Base;

defined( 'ABSPATH' ) || die();

/**
 * Elementor widget MenuAccordion .
 */
class MenuAccordion extends Widget_Base {
	/**
	 * Get the widget name.
	 *
	 * @return string Widget name.
	 */
	public function get_name() {
		return 'aura-theme-accordion-menu';
	}

	/**
	 * Get the widget title.
	 *
	 * @return string Widget title.
	 */
	public function get_title() {
		return __( 'Menu Accordion ', 'aura-prime' );
	}

	/**
	 * Get the list of keywords the widget belongs to.
	 *
	 * @return array Widget keywords.
	 */
	public function get_keywords() {
		return [ 'aura', 'theme', 'accordion', 'menu' ];
	}

	/**
	 * Get the widget icon.
	 *
	 * @return string Widget icon.
	 */
	public function get_icon() {
		return 'eicon-accordion';
	}

	/**
	 * Get the list of categories the widget belongs to.
	 *
	 * Used to determine where to display the widget in the editor.
	 *
	 * Note that currently Elementor supports only one category.
	 * When multiple categories passed, Elementor uses the first one.
	 *
	 * @return array Widget categories.
	 */
	public function get_categories() {
		return [ 'aura-theme' ];
	}

	/**
	 * Get the list of scripts the widget depended on.
	 *
	 * Used to set scripts dependencies required to run the widget.
	 *
	 * @return array Widget scripts dependencies.
	 */
	public function get_script_depends() {
		return [];
	}

	/**
	 * Register the widget controls.
	 *
	 * Adds different input fields to allow the user to change and customize the widget settings.
	 */
	protected function register_controls() {
		// Content section.
		$this->start_controls_section(
			'section_content',
			[
				'label' => __( 'Content', 'aura-prime' ),
			],
		);

		// Content > Menu.
		$this->add_control(
			'menu_id',
			[
				'label'   => __( 'Menu', 'aura-prime' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 0,
				'options' => get_terms(
					[
						'taxonomy' => 'nav_menu',
						'fields'   => 'id=>name',
					],
				),
			],
		);

		// END Content section.
		$this->end_controls_section();

		// Level 1 section.
		$this->start_controls_section(
			'menu_level_1_style',
			[
				'label' => __( 'Level 1', 'aura-prime' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			],
		);

		// Level 1 section > Text color.
		$this->add_control(
			'menu_level_1_text_color',
			[
				'label'     => __( 'Text color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-1-color: {{VALUE}}',
				],
			],
		);

		// Level 1 section > Typography.
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'menu_level_1_typography',
				'selector' => '{{WRAPPER}} .aura-nav-accordion .menu > li > a',
			],
		);

		// Level 1 section > Space between.
		$this->add_responsive_control(
			'menu_level_1_space_between',
			[
				'label'      => esc_html__( 'Space Between', 'aura-prime' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem', 'custom' ],
				'range'      => [
					'px'  => [
						'max' => 100,
					],
					'em'  => [
						'max' => 10,
					],
					'rem' => [
						'max' => 10,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-1-gap: {{SIZE}}{{UNIT}}',
				],
			],
		);

		// Level 1 section > Divider.
		$this->add_control(
			'menu_level_1_separator_panel_style',
			[
				'type'  => Controls_Manager::DIVIDER,
				'style' => 'thick',
			],
		);

		// Level 1 section > Submenu icon color.
		$this->add_control(
			'submenu_level_1_icon_color',
			[
				'label'     => __( 'Icon Color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-1-submenu-icon-color: {{VALUE}}',
				],
			],
		);

		// Level 1 section > Submenu icon size.
		$this->add_responsive_control(
			'submenu_level_1_icon_size',
			[
				'label'      => esc_html__( 'Icon Size', 'aura-prime' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
				'default'    => [
					'size' => 12,
				],
				'range'      => [
					'px' => [
						'min' => 6,
					],
					'%'  => [
						'min' => 6,
					],
					'vw' => [
						'min' => 6,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-1-submenu-icon-size: {{SIZE}}{{UNIT}};',
				],
			],
		);

		// Level 1 section > Submenu icon offset.
		$this->add_responsive_control(
			'submenu_level_1_icon_offset',
			[
				'type'      => Controls_Manager::SLIDER,
				'label'     => __( 'Icon offset', 'aura-prime' ),
				'range'     => [
					'px' => [
						'min'  => -25,
						'max'  => 25,
						'step' => 1,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-1-submenu-icon-offset: {{SIZE}}{{UNIT}};',
				],
			],
		);

		// END Level 1 section.
		$this->end_controls_section();

		// Level 2 section.
		$this->start_controls_section(
			'menu_level_2_style',
			[
				'label' => __( 'Level 2', 'aura-prime' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			],
		);

		// Level 2 section > Space between.
		$this->add_responsive_control(
			'menu_level_2_space_between',
			[
				'label'      => esc_html__( 'Space Between', 'aura-prime' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem', 'custom' ],
				'range'      => [
					'px'  => [
						'max' => 100,
					],
					'em'  => [
						'max' => 10,
					],
					'rem' => [
						'max' => 10,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-gap: {{SIZE}}{{UNIT}}',
				],
			],
		);

		// Level 2 section  > Background color.
		$this->add_control(
			'menu_level_2_background_color',
			[
				'label'     => __( 'Background color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-background-color: {{VALUE}}',
				],
			],
		);

		// Level 2 section > Text color.
		$this->add_control(
			'menu_level_2_text_color',
			[
				'label'     => __( 'Text color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-color: {{VALUE}}',
				],
			],
		);

		// Level 2 section > Typography.
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'menu_level_2_typography',
				'selector' => '{{WRAPPER}} .aura-nav-accordion .menu > li > ul > li > a',
			],
		);

		// Level 2 section > Padding.
		$this->add_responsive_control(
			'menu_level_2_padding',
			[
				'label'      => __( 'Padding', 'aura-prime' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion .menu > li > ul' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			],
		);

		// Level 2 section > Border.
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'menu_level_2_border',
				'selector' => '{{WRAPPER}} .aura-nav-accordion .menu > li > ul',
			],
		);

		// Level 2 section > Border radius.
		$this->add_responsive_control(
			'menu_level_2_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'aura-prime' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion .menu > li > ul' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			],
		);

		// Level 2 section > Divider.
		$this->add_control(
			'menu_level_2_separator_panel_style',
			[
				'type'  => Controls_Manager::DIVIDER,
				'style' => 'thick',
			],
		);

		// Level 2 section > Submenu icon color.
		$this->add_control(
			'submenu_level_2_icon_color',
			[
				'label'     => __( 'Icon Color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-submenu-icon-color: {{VALUE}}',
				],
			],
		);

		// Level 2 section > Submenu icon size.
		$this->add_responsive_control(
			'submenu_level_2_icon_size',
			[
				'label'      => esc_html__( 'Icon Size', 'aura-prime' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'em', 'rem', 'vw', 'custom' ],
				'default'    => [
					'size' => 12,
				],
				'range'      => [
					'px' => [
						'min' => 6,
					],
					'%'  => [
						'min' => 6,
					],
					'vw' => [
						'min' => 6,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-submenu-icon-size: {{SIZE}}{{UNIT}};',
				],
			],
		);

		// Level 2 section > Submenu icon offset.
		$this->add_responsive_control(
			'submenu_level_2_icon_offset',
			[
				'type'      => Controls_Manager::SLIDER,
				'label'     => __( 'Icon offset', 'aura-prime' ),
				'range'     => [
					'px' => [
						'min'  => -25,
						'max'  => 25,
						'step' => 1,
					],
				],
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-2-submenu-icon-offset: {{SIZE}}{{UNIT}};',
				],
			],
		);

		// END Level 2 section.
		$this->end_controls_section();

		// Level 3 section.
		$this->start_controls_section(
			'menu_level_3_style',
			[
				'label' => __( 'Level 3', 'aura-prime' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			],
		);

		// Level 3 section > Space between.
		$this->add_responsive_control(
			'menu_level_3_space_between',
			[
				'label'      => esc_html__( 'Space Between', 'aura-prime' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em', 'rem', 'custom' ],
				'range'      => [
					'px'  => [
						'max' => 100,
					],
					'em'  => [
						'max' => 10,
					],
					'rem' => [
						'max' => 10,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-3-gap: {{SIZE}}{{UNIT}}',
				],
			],
		);

		// Level 3 section  > Background color.
		$this->add_control(
			'menu_level_3_background_color',
			[
				'label'     => __( 'Background color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-3-background-color: {{VALUE}}',
				],
			],
		);

		// Level 3 section > Text color.
		$this->add_control(
			'menu_level_3_text_color',
			[
				'label'     => __( 'Text color', 'aura-prime' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .aura-nav-accordion' => '--aura-level-3-color: {{VALUE}}',
				],
			],
		);

		// Level 3 section > Typography.
		$this->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'menu_level_3_typography',
				'selector' => '{{WRAPPER}} .aura-nav-accordion .menu > li > ul > li > ul > li > a',
			],
		);

		// Level 3 section > Padding.
		$this->add_responsive_control(
			'menu_level_3_padding',
			[
				'label'      => __( 'Padding', 'aura-prime' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion .menu > li > ul > li > ul' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			],
		);

		// Level 3 section > Border.
		$this->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'menu_level_3_border',
				'selector' => '{{WRAPPER}} .aura-nav-accordion .menu > li > ul > li > ul',
			],
		);

		// Level 3 section > Border radius.
		$this->add_responsive_control(
			'menu_level_3_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'aura-prime' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em', 'rem', 'custom' ],
				'selectors'  => [
					'{{WRAPPER}} .aura-nav-accordion .menu > li > ul > li > ul' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			],
		);

		// END Level 3 section.
		$this->end_controls_section();
	}

	/**
	 * Render the widget output on the frontend.
	 *
	 * Written in PHP and used to generate the final HTML.
	 */
	protected function render() {
		$settings = $this->get_settings_for_display();

		// Do not proceed if there is no menu is selected.
		if ( ! $settings['menu_id'] ) {
			echo esc_html__( 'Select a menu.', 'aura-prime' );

			return;
		}

		wp_nav_menu(
			[
				'menu'            => $settings['menu_id'],
				'container'       => 'nav',
				'container_class' => 'aura-menu-modal__nav aura-nav-accordion',
				'depth'           => 3,
				'fallback_cb'     => false,
			],
		);
	}

	/**
	 * Render button widget output in the editor.
	 *
	 * Written as a Backbone JavaScript template and used to generate the live preview.
	 */
	protected function content_template() {}
}
