<?php

use \ModulesGarden\HostingQuotaNotifications\Core\ModuleConstants;
use \ModulesGarden\HostingQuotaNotifications\Core\ServiceLocator;
use \ModulesGarden\HostingQuotaNotifications\Core\FileReader\Validator;
use \ModulesGarden\HostingQuotaNotifications\Core\FileReader\File;
use \ModulesGarden\HostingQuotaNotifications\Core\DependencyInjection\Builder;
use \ModulesGarden\HostingQuotaNotifications\Core\DependencyInjection\Services;


if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('PS')) define('PS', PATH_SEPARATOR);
if (!defined('CRLF')) define('CRLF', "\r\n");

require_once dirname(__DIR__) . DS . "vendor" . DS . "autoload.php";
require_once __DIR__ . DS . 'Helper' . DS . 'Functions.php';

ModuleConstants::initialize();

/**
 * Initailize DI builder
 */
new Builder();

/**
 * Initialize Services
 */
new Services();

if (is_dir(ModuleConstants::getFullPath('storage')) === false 
        || is_writable(ModuleConstants::getFullPath('storage')) === false
        || is_writable(ModuleConstants::getFullPath('storage', 'app')) === false
        || is_writable(ModuleConstants::getFullPath('storage', 'framework')) === false
        || is_writable(ModuleConstants::getFullPath('storage', 'crons')) === false
        || is_writable(ModuleConstants::getFullPath('storage', 'logs')) === false)
{
    File::createPaths(
            ['full' => ModuleConstants::getFullPath('storage'),              'permission' => 0777],
            ['full' => ModuleConstants::getFullPath('storage', 'app'),       'permission' => 0777],
            ['full' => ModuleConstants::getFullPath('storage', 'framework'), 'permission' => 0777],
            ['full' => ModuleConstants::getFullPath('storage', 'crons'),     'permission' => 0777],
            ['full' => ModuleConstants::getFullPath('storage', 'logs'),      'permission' => 0777]
    );
}

// Validator::getInstance()->validateFile(ModuleConstants::getFullPath()) === false do sprawdzenia czy 
// main path z modulem jest writable i readable lecz trzeba jeszcze wymyśleć co ma się pojawić jeżeli nie :D
if (Validator::getInstance()->validateFile(ModuleConstants::getFullPath('storage', 'framework')) === false
        || Validator::getInstance()->validateFile(ModuleConstants::getFullPath('storage', 'framework')) === false
        || Validator::getInstance()->validateFile(ModuleConstants::getFullPath('storage', 'logs')) === false)
{
    ServiceLocator::call('errorManager')->addError(
        'Bootstrap',
        PHP_EOL . ServiceLocator::call('lang')->absoluteT('permissionsStorage'),
        ['path' => ModuleConstants::getFullPath('storage')]
    );
}
