<?php

loadcontroller('base/module');

class ControllerModuleLpbc extends ControllerBaseModule
{
    protected $referencePathFile = 'module/lpbc';

    protected function loadModel()
    {
        parent::loadModel();
    }

    protected function loadData()
    {
        $this->load->dmodel('design/layout');
        $this->load->model('catalog/category');
        parent::loadData();

        $this->dataVue['TypesForJs'] = array(
            Type_DsimDnao::getTypeForJs(),
            Type_ModulePosition::getTypeForJs(),
            Type_DefaultValues::getTypeForJs(),
            Type_Status::getTypeForJs()
        );

        // Remove as opções de layout que não são usadas nos módulos de lista de produtos
        $data = array(
            'notarray' => Type_NotListaProdutos::getEnumList()
        );

        $this->dataVue['layouts'] = $this->dmodel_design_layout->getLayouts($data);

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

        foreach ($results as $result) {
            if ($result['status']) {
                $this->dataVue['categories'][] = array(
                    'category_id' => $result['category_id'],
                    'name'        => $result['name']
                );
            }
        }
    }

    public function validate()
    {
        $this->loadLanguage();

        $errors = [];
        $targetLayoutIsDuplicatedObject = '';
        $targetCategoryIsDuplicatedObject = '';
        $targetPositionIsDuplicatedObject = '';
        $lpbcModuleHabilitado = [];
        $hasAppMobileSelectedInLpbcModule = false;
        $hasAppMobileSelectedInLpbcModuleInterests = false;

        $isShowOnlyLogged = $this->request->post['lpbc_module_interests_status'] == '1';
        $lpbcModule =  isset($this->request->post['lpbc_module']) ? $this->request->post['lpbc_module'] : [];
        $lpbcModuleInterests =  isset($this->request->post['lpbc_module_interests']) ? $this->request->post['lpbc_module_interests'] : [];

        if ($isShowOnlyLogged && isset($lpbcModuleInterests) && !empty($lpbcModuleInterests)) {
            foreach ($lpbcModuleInterests as $key => $value) {
                if ($value['status'] == Type_Status::Tp_Habilitado) {
                    array_push($lpbcModuleHabilitado, $value);
                }

                if ($value['position'] == Type_ModulePosition::Tp_AppMobile) {
                    $hasAppMobileSelectedInLpbcModuleInterests = true;
                }

                $module_errors = $this->validate->execute([
                    'lpbc_module_interests[' . $key . '][limit]' => [
                        'value' => $value['limit'],
                        'validates' => ['isEmpty'],
                    ],
                ]);

                $errors = array_merge($errors, $module_errors);
            }
        } else if (!$isShowOnlyLogged && isset($lpbcModule) && !empty($lpbcModule)) {
            foreach ($lpbcModule as $key => $value) {
                if ($value['status'] == Type_Status::Tp_Habilitado) {
                    array_push($lpbcModuleHabilitado, $value);
                }

                if ($value["position"] == Type_ModulePosition::Tp_AppMobile) {
                    $hasAppMobileSelectedInLpbcModule = true;
                }

                $module_errors = $this->validate->execute([
                    'lpbc_module[' . $key . '][limit]' => [
                        'value' => $value['limit'],
                        'validates' => ['isEmpty'],
                    ],
                ]);

                $errors = array_merge($errors, $module_errors);
            }
        }

        $targetLayoutIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
            $lpbcModuleHabilitado,
            $this->language->get('error_layout'),
            ["layout_id"]
        );

        $targetCategoryIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
            $lpbcModuleHabilitado,
            $this->language->get('error_layout'),
            ['category_id']
        );

        if ($hasAppMobileSelectedInLpbcModule || $hasAppMobileSelectedInLpbcModuleInterests) {
            $targetPositionIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
                $lpbcModuleHabilitado,
                $this->language->get('error_layout'),
                ['position']
            );
        }

        if (
            (!$isShowOnlyLogged && !$hasAppMobileSelectedInLpbcModule && $targetCategoryIsDuplicatedObject && $targetLayoutIsDuplicatedObject) || (!$isShowOnlyLogged && $hasAppMobileSelectedInLpbcModule && $targetPositionIsDuplicatedObject && $targetCategoryIsDuplicatedObject && $targetLayoutIsDuplicatedObject)
        ) {
            $errors['message'] = $targetCategoryIsDuplicatedObject;
        }

        if (
            ($isShowOnlyLogged && !$hasAppMobileSelectedInLpbcModuleInterests && $targetLayoutIsDuplicatedObject) || ($isShowOnlyLogged && $hasAppMobileSelectedInLpbcModuleInterests && $targetPositionIsDuplicatedObject && $targetLayoutIsDuplicatedObject)
        ) {
            $errors['message'] = $targetLayoutIsDuplicatedObject;
        }

        return $errors;
    }


    public function install()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->install('module', 'lpbc');
    }

    public function uninstall()
    {
        $this->load->model('setting/extension');
        $this->model_setting_extension->uninstallSetting('module', 'lpbc');
    }
}
