<?php

loadcontroller('base/module');

class ControllerModuleLatest extends ControllerBaseModule
{

    protected $referencePathFile = 'module/latest';

    protected $arrayJson = ['latest_module'];

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

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

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

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

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

        $errors = [];
        $targetLayoutIsDuplicatedObject = '';
        $latestModuleHabilitado = [];
        $hasAppMobileSelectedInLatestModule = false;

        $lpbcModule =  isset($this->request->post['latest_module']) ? $this->request->post['latest_module'] : [];

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

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

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

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

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

        if ($hasAppMobileSelectedInLatestModule) {
            $targetPositionIsDuplicatedObject = $this->validate->targetIsDuplicatedObject(
                $latestModuleHabilitado,
                $this->language->get('error_layout'),
                ['position']
            );
        }

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

        return $errors;
    }

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

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