<?php

namespace App\Controllers;

use CodeIgniter\Controller;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;

class FrontendBaseController extends Controller
{
    protected $request;
    protected \CodeIgniter\Session\Session $session;

    protected $helpers = [
        'basic',
        'form',
        'url',
        'cookie',
        'module'
    ];

    public $title = 'Rewarder';
    public $menu = '';
    public $submenu = '';

    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        parent::initController($request, $response, $logger);
        $this->session = \Config\Services::session();
        setDefaultViewData();
        $this->setDefaultPageData();
        service('request')->setLocale(getUserlang());
    }

    public function setDefaultPageData()
    {
        setPageData([
            'title' => $this->title,
            'menu' => $this->menu,
            'submenu' => $this->submenu,
        ]);
    }

    public function updatePageData(array $newData)
    {
        $defaultData = [
            'title' => $this->title,
            'menu' => $this->menu,
            'submenu' => $this->submenu,
        ];
        setPageData(array_merge($defaultData, $newData));
    }

    public function permissionCheck($key)
    {
        if (\is_logged() && !hasPermissions($key)) {
            $this->response->redirect('/errors/denied');
        }
    }
}

