<?php
class PZY_Menu
{
    public static function getMenuItemChildren($menu_items, $menu_item_parent = 0)
    {
        return array_filter($menu_items, function ($item) use ($menu_item_parent) {
            return $item->menu_item_parent == $menu_item_parent;
        });
    }

    public static function getRecursiveMenuItemChildren($menu_items, $menu_item_parent = 0)
    {
        $direct_children = self::getMenuItemChildren($menu_items, $menu_item_parent);
        foreach ($direct_children as $child) {
            $child->children = self::getRecursiveMenuItemChildren($menu_items, $child->ID);
        }
        return $direct_children;
    }

    public static function getTreeModeMenuItems($menu_items)
    {
        $mainItems = self::getMenuItemChildren($menu_items, 0);
        if ($mainItems) {
            foreach ($mainItems as $mainItem) {
                $mainItem->children = self::getRecursiveMenuItemChildren($menu_items, $mainItem->ID);
            }
        }
        return $mainItems;
    }

    public static function echoSubmenu($list, $level = 1)
    {
        if (count($list) > 0) {
            echo "<ul>";
            foreach ($list as $item) {
                $has_children = count($item->children) > 0;
                $xfn = '';
                if(!empty($item->xfn)){
                    $xfn = "rel='{$item->xfn}'";
                }
                $list_class = $has_children ? 'menu-item-has-children' : '';
                echo "<li class='$list_class'>";
                echo "<a href='{$item->url}' $xfn>";
                echo "<span class='pzy-link-text'>{$item->title}</span>";
                if ($has_children) {
                    if(is_rtl()){
                        echo "<i data-feather='chevron-left' class='toggle-menu-item'></i>";
                    }else{
                        echo "<i data-feather='chevron-right' class='toggle-menu-item'></i>";
                    }
                }
                echo "</a>";
                self::echoSubmenu($item->children, $level + 1);
                echo "</li>";
            }
            echo "</ul>";
        }
    }

    public static function create_pzy_menu($menu_items)
    {

        echo '<nav class="main-desktop-menu">';
        echo '<ul>';

        $current_object_id = get_queried_object_id();
        $home_url = esc_url(home_url('/'));

        foreach ($menu_items as $menu_item) {
            $has_children = count($menu_item->children) > 0;
            $is_current = pzy_is_current_menu_item($menu_item, $current_object_id, $home_url);

            $xfn = '';
            if(!empty($menu_item->xfn)){
                $xfn = "rel='{$menu_item->xfn}'";
            }

            echo "<li class='" . implode(' ', $menu_item->classes) . "'>";
            echo "<a class='" . ($is_current ? 'pzy-active' : '') . "' href='{$menu_item->url}' $xfn>";

            $icon_name = get_post_meta($menu_item->ID, '_menu_item_icon', true);

            $icon_text = '';
            if ($icon_name) {
//                $icon_text = "<span class='iconsax' icon-name='$icon_name'></span>";
                $icon_text = "<i data-feather='$icon_name'></i>";
            }

            echo "<span class='pzy-link-text'>{$icon_text}{$menu_item->title}</span>";
            echo "</a>";
            if ($has_children) {
                if (in_array('pzy-mega-menu', $menu_item->classes)) {
                    $children = $menu_item->children;
                    $prefix = "pzy-mg-menu-" . rand(10000,99999) . '-';
                    ?>
                    <ul>
                        <?php
                        echo "<div class='pzy-mega-menu-items'>";
                        foreach ($children as $index=>$child) {
                            $mega_item_has_children = count($child->children) > 0;
                            echo "<div class='pzy-mega-menu-item' ". ($mega_item_has_children?"data-content='#{$prefix}{$index}'":'') .">";
                            
                            $xfn = '';
                            if(!empty($child->xfn)){
                                $xfn = "rel='{$child->xfn}'";
                            }
                            
                            echo "<a href='{$child->url}' $xfn>";

                            $image_id = get_post_meta($child->ID, '_mega_menu_item_image_id', true);
                            if ($image_id) {
                                $image_bg_color = get_post_meta($child->ID, '_mega_menu_item_bg_color', true);
                                $style = '';
                                if ($image_bg_color){
                                    $style = "background: $image_bg_color";
                                }
                                echo "<div class='pzy-mega-menu-item-img-box' " . ($style?"style='$style'":'') . ">";
                                echo wp_get_attachment_image($image_id);
                                echo "</div>";
                            }

                            echo "<span class='pzy-link-text'>{$child->title}</span>";
                            if ($mega_item_has_children){
                                if(is_rtl()){
                                    echo '<i class="toggle-menu-item" data-feather="chevron-left"></i>';
                                }else{
                                    echo '<i class="toggle-menu-item" data-feather="chevron-right"></i>';
                                }
                            }

                            echo "</a>";

                            echo "</div>";

                        }
                        echo "</div>";

                        echo "<div class='pzy-mega-menu-contents'>";
                        foreach ($children as $index=>$child) {
                            $mega_item_has_children = count($child->children) > 0;
                            if ($mega_item_has_children) {
                                echo "<div class='pzy-mega-menu-content' id='{$prefix}{$index}'>";
                                $cnt = 0;
                                foreach ($child->children as $child_1) {
                                    if ($cnt % 2 == 0) {
                                        echo "<div class='pzy-mega-menu-col'>";
                                    }
                                    $ul_mb = get_post_meta($child_1->ID, '_mega_menu_content_ul_mb', true);
                                    $style = '';
                                    if (isset($ul_mb) && is_numeric($ul_mb)) {
                                        $style = "margin-bottom:{$ul_mb}px";
                                    }
                                    echo "<div class='pzy-mega-menu-content-list' style='$style'>";

                                    $heading_mb = get_post_meta($child_1->ID, '_mega_menu_content_heading_mb', true);
                                    $style = '';
                                    if (isset($heading_mb) && is_numeric($heading_mb)) {
                                        $style = "margin-bottom:{$heading_mb}px";
                                    }

                                    echo "<div class='pzy-mega-menu-content-list-item pzy-mega-menu-content-head-list-item' style='$style'>";
                                    
                                    $xfn = '';
                                    if(!empty($child_1->xfn)){
                                        $xfn = "rel='{$child_1->xfn}'";
                                    }
                                    
                                    echo "<a href='{$child_1->url}' $xfn>";
                                    echo "<span class='pzy-link-text'>{$child_1->title}</span>";
                                    echo "</a>";
                                    echo "</div>";

                                    if (count($child_1->children) > 0) {
                                        $children_space = get_post_meta($child_1->ID, '_mega_menu_content_children_space', true);
                                        $style = '';
                                        if (isset($children_space) && is_numeric($children_space)) {
                                            $style = "margin-bottom:{$children_space}px";
                                        }
                                        foreach ($child_1->children as $child_2) {
                                            echo "<div class='pzy-mega-menu-content-list-item' style='$style'>";

                                            $xfn = '';
                                            if(!empty($child_2->xfn)){
                                                $xfn = "rel='{$child_2->xfn}'";
                                            }

                                            echo "<a href='{$child_2->url}' $xfn>";
                                            echo "<span class='pzy-link-text'>{$child_2->title}</span>";
                                            echo "</a>";
                                            echo "</div>";
                                        }
                                    }

                                    echo "</div>";
                                    if ($cnt % 2 == 1 || ($cnt +1) == count($child->children)) {
                                        echo "</div>";
                                    }
                                    $cnt++;
                                }
                                echo "</div>";
                            }
                        }
                        echo "</div>";

                        ?>
                    </ul>
                    <?php
                } else {
                    self::echoSubmenu($menu_item->children);
                }
            }
            echo "</li>";
        }

        echo '</ul>';
        echo '</nav>';


    }

