<?php

class ControllerCommonFooter extends Controller
{
    protected $dataJs = [];

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

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

    protected function loadModel()
    {
        $this->load->dmodel('tool/online');
        $this->load->dmodel('tool/image');
        $this->load->model('design/banner');
        $this->load->model('setting/extension');
        $this->load->model('catalog/information');

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

    protected function socialMediaUrl() {
        $this->data['wg24_facebook_url'] = $this->config->get('wg24_facebook_url');
        $this->data['wg24_twitter_url'] = $this->config->get('wg24_twitter_url');
        $this->data['wg24_google_url'] = $this->config->get('wg24_google_url');
        $this->data['wg24_rss_url'] = $this->config->get('wg24_rss_url');
        $this->data['wg24_pinterest_url'] = $this->config->get('wg24_pinterest_url');
        $this->data['wg24_linking_url'] = $this->config->get('wg24_linking_url');
        $this->data['wg24_youtub_url'] = $this->config->get('wg24_youtub_url');
        $this->data['wg24_instagram_url'] = $this->config->get('wg24_instagram_url');
        $this->data['wg24_blogger_url'] = $this->config->get('wg24_blogger_url');
    }

    protected function floatingContent() {
        $this->dataJs['backToTop'] = $this->config->get('fc_back_to_top');

        if (isset($this->dataJs['backToTop'])) {
            if ($this->dataJs['backToTop']['position'] == Type_Position::Tp_Right) {
                $this->dataJs['backToTop']['position'] = 'scroll-to-top-right';
            } else {
                $this->dataJs['backToTop']['position'] = 'scroll-to-top-left';
            }
        }

        $this->dataJs['whatsApp'] = $this->config->get('fc_whatsapp');
        if (isset($this->dataJs['whatsApp'])) {
            $this->dataJs['whatsApp']['url'] = 'https://wa.me/55' . preg_replace('/\D/', '', $this->dataJs['whatsApp']['number']);
            if (isset($this->dataJs['whatsApp']['position']) && $this->dataJs['whatsApp']['position'] == Type_Position::Tp_Right) {
                $this->dataJs['whatsApp']['position'] = 'footer-whatsapp-right';
            } else {
                $this->dataJs['whatsApp']['position'] = 'footer-whatsapp-left';
            }
        }
    }

    protected function cookiesPolicy() {
        $cookiesId = (int) $this->config->get('config_cookies_id');

        $this->dataJs['cookiesPolicy'] = [
            'id' => $cookiesId,
            'url' => $this->url->link('information/information', 'information_id=' . $cookiesId),
        ];
    }

    protected function eBitCertificate() {
        $this->data['certificate_ebit_status'] = $this->config->get('certificate_ebit_status');
        $this->data['certificate_ebit_store_id'] = $this->config->get('certificate_ebit_store_id');
        $this->data['certificate_ebit_lightbox'] = $this->config->get('certificate_ebit_lightbox');
    }

    public function index()
    {
        $this->loadLanguage();
        $this->loadScript();
        $this->loadModel();

        $this->socialMediaUrl();
        $this->floatingContent();
        $this->cookiesPolicy();
        $this->eBitCertificate();

        $this->data['year'] = date("Y");

        $this->data['exibe_endereco'] = $this->config->get('wg24_exibe_contact');
        $this->data['wg24_statuspagamento'] = $this->config->get('wg24_statuspagamento');
        $this->data['wg24_exibicao_pagamento'] = $this->config->get('wg24_exibicao_pagamento');
        $this->data['wg24_certificado_status'] = $this->config->get('wg24_certificado_status');
        $this->data['wg24_type_configuration_contact'] = $this->config->get('wg24_type_configuration_contact');

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

        $this->data['wg24_foot_first_colunm'] = $this->config->get('wg24_foot_first_colunm');
        $this->data['wg24_linkFabricantes'] = $this->config->get('wg24_linkFabricantes');

        $default_lang = $this->config->get('config_language_id');
        $footer_logo = $this->config->get('wg24_logo_image');
        $this->data['footer_logo'] = html_entity_decode($footer_logo[$default_lang]);

        $default_lang = $this->config->get('config_language_id');
        $footer_contact = $this->config->get('wg24_contact_info_page_of');
        if (is_array($footer_contact)) {
            $this->data['footer_contact'] = html_entity_decode($footer_contact[$default_lang], ENT_COMPAT, 'UTF-8');
        }

        $default_lang = $this->config->get('config_language_id');
        $copyright = $this->config->get('wg24_copyr_text');
        if (!empty($copyright[$default_lang])) {
            $this->data['copyright'] = html_entity_decode($copyright[$default_lang]);
        }


        $this->data['text_wg24_titulo_rodape'] = ($this->config->get('wg24_titulo_rodape')) ? $this->config->get('wg24_titulo_rodape') : $this->language->get('contact_titulo_rodape');
        $config_telephone = $this->config->get('config_telephone');
        $config_telephone_rodape = false;
        $config = array();
        if ($config_telephone) {
            foreach (array_reverse($config_telephone) as $telefone) {
                if (isset($telefone['display'])) {
                    $config[] = [
                        'type' => $telefone['type'],
                        'number' => $telefone['numero'],
                        'only_number' => ftr_geral::removeMaskToInt($telefone['numero']),
                    ];
                }
            }
        }

        $this->data['config_telephone'] = $config;
        $this->data['config_telephone_rodape'] = is_array($config_telephone_rodape) ? implode(' - ', $config_telephone_rodape) : false;
        $this->data['text_fax'] = $this->config->get('config_fax');
        $this->data['text_config_email'] = $this->config->get('config_email');
        $this->data['text_config_address'] = $this->config->get('config_address');
        $this->data['Type_Phone'] = $this->load->typeForTwig('Type_Phone');

        $this->getInformation();
        $this->getContact();

        $this->data['return'] = $this->url->link('account/return', '', 'SSL');
        $this->data['sitemap'] = $this->url->link('information/sitemap');
        $this->data['manufacturer'] = $this->url->link('product/manufacturer');
        $this->data['voucher'] = $this->url->link('account/voucher', '', 'SSL');
        $this->data['affiliate'] = $this->url->link('affiliate/account', '', 'SSL');
        $this->data['special'] = $this->url->link('product/special');
        $this->data['account'] = $this->url->link('account/account', '', 'SSL');
        $this->data['order'] = $this->url->link('account/order', '', 'SSL');
        $this->data['wishlist'] = $this->url->link('account/wishlist', '', 'SSL');
        $this->data['newsletter'] = $this->url->link('account/newsletter', '', 'SSL');

        $this->data['powered'] = sprintf($this->language->get('text_powered'), $this->config->get('config_name'), date('Y', time()));

        $this->data['tipo_rodape'] = ftr_geral::isType($this->config->get('wg24_tipo_rodape'), Type_AutoOrPersonalizado::getEnumList(), Type_AutoOrPersonalizado::Tp_Personalizado);
        if (!$this->config->get('wg24_tipo_rodape')) {
            $this->data['config_name'] = $this->config->get('config_name');
            $this->data['config_owner'] = $this->config->get('config_owner');
            $this->data['config_address'] = $this->config->get('config_address');
            $this->data['config_cnpj'] = $this->config->get('config_cnpj');
            $this->data['config_ie'] = $this->config->get('config_ie');
            $this->data['config_email'] = $this->config->get('config_email');
            $this->data['config_email'] = $this->config->get('config_email');
        }

        if (ftr_geral::isIsset($this->config->get('avise_me_status'))) {
            if ($this->config->get('config_stock_checkout')) {
                $this->data['aviseme_status'] = Type_Status::Tp_Desabilitado;
            } else {
                $this->data['aviseme_status'] = $this->config->get('avise_me_status');
            }
        } else {
            $this->data['aviseme_status'] = Type_Status::Tp_Desabilitado;
        }

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

        $this->app();

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

        $this->data['futura_desenvolvidopor'] = $this->config->get('futura_desenvolvidopor');
        $this->data['return_status'] = ftr_geral::isType($this->config->get('wg24_linkDevolucoes'), Type_Status::getEnumList(), Type_Status::Tp_Habilitado);


        if (isset($this->request->server['REMOTE_ADDR'])) {
            $ip = $this->request->server['REMOTE_ADDR'];
        } else {
            $ip = '';
        }

        if (isset($this->request->server['HTTP_HOST']) && isset($this->request->server['REQUEST_URI'])) {
            $url = 'http://' . $this->request->server['HTTP_HOST'] . $this->request->server['REQUEST_URI'];
        } else {
            $url = '';
        }

        if (isset($this->request->server['HTTP_REFERER'])) {
            $referer = $this->request->server['HTTP_REFERER'];
        } else {
            $referer = '';
        }

        $this->dmodel_tool_online->whosonline($ip, $this->customer->getId(), $url, $referer);

        $this->data['banners'] = array();
        $this->data['banners_certificado'] = array();

        if ($this->config->get('wg24_statuspagamento') == "enable") {
            $results = $this->model_design_banner->getBanner($this->config->get('wg24_banner_pagamento'));

            $this->data['wg24_bandeiras_automaticas'] = Ftr_Geral::isType($this->config->get('wg24_bandeiras_automaticas'), Type_DsimDnao::getEnumList(), Type_DsimDnao::Tp_Nao);

            if (!$this->data['wg24_bandeiras_automaticas']) {
                foreach ($results as $result) {
                    if (file_exists(DIR_IMAGE . $result['image'])) {
                        $this->data['banners'][] = array(
                            'title' => $result['title'],
                            'link' => $result['link'],
                            'image' => $this->dmodel_tool_image->resizeWebp($result['image'], $this->config->get('wg24_pagtolargura'), $this->config->get('wg24_pagtoaltura'))
                        );
                    }
                }
            } else {
                $bandeiras = array(
                    'master' => 'MasterCard',
                    'visa' => 'Visa',
                    'elo' => 'Elo',
                    'amex' => 'American Express',
                    'hipercard' => 'Hipercard',
                    'diners' => 'Diners',
                    'cabal' => 'Cabal',
                    'aura' => 'Aura',
                    'sorocred' => 'Sorocred',
                    'bradesco' => 'Banco Bradesco',
                    'brasil' => 'Banco do Brasil',
                    'itau' => 'Banco Itaú',
                    'banrisul' => 'Banco Banrisul',
                    'discover' => 'Discover',
                    'jcb' => 'JCB',
                    'boleto' => 'Boleto Bancário',
                    'hiper' => 'Hiper card',
                    'ml' => 'Mercado Livre',
                    'transferencia' => 'Transferencia Bancária',
                    'paypal' => 'PayPal'
                );
                // pagseguro
                $payment_method['pagseguro'] = array(
                    'master',
                    'visa',
                    'elo',
                    'amex',
                    'hipercard',
                    'diners',
                    'cabal',
                    'aura',
                    'sorocred',
                    'bradesco',
                    'brasil',
                    'itau',
                    'banrisul',
                    'boleto'
                );
                // pagseguro_api
                $payment_method['pagseguro_api'] = array(
                    'master',
                    'visa',
                    'elo',
                    'amex',
                    'hiper',
                    'hipercard',
                    'diners',
                    'boleto'
                );
                // eRede
                $payment_method['FuturaKomerci'] = array(
                    'master',
                    'visa',
                    'diners',
                    'hiper',
                    'hipercard',
                    'jcb'
                );

                // cielo_checkout
                $payment_method['cielo_transparente'] = array();
                if ($this->config->get('cielo_transparente_master')) array_push($payment_method['cielo_transparente'], 'master');
                if ($this->config->get('cielo_transparente_visa')) array_push($payment_method['cielo_transparente'], 'visa');
                if ($this->config->get('cielo_transparente_amex')) array_push($payment_method['cielo_transparente'], 'amex');
                if ($this->config->get('cielo_transparente_diners')) array_push($payment_method['cielo_transparente'], 'diners');
                if ($this->config->get('cielo_transparente_elo')) array_push($payment_method['cielo_transparente'], 'elo');
                if ($this->config->get('cielo_transparente_discover')) array_push($payment_method['cielo_transparente'], 'discover');

                //cielo api 3.0
                $payment_method['cielo_api_credito'] = array();
                $cielo_credit_cards = $this->config->get('cielo_api_credit_card');
                if($cielo_credit_cards){
                    foreach ($cielo_credit_cards as $cielo_credit_card){
                        if (strtolower($cielo_credit_card['descricao'])) {
                            $payment_method['cielo_api_credito'][] = strtolower($cielo_credit_card['descricao']);
                        }
                    }
                }

                //e-rede
                $payment_method['rede_credito'] = array();
                $rede_credit_cards = $this->config->get('rede_credit_card');
                if($rede_credit_cards){
                    foreach ($rede_credit_cards as $rede_credit_card){
                        switch (strtolower($rede_credit_card['descricao'])) {
                            case Type_RedeBandeira::getNickFromValue(Type_RedeBandeira::Tp_Mastercard):
                                $payment_method['rede_credito'][] = 'master';
                                break;
                            case Type_RedeBandeira::getNickFromValue(Type_RedeBandeira::Tp_Amex):
                                $payment_method['rede_credito'][] = 'amex';
                                break;
                            default:
                                $payment_method['rede_credito'][] = strtolower($rede_credit_card['descricao']);
                        }
                    }
                }

                //yapay
                $payment_method['yapay'] = array();
                if ($this->config->get('yapay_credit_card')) {
                    $yapay_credit_cards = $this->config->get('yapay_credit_card');
                    foreach ($yapay_credit_cards as $yapay_credit_card) {
                        switch (strtolower($yapay_credit_card['descricao'])) {
                            case Type_FormPgtoYapayCartao::getNickFromValue(Type_FormPgtoYapayCartao::Tp_MasterCard):
                                $payment_method['yapay'][] = 'master';
                                break;
                            case Type_FormPgtoYapayCartao::getNickFromValue(Type_FormPgtoYapayCartao::Tp_AmericanExpress):
                                $payment_method['yapay'][] = 'amex';
                                break;
                            default:
                                $payment_method['yapay'][] = strtolower($yapay_credit_card['descricao']);
                        }
                    }
                }
                if ($this->config->get('yapay_billet')) array_push($payment_method['yapay'], 'boleto');
                if ($this->config->get('yapay_bank_transfer')) array_push($payment_method['yapay'], 'transferencia');

                //wirecard
                $payment_method['wirecard'] = array();
                if ($this->config->get('wirecard_credit_card')) {
                    $wirecard_credit_cards = $this->config->get('wirecard_credit_card');
                    foreach ($wirecard_credit_cards as $wirecard_credit_card) {
                        switch (strtolower($wirecard_credit_card['descricao'])) {
                            case Type_FormPgtoWirecardCartao::getNickFromValue(Type_FormPgtoWirecardCartao::Tp_MasterCard):
                                $payment_method['wirecard'][] = 'master';
                                break;
                            case Type_FormPgtoWirecardCartao::getNickFromValue(Type_FormPgtoWirecardCartao::Tp_AmericanExpress):
                                $payment_method['wirecard'][] = 'amex';
                                break;
                            case Type_FormPgtoWirecardCartao::getNickFromValue(Type_FormPgtoWirecardCartao::Tp_DinnersClub):
                                $payment_method['wirecard'][] = 'diners';
                                break;
                            default:
                                $payment_method['wirecard'][] = strtolower($wirecard_credit_card['descricao']);
                        }
                    }
                }

                if ($this->config->get('wirecard_billet')) array_push($payment_method['wirecard'], 'boleto');
                if ($this->config->get('wirecard_bank_transfer')) array_push($payment_method['wirecard'], 'transferencia');

                //getnet
                $payment_method['getnet'] = array();
                if ($this->config->get('getnet_credit_card')) {
                    $getnet_credit_cards = $this->config->get('getnet_credit_card');
                    foreach ($getnet_credit_cards as $getnet_credit_card) {
                        switch (strtolower($getnet_credit_card['descricao'])) {
                            case Type_GetnetBandeira::getNickFromValue(Type_GetnetBandeira::Tp_Mastercard):
                                $payment_method['getnet'][] = 'master';
                                break;
                            default:
                                $payment_method['getnet'][] = strtolower($getnet_credit_card['descricao']);
                        }
                    }
                }
				$getnet_billet_status = $this->config->get('getnet_billet');
                if (isset($getnet_billet_status['status'])&&($getnet_billet_status['status']==1)) array_push($payment_method['getnet'], 'boleto');

                $payment_method['cielointegradofutura'] = array(
                    'master',
                    'visa',
                    'amex',
                    'diners',
                    'elo',
                    'discover'
                );
                // itau_shopline
                $payment_method['itaushopline'] = array(
                    'itau',
                    'boleto',
                    'transferencia'
                );
                // bcash
                $payment_method['pagamento_digital'] = array(
                    'visa',
                    'master',
                    'aura',
                    'amex',
                    'diners',
                    'hipercard',
                    'elo',
                    'boleto'
                );
                // paypal
                $payment_method['paypal'] = array(
                    'visa',
                    'master',
                    'amex',
                    'hiper',
                    'hipercard',
                    'elo',
                    'paypal'
                );
                // mercado_pago
                $payment_method['mercadopago2'] = array(
                    'master',
                    'visa',
                    'elo',
                    'amex',
                    'hipercard',
                    'diners',
                    'ml',
                    'boleto'
                );
                $payment_method['payu'] = array(
                    'visa',
                    'master',
                    'amex',
                    'diners',
                    'hipercard',
                    'elo',
                    'boleto'
                );

              $payment_method['pagarme'] = array();
              if ($this->config->get('pagarme_credit_card')) {
                $pagarme_credit_cards = $this->config->get('pagarme_credit_card');
                foreach ($pagarme_credit_cards as $pagarme_credit_card) {
                  if($pagarme_credit_card['status'] == "1"){
                    switch (strtolower($pagarme_credit_card['descricao'])) {
                      case Type_PagarmeBandeira::getNickFromValue(Type_PagarmeBandeira::Tp_Mastercard):
                        $payment_method['pagarme'][] = 'master';
                        break;
                      default:
                        $payment_method['pagarme'][] = strtolower($pagarme_credit_card['descricao']);
                    }
                  }
                }
              }

                //boleto
                $payment_method['boleto_santander'] =
                    $payment_method['boleto_bb'] =
                    $payment_method['boleto_bradesco'] = array(
                        'boleto'
                    );
                //transferencia bancaria
                $payment_method['bank_transfer'] = array(
                    'transferencia'
                );

                $bandeirasKeys = array();
                $results = $this->model_setting_extension->getExtensions('payment');

                foreach ($results as $result) {
                    $exclude_payments = array(
                        'cod',
                        'orcamento',
                        'pagamentodinamico'
                    );
                    if (!in_array($result['code'], $exclude_payments) && $this->config->get($result['code'] . '_status')) {
                        $bandeirasKeys = array_merge($bandeirasKeys, $payment_method[$result['code']]);
                    }
                }

                $bandeirasKeys = array_unique($bandeirasKeys);

                $this->data['banners'] = array();
                foreach ($bandeirasKeys as $key) {
                  if($key != '') {
                    $this->data['banners'][] = array(
                      'title' => $bandeiras[$key],
                      'class' => $key
                    );
                  }
                }
            }
        }


        // LIST BLOCKS

        if ($this->config->get('wg24_certificado_status') == Type_Certificado::TpCert_Personalizado) {
            $results = $this->model_design_banner->getBanner($this->config->get('wg24_certificado_banner'));


            foreach ($results as $result) {
                if (file_exists(DIR_IMAGE . $result['image'])) {
                    $this->data['banners_certificado'][] = array(
                        'title' => $result['title'],
                        'link' => $result['link'],
                        'image' => $this->dmodel_tool_image->resize($result['image'], $this->config->get('wg24_certificado_largura'), $this->config->get('wg24_certificado_altura'))
                    );
                }
            }
        }

        $this->children = array(
            'common/module_newsletter',
            'common/scripts_bottom'
        );

        $this->data['scripts_async'] = $this->ftrgeralregistry->getScriptManifest('catalog/view/theme/vista/public/js/scripts-async.js', true);

        $this->dataJs['testEnvironment']['status'] = (defined('AMBIENTE_TESTE')) ? true : false;

        $this->data['dataJs'] = $this->dataJs;

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

    private function app()
    {
        $this->data['mainheader_js'] = $this->ftrgeralregistry->getScriptManifest('catalog/view/theme/appmobile/public/js/mainheader.js', true);
        if (isset($this->data['aviseme_status']) && $this->data['aviseme_status']) {
            $this->document->addScriptApp(array(
                'catalog/view/theme/appmobile/public/js/mainheaderform.js',
                'catalog/view/theme/appmobile/js/jquery.aviseme.js'
            ));

            $this->data['aviseme_info']['heading_title_aviseme'] = $this->language->get('heading_title_aviseme');
            $this->data['aviseme_info']['text_nome'] = $this->language->get('text_nome');
            $this->data['aviseme_info']['text_email'] = $this->language->get('text_email');
            $this->data['aviseme_info']['entry_captcha'] = $this->language->get('entry_captcha');
            $this->data['aviseme_info']['text_continue'] = $this->language->get('text_continue');
            $this->data['aviseme_info']['entry_nome'] = $this->language->get('entry_nome');
            $this->data['aviseme_info']['entry_email'] = $this->language->get('entry_email');
            $this->data['aviseme_info']['sucess_send'] = $this->language->get('sucess_send');
            $this->data['aviseme_info']['body_message'] = $this->language->get('body_message');
            $this->data['aviseme_info']['price_esgotado'] = $this->language->get('price_esgotado');
            $this->data['aviseme_info']['heading_title_aviseme_btn'] = $this->language->get('heading_title_aviseme_btn');
            $this->data['aviseme_info']['text_send'] = $this->language->get('text_send');
            $this->data['aviseme_info']['text_cancel'] = $this->language->get('text_cancel');
            $this->data['aviseme_info']['text_conclude'] = $this->language->get('text_conclude');
        }
    }

    public function getInformation()
    {
        $this->loadLanguage();
        $this->loadModel();

        $this->data['informations'] = array();
        $informationsApp = array();

        foreach ($this->model_catalog_information->getInformations() as $result) {
            if ($result['bottom']) {
                $this->data['informations'][] = array(
                    'title' => $result['title'],
                    'href' => $this->url->link('information/information', 'information_id=' . $result['information_id'])
                );
            }

            if ($result['displayapp']) {
                $informationsApp[] = array(
                    'title' => $result['title'],
                    'href' => $this->url->link('information/information', 'information_id=' . $result['information_id'])
                );
            }
        }

        return $informationsApp;
    }

    public function getContact()
    {
        $this->loadLanguage();
        $this->data['contact'] = $this->url->link('information/contact');

        return array(
            'title' => $this->data['text_contact'],
            'href' => $this->data['contact']
        );
    }
}
