<?php
class ControllerCommonHeaderTop extends Controller
{
    protected function index()
    {
        $language = $this->language->load('common/header_top');
        $this->data = array_merge($this->data, $language);

        $this->loadScript();

        $this->load->type('tipocomum');
        $this->load->helper('ftr_geral');

        $this->data['url_home'] = $this->url->link('common/home', '', 'NONSSL', 'false');
        $this->data['url_log_in_or_register'] = $this->url->link('account/register', '', 'SSL');
        $this->data['url_account'] = $this->url->link('account/account', '', 'SSL');
        $this->data['url_order'] = $this->url->link('account/order', '', 'SSL');
        $this->data['url_log_out'] = $this->url->link('account/logout', '', 'SSL');
        $this->data['url_wish_list'] = $this->url->link('account/wishlist', '', 'SSL');

        $this->data['has_wish_list'] = $this->config->get('wg24_futura_status_lista_desejos') == '' || $this->config->get('wg24_futura_status_lista_desejos') == Type_Status::Tp_Habilitado;

        $this->data['count_wish_list'] = isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0;

        $this->data['is_logged_in']   = $this->customer->isLogged();
        $this->data['user_logged_in'] = substr($this->customer->getFirstName(), 0, 10);

        $this->data['points'] = $this->customer->getPoints();
        $this->data['url_resgatar_pontos'] = $this->url->link('fidelity/category', '', 'SSL');
        $this->data['url_extractlist'] = $this->url->link('fidelity/account/extractlist', '', 'SSL');

        $this->data['notification_background'] = $this->config->get('wg24_link_color_stick');
        $this->data['notification_color'] = $this->config->get('wg24_header_bg_color');

        $this->data['icon_cart'] = $this->config->get('icon_cart');
        $this->data['header_logo_position'] = $this->config->get('header_logo_position');

        $this->children = array(
            'module/cart',
        );

        $this->header();
        $this->schoolListLink();
        $this->giftListLink();
        $this->comparisonListLink();
        $this->customLinks();

        $this->language->load('module/cart');

        $countProducts = $this->cart->countProducts();
        $this->data['qtd_items'] = $countProducts;

        $config_telephone = $this->config->get('config_telephone');
        $telephones = array();

        if ($config_telephone) {
            foreach (array_reverse($config_telephone) as $telefone) {
                if (isset($telefone['display_header']) && $telefone['display_header']) {
                    $telephones[] = [
                        'type' => $telefone['type'],
                        'number' => $telefone['numero'],
                    ];
                }
            }
        }
        $this->data['config_telephone'] = $telephones;
        $this->data['Type_Phone'] = $this->load->typeForTwig('Type_Phone');
        $this->data['Type_HeaderLogoPosition'] = $this->load->typeForTwig('Type_HeaderLogoPosition');

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

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

    protected function schoolListLink()
    {
        $this->data['main_menu_exibe_listaescolar'] = $this->config->get('listaescolar_status') && $this->config->get('listaescolar_exibe_menu');
        $this->data['main_menu_link_listaescolar'] = $this->urlloja->link('listaescolar/listaescolar');
    }

    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 header()
    {
        $this->data['name'] = $this->config->get('config_name');

        // Logo
        if ($this->config->get('config_logo') && file_exists(DIR_IMAGE . $this->config->get('config_logo'))) {
            $logo_original = getimagesize(DIR_IMAGE . $this->config->get('config_logo'));
            $this->data['logo_original_width'] = $logo_original[0];
            $this->data['logo_original_height'] = $logo_original[1];
            $this->data['logo'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo'), 200, true);
        } else {
            $this->data['logo'] = '';
        }

        // Logo Responsivo
        if ($this->config->get('config_logo_responsivo') && file_exists(DIR_IMAGE . $this->config->get('config_logo_responsivo'))) {
            $this->data['logo_responsivo'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo_responsivo'), 140, true);
        } elseif ($this->data['logo']) {
            $this->data['logo_responsivo'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo'), 140, true);
        } else {
            $this->data['logo_responsivo'] = '';
        }

        // Logo Flutuante
        if ($this->config->get('config_logo_floating') && file_exists(DIR_IMAGE . $this->config->get('config_logo_floating'))) {
            $this->data['logo_floating'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo_floating'), 40, true);
        } elseif ($this->data['logo_responsivo']) {
            $this->data['logo_floating'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo_responsivo'), 40, true);
        } elseif ($this->data['logo']) {
            $this->data['logo_floating'] = $this->dmodel_tool_image->proportionatelyResize($this->config->get('config_logo'), 40, true);
        } else {
            $this->data['logo_floating'] = '';
        }
    }

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