<?php

abstract class BaseController extends Controller
{
    protected $errors = [];
    protected $referencePathFile;
    protected $fileName;
    protected $pathName;
    protected $token;
    protected $dataVue = [];
    protected $responseJson = [];

    abstract protected function loadData();

    public function __construct($registry)
    {
        parent::__construct($registry);
        $this->token = $this->session->data['token'];

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

    protected function loadLanguage()
    {
        $language = $this->load->language($this->referencePathFile);
        $this->dataVue = array_merge($this->dataVue, $language);
    }

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

    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 loadController()
    {
        // Não implementar aqui, opcional implementar nos filhos
    }

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

    protected function hasPermission()
    {
        //validação feitas para pathfile que tenham mais de uma tela ex: mercadolivre, skyhub
        if (substr_count($this->referencePathFile, '/') > 1) {
            $referencePathFile = explode("/", $this->referencePathFile) ;
            $permission = $referencePathFile[0]."/".$referencePathFile[1];
        }else{
            $permission = $this->referencePathFile;
        }

        if (!$this->user->hasPermission('modify', $permission)) {
            $this->errors['warning'] = $this->language->get('error_permission');
            $this->responseJson['errors'] = $this->errors;
            return $this->response->setOutputJson($this->responseJson);
        }
    }


    protected function removeMaskExec()
    {
        foreach ($this->fieldsToRemoveMask as $fieldName => $method) {
            $strpos = strpos($fieldName, '.');
            if ($strpos){
                $this->removeMaskFromObject($fieldName, $method, $this->request->post);
            } else {
                $this->request->post[$fieldName] = ftr_geral::$method($this->request->post[$fieldName]);
            }
        }
        $this->fieldsToRemoveMask = [];
    }

    protected function removeMaskFromObject($field, $method, &$post){
        $start = 0;
        $result = [];

        $result = &$post[substr($field, 0, strpos($field, '.', strpos($field, '.')))];
        while(($newLevel = strpos($field, '.', $start)) !== false){
            $start = $newLevel + 1;
            $strpos = strpos($field, '.', $start) ? strpos($field, '.', $start) - $start : strlen($field);
            $result = &$result[substr($field, $start, $strpos)];
        }

        $result = ftr_geral::$method($result);
    }

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

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

    protected 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);
    }
}