    public static function echoMobileSubmenu($list, $level = 1)
    {
        if ($level == 1)
            echo "<div class='mobile-categories-list'>";
        echo '<ul>';
        foreach ($list as $child) {
            $has_children = count($child->children) > 0;
            $list_class = $has_children ? 'menu-item-has-children' : '';

            $xfn = '';
            if(!empty($child->xfn)){
                $xfn = "rel='{$child->xfn}'";
            }

            echo "<li class='$list_class'>";
            echo "<a href='{$child->url}' $xfn>";
            echo "<span class='pzy-link-text'>{$child->title}</span>";
//            echo '<i class="iconsax toggle-menu-item" icon-name="chevron-left"></i>';
            if(is_rtl()){
                echo '<i class="toggle-menu-item" data-feather="chevron-left"></i>';
            }else{
                echo '<i class="toggle-menu-item" data-feather="chevron-right"></i>';
            }
            echo "</a>";
            if ($has_children) {
                self::echoMobileSubmenu($child->children, $level + 1);
            }
            echo "</li>";
        }
        echo '</ul>';
        if ($level == 1)
            echo "</div>";
    }

    public static function create_pzy_mobile_menu($menu_items)
    {
        $end_items = [];
        echo "<ul class='mobile-menu-list'>";
        foreach ($menu_items as $menu_item) {
            $submenu_title = get_post_meta($menu_item->ID, '_menu_item_submenu_title', true);
            $show_end = !empty($submenu_title) && count($menu_item->children) > 0;
            if($show_end){
                $end_items[]=[
                    'title'=>$submenu_title,
                    'submenu'=>$menu_item
                ];
            }
            echo "<li ".(count($menu_item->children) > 0?'class="menu-item-has-children"':'').">";

            $xfn = '';
            if(!empty($menu_item->xfn)){
                $xfn = "rel='{$menu_item->xfn}'";
            }
            
            echo "<a href='{$menu_item->url}' $xfn>";

            $icon_name = get_post_meta($menu_item->ID, '_menu_item_icon', true);
            $icon_text = '';
            if ($icon_name) {
//                $icon_text = "<span class='iconsax' icon-name='$icon_name'></span>";
                $icon_text = "<i data-feather='$icon_name'></i>";
            }

            echo "<span class='pzy-link-text'>{$icon_text}{$menu_item->title}</span>";

            if(!$show_end && count($menu_item->children) > 0){
                if(is_rtl()){
                    echo '<i class="toggle-menu-item" data-feather="chevron-left"></i>';
                }else{
                    echo '<i class="toggle-menu-item" data-feather="chevron-right"></i>';
                }
            }

            echo "</a>";

            if(!$show_end && count($menu_item->children) > 0){
                self::echoMobileSubmenu($menu_item->children);
            }

            echo "</li>";
        }
        echo "</ul>";

        foreach ($end_items as $item) {
            echo '<p class="categories-list-title">' . $item['title'] . '</p>';
            self::echoMobileSubmenu($item['submenu']->children);
        }
    }
}