<?php

class ControllerModuleFuturaFiltroMarca extends Controller {

    private $error = array();

    public function index() {
        $this->load->type('tipocomum');
        $this->load->helper('ftr_geral');

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

        $this->document->setTitle($this->language->get('heading_title'));

        $this->data = array_merge($this->data, $language);

        $this->data['languageForJS'] = array(
          'module/futurafiltromarca' => $language
        );

        $this->load->model('setting/setting');

        $this->document->addScript('admin/public/js/module/futura-filtro-marca.js');

        $this->data['ftr_geral'] = new ftr_geral();
        $this->data['Type_Status'] = $this->load->typeForTwig('Type_Status');

        $this->document->addTypesForJs(array(
          Type_Status::getTypeForJs(),
          Type_ModulePosition::getTypeForJs(),
        ));

        $this->data['positions'] = Type_ModulePosition::getRemoveArray([Type_ModulePosition::Tp_AppMobile], true);

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validate())) {
            $this->model_setting_setting->editSetting('futurafiltromarca', $this->request->post);

            $this->session->data['success'] = $this->language->get('text_success');

            $this->redirect($this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'));
        }

        $this->data['token'] = $this->session->data['token'];
        $this->data['cancel'] = $this->url->link('index', 'token=' . $this->session->data['token'], 'SSL');

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

        $this->data['button_save']       = $this->language->get('button_save');
        $this->data['button_cancel']  	 = $this->language->get('button_cancel');
        $this->data['button_add_module'] = $this->language->get('button_add_module');
        $this->data['button_remove']     = $this->language->get('button_remove');
        
        $this->data['text_heading_filter_title']        = $this->language->get('text_heading_filter_title');
        $this->data['text_enabled']                     = $this->language->get('text_enabled');
        $this->data['text_disabled']                    = $this->language->get('text_disabled');
        $this->data['text_futurafiltromarca_status']    = $this->language->get('text_futurafiltromarca_status');
        $this->data['text_content_top']                 = $this->language->get('text_content_top');
        $this->data['text_content_bottom']              = $this->language->get('text_content_bottom');
        $this->data['text_column_left']                 = $this->language->get('text_column_left');
        $this->data['text_column_right']                = $this->language->get('text_column_right');

        $this->data['entry_layout']     = $this->language->get('entry_layout');
        $this->data['entry_position']   = $this->language->get('entry_position');
        $this->data['entry_status']     = $this->language->get('entry_status');
        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];
            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        if (isset($this->request->post['futurafiltromarca_heading_title'])) {
            $this->data['futurafiltromarca_heading_title'] = $this->request->post['futurafiltromarca_heading_title'];
        } else {
            $this->data['futurafiltromarca_heading_title'] = $this->config->get('futurafiltromarca_heading_title');
        }

        if (isset($this->request->post['futurafiltromarca_status'])) {
            $this->data['futurafiltromarca_status'] = $this->request->post['futurafiltromarca_status'];
        } elseif ($this->config->get('futurafiltromarca_status')) {
            $this->data['futurafiltromarca_status'] = $this->config->get('futurafiltromarca_status');
        }

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

        if (isset($this->request->post['futurafiltromarca_module'])) {
            $this->data['modules'] = $this->request->post['futurafiltromarca_module'];
        } elseif ($this->config->get('futurafiltromarca_module')) {
            $this->data['modules'] = $this->config->get('futurafiltromarca_module');
        }

        $this->load->dmodel('design/layout');

        $arrayRemove = array(
            Type_IdLayout::Tp_IdHome,
            Type_IdLayout::Tp_IdProduct,
            Type_IdLayout::Tp_IdManufactured,
            Type_IdLayout::Tp_IdConta,
            Type_IdLayout::Tp_IdConfirme,
            Type_IdLayout::Tp_IdContact,
            Type_IdLayout::Tp_IdSiteMap,
            Type_IdLayout::Tp_IdInformation,
        );

        $layouts = $this->dmodel_design_layout->getLayouts();
        $layouts = array_filter($layouts, function ($value) use($arrayRemove) {
            return !in_array($value['layout_id'], $arrayRemove);
        });
        $this->data['layouts'] = $layouts;

        $this->data['breadcrumbs'] = array();
        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_module'),
            'href' => $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('module/futurafiltromarca', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL'),
            'separator' => ' :: '
        );

        $this->data['action'] = $this->url->link('module/futurafiltromarca', 'token=' . $this->session->data['token'] . '&type=' . $this->request->get['type'], 'SSL');
        $this->data['cancel'] = $this->url->linkAnchor('extension/module', 'token=' . $this->session->data['token'], 'tab-info=' . $this->request->get['type'], 'SSL');

        $this->children = array(
            'common/header',
            'common/footer',
        );

        $this->response->setOutput($this->render('module/futurafiltromarca'));
    }

    private function validate() {
        if (!$this->user->hasPermission('modify', 'module/futurafiltromarca')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }
    
        if (isset($this->request->post['futurafiltromarca_module'])) {
            $modules = $this->request->post['futurafiltromarca_module'];
            $positions_to_check = ['content_top', 'content_bottom', 'column_left'];
            $app_position = 'content_app';
            $combination_check = [];
    
            foreach ($modules as $module) {
                $layout_id = $module['layout_id'];
                $position = $module['position'];
    
                if (!isset($combination_check[$layout_id])) {
                    $combination_check[$layout_id] = [];
                }
    
                if (!isset($combination_check[$layout_id][$position])) {
                    $combination_check[$layout_id][$position] = 0;
                }
                $combination_check[$layout_id][$position]++;
            }
    
            foreach ($combination_check as $layout_id => $positions_array) {
                $content_count = 0;
                $app_count = 0;
    
                foreach ($positions_array as $position => $count) {
                    if (in_array($position, $positions_to_check)) {
                        $content_count += $count;
                    } elseif ($position === $app_position) {
                        $app_count += $count;
                    }
                }
    
                if ($content_count > 1 || $app_count > 1) {
                    $this->error['warning'] = $this->language->get('error_duplicate_cross_layout');
                   
                    foreach ($modules as &$module) {
                        $layout_id = $module['layout_id'];
                        $position = $module['position'];
    
                        if (($content_count > 1 && in_array($position, $positions_to_check)) || ($app_count > 1 && $position === $app_position)) {
                            $module['error'] = true;
                        }
                    }
                    break;
                }
            }
        }
    
        return !$this->error;
    }
                         
}
