<?php
class ControllerCommonHeader extends Controller {

    public function index() {
        // Analytics
        $this->load->model('extension/extension');

        $data = $this->load->language('common/header');

        $data['analytics'] = array();
        /* Theme upgrade additional start */
        $data['language_id'] = $this->config->get('config_language_id');
        $data['text_menu_brands'] = '';

        /* Theme upgrade additional end */

        $analytics = $this->model_extension_extension->getExtensions('analytics');

        foreach ($analytics as $analytic) {
            if ($this->config->get($analytic['code'] . '_status')) {
                $data['analytics'][] = $this->load->controller('extension/analytics/' . $analytic['code'], $this->config->get($analytic['code'] . '_status'));
            }
        }

        if ($this->request->server['HTTPS']) {
            $server = $this->config->get('config_ssl');
        } else {
            $server = $this->config->get('config_url');
        }

        if (is_file(DIR_IMAGE . $this->config->get('config_icon'))) {
            $this->document->addLink($server . 'image/' . $this->config->get('config_icon'), 'icon');
        }

        $data['title'] = $this->document->getTitle();
        $data['base'] = $server;
        //Deprecated
        $data['template_name'] = $this->config->get('theme_default_directory') ? $this->config->get('theme_default_directory') : $this->config->get('config_template');
        //Current
        $data['theme_directory'] = $this->config->get('theme_default_directory') ? $this->config->get('theme_default_directory') : $this->config->get('config_template');

        $data['description'] = $this->document->getDescription();
        $data['keywords'] = $this->document->getKeywords();
        $data['links'] = $this->document->getLinks();
        $data['styles'] = $this->document->getStyles();
        $data['scripts'] = $this->document->getScripts();
        $data['lang'] = $this->language->get('code');
        $data['direction'] = $this->language->get('direction');

        $data['name'] = $this->config->get('config_name');

        if (is_file(DIR_IMAGE . $this->config->get('config_logo'))) {
            $data['logo'] = $server . 'image/' . $this->config->get('config_logo');
        } else {
            $data['logo'] = '';
        }

        $data['text_home'] = $this->language->get('text_home');

        // Wishlist
        if ($this->customer->isLogged()) {
            $this->load->model('account/wishlist');

            $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), $this->model_account_wishlist->getTotalWishlist());
        } else {
            $data['text_wishlist'] = sprintf($this->language->get('text_wishlist'), (isset($this->session->data['wishlist']) ? count($this->session->data['wishlist']) : 0));
        }

        $data['text_logged'] = sprintf($this->language->get('text_logged'), $this->url->link('account/account', '', true), $this->customer->getFirstName(), $this->url->link('account/logout', '', true));

        $data['home'] = $this->url->link('common/home');
        $data['wishlist'] = $this->url->link('account/wishlist', '', true);
        $data['logged'] = $this->customer->isLogged();
        $data['account'] = $this->url->link('account/account', '', true);
        $data['register'] = $this->url->link('account/register', '', true);
        $data['login'] = $this->url->link('account/login', '', true);
        $data['order'] = $this->url->link('account/order', '', true);
        $data['transaction'] = $this->url->link('account/transaction', '', true);
        $data['download'] = $this->url->link('account/download', '', true);
        $data['logout'] = $this->url->link('account/logout', '', true);
        $data['shopping_cart'] = $this->url->link('checkout/cart');
        $data['checkout'] = $this->url->link('checkout/checkout', '', true);
        $data['contact'] = $this->url->link('information/contact');
        $data['email'] = $this->config->get('config_email');
        $data['telephone'] = $this->config->get('config_telephone');
        $data['product_catalog'] = $this->url->link('product/category', '', true);

        $data['cms_hide_top_eshop_links'] = $this->config->get('cms_hide_top_eshop_links');
        $data['cms_hide_top_search'] = $this->config->get('cms_hide_top_search');
        $data['cms_hide_top_cart'] = $this->config->get('cms_hide_top_cart');

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

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

        $data['categories'] = array();

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

        foreach ($categories as $category) {
            if ($category['top']) {
                // Level 2
                $children_data = array();

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

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

                    //prd($child);

                    $children_data[] = array(
                        'name' => $child['name'] . ($this->config->get('config_product_count') ? ' (' . $this->model_catalog_product->getTotalProducts($filter_data) . ')' : ''),
                        'href' => $this->url->link('product/category', 'path=' . $category['category_id'] . '_' . $child['category_id']),
                        'thumb' => $this->model_tool_image->resize($child['image'], 300, 300),
                    );
                }

                // Level 1
                $data['categories'][] = array(
                    'name'     => $category['name'],
                    'children' => $children_data,
                    'column'   => $category['column'] ? $category['column'] : 1,
                    'href'     => $this->url->link('product/category', 'path=' . $category['category_id']),
                    'thumb'    => $this->model_tool_image->resize($category['image'], 50, 50),
                );
            }
        }

        // prd($data['categories']);

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

        $data['informations'] = array();
        $informations = $this->model_catalog_information->getInformations(array(
            'top' => true ));
        foreach ($informations as $information) {
            $data['informations'][] = array(
                'information_id' => $information['information_id'],
                'title'          => $information['title'],
                'href'           => ($information['external_link'] ? $this->url->externalLink($information['external_link']) : $this->url->link('information/information', 'information_id=' . $information['information_id'])
                ) );
        }

        $data['language'] = $this->load->controller('common/language');
        $data['currency'] = $this->load->controller('common/currency');
        $data['search'] = $this->load->controller('common/search');
        $data['cart'] = $this->load->controller('common/cart');

        $class = '';

        if(!empty( $this->request->get['route'] ) && $this->request->get['route'] == 'product/product') {
            $class = 'product ';
        }

        // For page specific css
        if (isset($this->request->get['route'])) {
            if (isset($this->request->get['product_id'])) {
                $class .= 'product-' . $this->request->get['product_id'] . " ";
                $this->document->addOGMeta('property="og:type"', 'product');
            } elseif (isset($this->request->get['path'])) {
                $class .= 'category-' . $this->request->get['path'] . " ";
            } elseif (isset($this->request->get['manufacturer_id'])) {
                $class .= 'manufacturer-' . $this->request->get['manufacturer_id'] . " ";
            } elseif (isset($this->request->get['information_id'])) {
                $class .= 'information-' . $this->request->get['information_id'] . " ";
                $this->document->addOGMeta('property="og:type"', 'article');
            } else {
                $class .= '';
            }

            $data['class'] = $class . str_replace('/', '-', $this->request->get['route']);
        } else {
            $data['class'] = 'common-home';
            $this->document->addOGMeta('property="og:type"', 'website');
        }

        $data['logo_meta'] = str_replace(' ', '%20', $this->model_tool_image->resize($this->config->get('config_logo'), 300, 300));
        $data['ogmeta'] = $this->document->getOGMeta();

        $this->hook->getHook('header/index/after', $data);

        return $this->load->view('common/header', $data);
    }

}
