<?php

namespace App\Controllers\Mobile;

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

/**
 * Class BaseController
 *
 * BaseController provides a convenient place for loading components
 * and performing functions that are needed by all your controllers.
 * Extend this class in any new controllers:
 *     class Home extends BaseController
 *
 * For security be sure to declare any new methods as protected or private.
 */
class BC extends Controller
{
    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * An array of helpers to be loaded automatically upon
     * class instantiation. These helpers will be available
     * to all other controllers that extend BaseController.
     *
     * @var array
     */
    protected $helpers = ['cookie'];

    /**
     * Constructor.
     */
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        // Do Not Edit This Line
        parent::initController($request, $response, $logger);
        helper(['app', 'Mobile/auth']);
        $minialBrowserVersion = [
            'Google Chrome' => [
                'image' => 'chrome.png',
                'version' => 105
            ],
            'Mozilla Firefox' => [
                'image' => 'mozila.png',
                'version' => 104,
            ],
            'Safari' => [
                'image' => 'safari.png',
                'version' => 12
            ],
            'Apple Safari' => [
                'image' => 'safari.png',
                'version' => 15
            ],
            'CandyCBTBro' => [
                'image' => 'candycbt.png',
                'version' => options('exambro_version')
            ]
        ];
        $bc['no_footer'] = true;
        $bc['no_header'] = true;
        if (redison()) autoload_option_to_redis();
        else autoload_options();
        //page without database
        $allowMaintenance = ['maintenance'];
        //check maintenance mode
        try {
            $ap = options('maintenance');
            if ($ap == 1 || $ap == null) throw new Exception("Maintenance Mode");
            if (uri_string() == 'maintenance') {
                header("Location: " . base_url());
                die;
            }
        } catch (\Throwable $th) {
            if (!in_array(uri_string(), $allowMaintenance)) {
                header("Location: " . base_url('maintenance'));
                die;
            }
        }

        if (options('browserCheck') == '1') {
            try {
                $b = getBrowser();
                $bvn = (int) explode('.', $b['version'])[0];
                $mbv = $minialBrowserVersion[$b['name']];
                if ($bvn < $mbv['version']) {
                    $bc['browserName'] = $b['name'];
                    $bc['browserImage'] = $mbv['image'];
                    echo view('Mobile/Pages/outdated-browser', $bc);
                    die;
                }
            } catch (\Throwable $th) {
                if (ENVIRONMENT != 'production') {
                    dd($th->getMessage());
                } else {
                    echo view('Mobile/Pages/outdated-browser', $bc);
                }
                die;
            }
        }
        if (options('strict_browser') == '1') {
            try {
                $b = getBrowser();
                $bvn = (int) explode('.', $b['version'])[0];
                $mbv = $minialBrowserVersion[$b['name']];
                $ua = strtolower($b['userAgent']);
                $isApple = strpos($ua, 'iphone') !== false;
                if (!$isApple) $isApple = strpos($ua, 'ipad') !== false;
                // if(!$isApple) $isApple = strpos($ua, 'mac') !== false;
                // if(!$isApple) $isApple = strpos($ua, 'ios') !== false;
                // if(!$isApple) $isApple = strpos($ua, 'safari') !== false;
                // if(!$isApple) $isApple = strpos($ua, 'apple') !== false;

                if ($b['name'] != 'CandyCBTBro' && !$isApple) {
                    $bc['browserName'] = $b['name'];
                    $bc['browserImage'] = $mbv['image'];
                    echo view('Mobile/Pages/strict-browser', $bc);
                    die;
                }
            } catch (\Throwable $th) {
                if (ENVIRONMENT != 'production') {
                    dd($th->getMessage());
                } else {
                    echo view('Mobile/Pages/strict-browser', $bc);
                }
                die;
            }
        }


        // Preload any models, libraries, etc, here.

        // E.g.: $this->session = \Config\Services::session();
    }
}
