<?php
abstract class ControllerBaseBase extends Controller
{
    protected $errors = [];
    protected $referencePathFile;
    protected $fileName;
    protected $pathName;
    protected $fieldsToRemoveMask = [];
    protected $dataVue = [];

    public function __construct($registry)
    {
        parent::__construct($registry);

        $referenceArray = explode('/', $this->referencePathFile);
        $this->fileName = $referenceArray[1];
        $this->pathName = $referenceArray[0];
    }

    protected function loadLanguage()
    {
        $language = $this->language->load($this->referencePathFile);
        $language = array_filter($language, function($key){
            return strpos($key, 'error_') !== 0;
        }, ARRAY_FILTER_USE_KEY);
        $this->dataVue = array_merge($this->dataVue, $language);
    }

    protected function loadScript()
    {
        $this->document->addScript("public/js/$this->referencePathFile.js");
    }

    protected function loadStyle()
    {
    }

    protected function breadcrumbs()
    {
    }

    protected function actionUrls()
    {
    }

    protected function loadModel()
    {
        $this->load->model('setting/setting');

        $model = DIR_APPLICATION . "model/$this->referencePathFile.php";
        $dmodel = DIR_SYSTEM . "dmodel/$this->referencePathFile.php";

        if (file_exists($model)) {
            $this->load->model($this->referencePathFile);
        }

        if (file_exists($dmodel)) {
            $this->load->dmodel($this->referencePathFile);
        }
    }

    protected function loadData()
    {
    }

    protected function childrens()
    {
        $this->children = [
            'common/header',
            'common/footer'
        ];
    }

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

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

        $this->loadModel();

        $this->loadData();

        $this->childrens();

        $this->actionUrls();

        if ($this->ftrgeralregistry->isApp()) {
            $this->app();
        }

        $this->response->setOutput($this->render($this->referencePathFile));
    }

    public function save()
    {
        // $this->loadLanguage();
        // $this->loadModel();
        // $response = [];

        // $this->errors = $this->validate();

        // if (!$this->errors) {
        //     $this->removeMask();
        //     $this->removeMaskExec();

        //     $this->session->data['success'] = $this->language->get('text_success');
        //     $response['success'] = $this->language->get('text_success');
        // } else {
        //     $response['errors'] = $this->errors;
        // }

        // $this->response->setOutputJson($response);
    }

    protected function app()
    {
        // Não implementar aqui, opcional implementar nos filhos
    }

    private function removeMaskExec()
    {
        foreach ($this->fieldsToRemoveMask as $fieldName => $method) {
            $this->request->post[$fieldName] = ftr_geral::$method($this->request->post[$fieldName]);
        }
        $this->fieldsToRemoveMask = [];
    }

    protected function removeMask()
    {
        // Não implementar aqui, opcional implementar nos filhos
    }

    protected function validate()
    {
        // Não implementar aqui, opcional implementar nos filhos
    }

    private function addData()
    {
        $this->data = array_merge($this->data, $this->dataVue);
        $this->data['dataVue'] = $this->dataVue;
    }

    protected function render($templateRoute = null)
    {
        $this->addData();
        return parent::render($templateRoute);
    }
}
