<?php
class ControllerCommonNav extends Controller
{
    protected function index()
    {
        $this->loadLanguage();
        $this->loadScript();

        $this->giftListLink();
        $this->comparisonListLink();
        $this->homePageLink();
        $this->customLinks();

        
        $this->data['menu_departamentos'] = $this->config->get('wg24_main_menu_departamentos');
        $this->data['text_menu_departamento']
            = $this->config->get('wg24_main_menu_departamentos_text')
            ? $this->config->get('wg24_main_menu_departamentos_text')
            : $this->language->get('text_departamentos');

        // Menu
        $this->data['wg24_exibe_imagens_categorias_menu'] = $this->config->get('wg24_exibe_imagens_categorias_menu');
        $this->data['wg24_submenu_text'] = $this->config->get('wg24_submenu_text');
        $this->data['wg24_submenu_width'] = $this->config->get('wg24_submenu_width');

        $this->load->model('catalog/category');
        $this->load->model('catalog/product');

        $this->data['menu_position_full'] = $this->config->get('menu_position') == Type_MenuPosition::Full;
        $this->data['menu_divider'] = $this->config->get('menu_divider') == Type_StatusEn::Tp_Enable;

        $this->data['menu_selector'] = $this->config->get('menu_selector') == Type_StatusEn::Tp_Enable;
        $this->data['menu_selector_indicator'] = $this->config->get('menu_selector_indicator') == Type_SelectionIndicators::Underline;
        $this->data['menu_style_subcategory'] = $this->config->get('menu_style_subcategory') == Type_MenuSubCategory::Topic;
        $this->data['menu_selector_indicator_color'] = $this->config->get('wg24_menum_lcolor');
        $this->data['menu_typography'] = $this->config->get('menu_typography');

        $this->data['wg24_l_show_custom_htmlm'] = $this->config->get('wg24_l_show_custom_htmlm');
        $this->data['wg24_custom_item_menu'] = $this->config->get('wg24_custom_item_menu');

        $this->data['data'] = [
            'configurations' => $this->config->get('menu_typography')
        ];

        $default_lang = $this->config->get('config_language_id');
        $menu_custome_con = $this->config->get('wg24_l_custom_bmenu');

        $this->data['menu_custome_con'] = html_entity_decode($menu_custome_con[$default_lang]);
        $this->data['subchildren'] = false;
        $this->data['categories'] = array();
        $this->data['departamentos'] = array();

        $categories = $this->model_catalog_category->getCategories(0);

        foreach ($categories as $category) {
            // Level 2
            $children_data = array();

            $children = $this->model_catalog_category->getCategories($category['category_id']);

            foreach ($children as $child) {
                $data = array(
                    'filter_category_id' => $child['category_id'],
                    'filter_sub_category' => true,
                );

                $subchildren_data = array();

                $subchildren = $this->model_catalog_category->getCategories($child['category_id']);
                //level 3
                foreach ($subchildren as $subchild) {
                    if ($subchild['top']) {
                        $subchildren_data[] = array(
                            'name' => $subchild['name'],
                            'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id'] . '_' . $subchild['category_id']),
                        );
                        $this->data['subchildren'] = true;
                    }
                }

                if ($child['top']) {
                    $children_data[] = array(
                        'name' => $child['name'],
                        'subchildren' => $subchildren_data,
                        'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
                    );
                }
            }

            $name = $category['name'];
            $href = $this->url->link('product/category', 'path=' . $category['category_id']);
            $image = false;
            if (isset($category['image'])) {
                $image = $this->dmodel_tool_image->resize(
                    $category['image'],
                    $this->config->get('wg24_largura_imagens_categorias') ? $this->config->get('wg24_largura_imagens_categorias') : 16,
                    $this->config->get('wg24_altura_imagens_categorias') ? $this->config->get('wg24_altura_imagens_categorias') : 16
                );
            }

            $menuItem = [
                'name' => $name,
                'href' => $href,
                'image' => $image,
                'children' => $children_data,
            ];

            if ($category['top']) {
                /* Monta um array com as categorias que foram definidas no Futura Server para serem exibidas no menu principal */
                $this->data['categories'][] = $menuItem;
            }

            /* Monta um array com todas as categorias para serem exibidas em departamentos */
            $this->data['departamentos'][] = $menuItem;
        }

        $category_in_home = false;
        if (!isset($this->request->get['route'])) {
            $this->load->dmodel('design/layout');
            $layout_id = $this->dmodel_design_layout->getLayoutRoute('common/home');

            $category_modules = $this->config->get('category_module');
            if ($category_modules) {
                foreach ($category_modules as $category_module) {
                    if ($category_module['layout_id'] == $layout_id && $category_module['position'] == 'column_left' && $category_module['status']) {
                        $category_in_home = true;
                    }
                }
            }
        }

        /* Exibir o menu departamento no mobile se as categorias laterais da home
        estiver habilitado e não tiver nenhuma categoria no menu e o menu departamento estiver desabilitado */
        

        
        if (
            count($this->data['categories']) == 0 &&
            $this->config->get('wg24_main_menu_departamentos') == Type_StatusEn::Tp_Disable &&
            ($this->config->get('wg24_categories_sidebar_home') == Type_Status::Tp_Habilitado || $category_in_home)
        ) {
            $this->data['menu_departamentos'] = Type_StatusEn::Tp_Enable;
            $this->data['menu_departamentos_only_mobile'] = true;
        }


        $this->render('common/nav');
    }

    protected function loadLanguage()
    {
        $language = $this->language->load('common/nav');
        $this->data = array_merge($this->data, $language);
    }

    protected function loadScript()
    {
        $this->document->addScript('public/js/common/nav.js');
    }

    protected function giftListLink()
    {
        $this->data['url_gift_list'] = $this->url->link('listapresente/listapresente/buscalista', '', 'SSL');
        $this->data['has_gift_list'] = $this->config->get('listapresente_status');
    }

    protected function comparisonListLink()
    {
        $this->data['url_comparation_list'] = $this->url->link('product/compare', '', 'SSL');
        $this->data['has_comparation_list'] = $this->config->get('wg24_futura_status_lista_comparacao') == '' || $this->config->get('wg24_futura_status_lista_comparacao') == Type_Status::Tp_Habilitado;
        $this->data['count_comparation_list'] = isset($this->session->data['compare']) ? count($this->session->data['compare']) : 0;
    }

    protected function homePageLink()
    {
        $this->data['wg24_main_menu_link_home'] = $this->config->get('wg24_main_menu_link_home');
        $this->data['wg24_main_menu_link_home_display'] = $this->config->get('wg24_main_menu_link_home_display');
        $this->data['home'] = $this->url->link('common/home', '', 'NONSSL', 'false');
    }

    protected function customLinks()
    {
        $this->data['custom_links'] = $this->ftrgeralregistry->organizeCustomLinks(TypeCustomLinks::Nav);
    }
}
