<?php

namespace Elementor;

use My_Custom_Walker_Nav_Menu;

class BantaMenu extends Widget_Base
{

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

    public function get_title()
    {
        return __('Header Nav', TEXT_DOMAIN);
    }

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

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

    public function get_keywords()
    {
        return ['menu', 'link', 'items','header','menu','header','nav'];
    }

    protected function _register_controls()
    {


        $this->start_controls_section(
            'section_title',
            [
                'label' => esc_html__( 'Content', TEXT_DOMAIN ),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $menus = get_terms( 'nav_menu' );
        $menus = array_combine( wp_list_pluck( $menus, 'term_id' ), wp_list_pluck( $menus, 'name' ) );
        $defaultMenu = null;

        if (is_array($menus)){
            if (array_key_exists(0,$menus)){
                $defaultMenu = $menus[0][0];
            }
        }

        $this->add_control(
            'menu',
            [
                'type' => \Elementor\Controls_Manager::SELECT,
                'label' => esc_html__( 'Choose a nav', TEXT_DOMAIN ),
                'options' => $menus,
                'default' => $defaultMenu,
            ]
        );


        $this->add_control(
            'alignment',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__( 'alignment', TEXT_DOMAIN ),
                'options' => [
                    'text-left' => [
                        'title' => esc_html__( 'left', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-left',
                    ],
                    'text-center' => [
                        'title' => esc_html__( 'center', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-center',
                    ],
                    'text-right' => [
                        'title' => esc_html__( 'right', TEXT_DOMAIN ),
                        'icon' => 'eicon-text-align-right',
                    ],
                ],
                'default' => 'text-center',
            ]
        );

        $this->add_control(
            'direction',
            [
                'type' => \Elementor\Controls_Manager::CHOOSE,
                'label' => esc_html__( 'direction', TEXT_DOMAIN ),
                'options' => [
                    'dir-ltr' => [
                        'title' => esc_html__( 'left to right', TEXT_DOMAIN ),
                        'icon' => 'eicon-chevron-right',
                    ],
                    'dir-rtl' => [
                        'title' => esc_html__( 'right to left', TEXT_DOMAIN ),
                        'icon' => 'eicon-chevron-left',
                    ],
                ],
                'default' => 'dir-rtl',
            ]
        );


        $this->add_control(
            'mobile_logo',
            [
                'label' => esc_html__('Mobile Menu Logo Image', TEXT_DOMAIN),
                'type' => Controls_Manager::MEDIA,
                'default' => [
                    'url' => esc_url(get_template_directory_uri() . '/images/logo_white.webp'),
                ],
                'separator' => 'before'
            ]
        );

        $this->add_control(
            'show_mobile_btn',
            [
                'label'   => esc_html__( 'Show Mobile Menu Button', TEXT_DOMAIN ),
                'type'    => Controls_Manager::SWITCHER,
                'default' => 'yes',
            ]
        );

        $this->add_control(
            'mobile_btn_link',
            [
                'label' => esc_html__('Mobile Menu Button Link', TEXT_DOMAIN),
                'type' => \Elementor\Controls_Manager::URL,
                'placeholder' => esc_html__('https://your-link.com', TEXT_DOMAIN),
            ]
        );

        $this->end_controls_section();




        // styles
        $this->register_menu_styles_controls();
        $this->register_submenu_box_styles_controls();
        $this->register_submenu_styles_controls();


    }

    protected function register_menu_styles_controls()
    {
        $this->start_controls_section(
            'section_menu_style',
            [
                'label' => esc_html__('menus', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'menu_items_typography',
                'label' => esc_html__('typography', TEXT_DOMAIN),
                'selector' => '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a',
            ]
        );

        $this->start_controls_tabs(
            'style_menu_tabs'
        );
        $this->start_controls_tab(
            'menu_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );

        $this->add_responsive_control(
            'menu_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a' => 'background: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_before_color',
            [
                'label'     => esc_html__( 'second background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a:before' => 'background: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_text_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_icon_color',
            [
                'label'     => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li.menu-item-has-children > a:after' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a,{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}}!important;',
                ],
            ]
        );

        $this->add_control(
            'menu_padding',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->start_controls_tab(
            'menu_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );

        $this->add_control(
            'menu_bg_color_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li:hover > a' => 'background: {{VALUE}};',
                ],
            ]
        );


        $this->add_control(
            'menu_text_color_hover',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li:hover > a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_icon_color_hover',
            [
                'label'     => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li.menu-item-has-children:hover > a:after' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'menu_border_radius_hover',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li:hover > a,{{WRAPPER}} .banta-elementor-menu-container > div > ul > li > a:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'menu_padding_hover',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li:hover > a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );


        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();
    }

    protected function register_submenu_box_styles_controls()
    {
        $this->start_controls_section(
            'section_submenu_box_style',
            [
                'label' => esc_html__('submenu box', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'submenu_box_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu li ul' => 'background: {{VALUE}};',
                ],
            ]
        );


        $this->add_control(
            'submenu_box_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu li ul' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_box_padding',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu li ul' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );


        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name'      => 'submenu_box_shadow',
                'selector'  => '{{WRAPPER}} .banta-elementor-menu li ul',
            ]
        );



        $this->end_controls_section();
    }

    protected function register_submenu_styles_controls()
    {
        $this->start_controls_section(
            'section_submenu_style',
            [
                'label' => esc_html__('submenu items', TEXT_DOMAIN),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'submenu_items_typography',
                'label' => esc_html__('typography', TEXT_DOMAIN),
                'selector' => '{{WRAPPER}} .banta-elementor-menu-container > div > ul > li ul a',
            ]
        );


        $this->start_controls_tabs(
            'style_submenu_tabs'
        );
        $this->start_controls_tab(
            'submenu_normal_tab',
            [
                'label' => esc_html__( 'normal', TEXT_DOMAIN ),
            ]
        );

        $this->add_control(
            'submenu_bg_color',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a' => 'background: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_before_color',
            [
                'label'     => esc_html__( 'second background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a:before' => 'background: {{VALUE}}!important;',
                ],
            ]
        );

        $this->add_control(
            'submenu_text_color',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_icon_color',
            [
                'label'     => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li.menu-item-has-children > a:after' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_border_radius',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a,{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_padding',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->start_controls_tab(
            'submenu_hover_tab',
            [
                'label' => esc_html__( 'hover', TEXT_DOMAIN ),
            ]
        );

        $this->add_control(
            'submenu_bg_color_hover',
            [
                'label'     => esc_html__( 'background color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a' => 'background: {{VALUE}};',
                ],
            ]
        );


        $this->add_control(
            'submenu_text_color_hover',
            [
                'label'     => esc_html__( 'color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_icon_color_hover',
            [
                'label'     => esc_html__( 'icon color', TEXT_DOMAIN ),
                'type'      => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a:after' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_border_radius_hover',
            [
                'label'      => esc_html__( 'border radius', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a,{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a:before' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'submenu_padding_hover',
            [
                'label'      => esc_html__( 'padding', TEXT_DOMAIN ),
                'type'       => Controls_Manager::DIMENSIONS,
                'size_units' => [ 'px', '%' ],
                'selectors'  => [
                    '{{WRAPPER}} .banta-elementor-menu-container > div > ul ul li:hover a' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );


        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->end_controls_section();
    }


    protected function render()
    {
        $settings = $this->get_settings_for_display();
        $menu = $settings['menu'];
        $align = $settings['alignment'];
        $direction = $settings['direction'];
        $mobileLogoUrl = empty($settings['mobile_logo']['url']) ? '' : $settings['mobile_logo']['url'] ;
        $showMobileMenu = $settings['show_mobile_btn'] == 'yes';


        if (class_exists('WooCommerce')){
            $my_account_page_id  = wc_get_page_id( 'myaccount' );
            $my_account_page_url = $my_account_page_id ? get_permalink( $my_account_page_id ) : '';
        }
        $mobileBtnLink = empty($settings['mobile_btn_link']['url'] || !isset($my_account_page_url)) ? $my_account_page_url : $settings['mobile_btn_link']['url'] ;

        echo "<div class='banta-menu-backdrop'></div>";
        echo "<input type='hidden' id='mobileMenuLogoSrc' value='$mobileLogoUrl'>";
        echo "<input type='hidden' id='mobileMenuProfileBtnLink' value='$mobileBtnLink'>";
        echo "<input type='hidden' id='showMobileMenuProfileBtn' value='$showMobileMenu'>";
        echo "<div class='banta-elementor-menu $direction $align'>";
        echo "<div class='banta-elementor-menu-container'>";
        echo wp_nav_menu(array(
            'menu' => $menu,
            'walker' => new My_Custom_Walker_Nav_Menu(),
            'menu_class' => 'menu-items',
            'fallback_cb' => 'wp_page_menu'));
        echo "</div>";
        echo "</div>";
    }


}

Plugin::instance()->widgets_manager->register_widget_type(new BantaMenu());