<?php

namespace kernel\services;

use DirectoryIterator;
use kernel\EntityRelation;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\helpers\Version;
use kernel\models\Option;
use ZipArchive;

class ModuleService
{
    protected array $errors = [];

    protected null|bool $serverAvailable = null;

    /**
     * @param string $module
     * @return false|array|string
     */
    public function getModuleInfo(string $module): false|array|string
    {
        $info = [];
        $info['path'] = $module;
        if (file_exists($module . "/manifest.json")) {
            $manifest = file_get_contents($module . "/manifest.json");
            $manifest = Manifest::getWithVars($manifest);
            $manifest = getConst($manifest);
            $info = array_merge($info, $manifest);
        }

        return $info;
    }

    /**
     * @return array
     */
    public function getErrors(): array
    {
        return $this->errors;
    }

    /**
     * @param $msg
     * @return void
     */
    public function addError($msg): void
    {
        $this->errors[] = $msg;
    }

    /**
     * @param string $slug
     * @return false|array|string
     */
    public function getModuleInfoBySlug(string $slug): false|array|string
    {
        return $this->getModuleInfo($this->getModuleDir($slug));
    }

    /**
     * @param string $slug
     * @return bool
     */
    public function isActive(string $slug): bool
    {
        $active_modules = Option::where("key", "active_modules")->first();
        if ($active_modules) {
            $modules = json_decode($active_modules->value);
            foreach ($modules->modules as $mod) {
                if ($mod === $slug) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * @param string $module
     * @return void
     */
    public function toggleModule(string $module): void
    {
        $active_modules_info = Option::where("key", "active_modules")->first();
        $active_modules = json_decode($active_modules_info->value);
        if (in_array($module, $active_modules->modules)) {
            $this->deactivateModule($module);
        } else {
            $this->setActiveModule($module);
        }
    }

    /**
     * @param string $module
     * @return bool
     */
    public function setActiveModule(string $module): bool
    {
        $active_modules_info = Option::where("key", "active_modules")->first();
        $active_modules = json_decode($active_modules_info->value);
        if (in_array($module, $active_modules->modules)) {
            return true;
        }

        $module_info = $this->getModuleInfoBySlug($module);
        if (isset($module_info['dependence'])) {
            $dependence_array = explode(',', $module_info['dependence']);
            foreach ($dependence_array as $depend) {
                if (!in_array(trim($depend), $active_modules->modules)) {
                    $this->addError("first activate the $depend module");
                    return false;
                }
            }
        }
        $active_modules->modules[] = $module;
        $this->runInitScript($this->getModuleInfoBySlug($module));

        $active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
        $active_modules_info->save();

        return true;
    }

    /**
     * @param string $module
     * @return bool
     */
    public function deactivateModule(string $module): bool
    {
        $active_modules_info = Option::where("key", "active_modules")->first();

        EntityRelation::removeEntityRelation($module);
        EntityRelation::removePropertyRelation($module);

        $active_modules = json_decode($active_modules_info->value);
        if (!in_array($module, $active_modules->modules)) {
            return true;
        }

        $dependence_array = $this->getDependencies();
        $str_for_exception = '';
        foreach ($dependence_array as $mod => $depend) {
            if (in_array($module, $depend)) {
                if ($str_for_exception !== '') {
                    $str_for_exception .= ', ';
                }
                $str_for_exception .= $mod;
            }
        }

        if ($str_for_exception !== '') {
            $this->addError("You can not deactivate $module module. First deactivate modules: $str_for_exception");
            return false;
        }

        unset($active_modules->modules[array_search($module, $active_modules->modules)]);
        $active_modules->modules = array_values($active_modules->modules);
        $this->runDeactivateScript($this->getModuleInfoBySlug($module));

        $active_modules_info->value = json_encode($active_modules, JSON_UNESCAPED_UNICODE);
        $active_modules_info->save();

        return true;
    }

    public function getModuleDir(string $slug)
    {
        $module_paths = Option::where("key", "module_paths")->first();
        $dirs = [];
        if ($module_paths) {
            $path = json_decode($module_paths->value);
            foreach ($path->paths as $p) {
                $dirs[] = getConst($p);
            }
        }

        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if (basename($fileInfo->getPathname()) === $slug) {
                    return $fileInfo->getPathname();
                }
            }
        }
        return false;
    }

    /**
     * @param $mod_info
     * @return void
     */
    public function runInitScript($mod_info): void
    {
        if (isset($mod_info['module_class'])) {
            if (isset($mod_info['module_class_file'])) {
                require_once $mod_info['module_class_file'];
            }
            $moduleClass = new $mod_info['module_class']();
            $moduleClass->init();
        }
    }

    /**
     * @param $mod_info
     * @return void
     */
    public function runDeactivateScript($mod_info): void
    {
        if (isset($mod_info['module_class'])) {
            if (isset($mod_info['module_class_file'])) {
                require_once $mod_info['module_class_file'];
            }
            $moduleClass = new $mod_info['module_class']();
            $moduleClass->deactivate();
        }
    }

    /**
     * @return array
     */
    public function getActiveModules(): array
    {
        $modules = [];
        $module_paths = Option::where("key", "module_paths")->first();
        $active_modules = Option::where("key", "active_modules")->first();
        $dirs = [];
        if ($module_paths) {
            $path = json_decode($module_paths->value);
            foreach ($path->paths as $p) {
                $dirs[] = getConst($p);
            }
        }

        $active_modules = json_decode($active_modules->value, true);

        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot() or !in_array($fileInfo->getFilename(), $active_modules['modules'])) continue;
                $modules[] = $this->getModuleInfo($fileInfo->getPathname());
            }
        }

        return $modules;
    }

    /**
     * @return array
     */
    public function getModulesRouts(): array
    {
        $routs = [];
        $modules = $this->getActiveModules();
        foreach ($modules as $module) {
            if (isset($module['routs'])) {
                $routs[] = $module['path'] . "/" . $module['routs'];
            }
        }

        return $routs;
    }

    /**
     * @return array
     */
    public function getModulesMigrationsPaths(): array
    {
        $migrationsPaths = [];
        $modules = $this->getActiveModules();
        foreach ($modules as $module) {
            if (isset($module['migration_path'])) {
                $migrationsPaths[] = $module['path'] . "/" . $module['migration_path'];
            }
        }

        return $migrationsPaths;
    }

    /**
     * @param string $path
     * @return bool
     */
    public function installModule(string $path): bool
    {
        $zip = new ZipArchive;
        $tmpModuleDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
            $zip->extractTo($tmpModuleDirFull);
            $zip->close();
        } else {
            $this->addError('unable to open zip archive');
            return false;
        }

        if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
            $this->addError('manifest.json not found');
            return false;
        }

        $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
        $manifest = Manifest::getWithVars($manifestJson);

        $fileHelper = new Files();
        if (isset($manifest['app_module_path'])) {
            $fileHelper->copy_folder($tmpModuleDirFull . 'app', $manifest['app_module_path']);
        } else {
            $fileHelper->copy_folder($tmpModuleDirFull . 'app', APP_DIR . '/modules/' . $manifest['slug']);
        }

        if (isset($manifest['kernel_module_path'])) {
            $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
        } else {
            $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
        }

        $fileHelper->recursiveRemoveDir($tmpModuleDirFull);
        unlink(ROOT_DIR . $path);

        return true;
    }

    /**
     * @param string $path
     * @return void
     */
    public function uninstallModule(string $path): void
    {
        $moduleInfo = $this->getModuleInfo(APP_DIR . '/modules/' . basename($path));

        if ($this->isActive($moduleInfo['slug'])) {
            $this->deactivateModule($moduleInfo['slug']);
        }

        $fileHelper = new Files();
        if (file_exists(APP_DIR . '/modules/' . $moduleInfo['slug'])) {
            $fileHelper->recursiveRemoveDir(APP_DIR . '/modules/' . $moduleInfo['slug']);
        }
        if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug'])) {
            $fileHelper->recursiveRemoveDir(KERNEL_APP_MODULES_DIR . '/' . $moduleInfo['slug']);
        }
    }

    /**
     * @param string $path
     * @return void
     */
    public function packModule(string $path): void
    {
        $moduleName = basename($path);

        $tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $moduleName . "/";

        $fileHelper = new Files();
        $fileHelper->copy_folder(ROOT_DIR . $path, $tmpModuleDirFull . 'app/');
        if (file_exists(KERNEL_APP_MODULES_DIR . '/' . $moduleName)) {
            $fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');
        } else {
            $old_mask = umask(0);
            mkdir($tmpModuleDirFull . 'kernel/', 0775, true);
            umask($old_mask);
        }

        if (!is_dir(RESOURCES_DIR . '/tmp/modules')) {
            $old_mask = umask(0);
            mkdir(RESOURCES_DIR . '/tmp/modules', 0775, true);
            umask($old_mask);
        }
        $fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.igm');
    }

    /**
     * @param string $path
     * @return bool
     */
    public function updateModule(string $path): bool
    {
        $zip = new ZipArchive;
        $tmpModuleDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpModuleDirFull = RESOURCES_DIR . '/tmp/modules/' . $tmpModuleDir . "/";
            $zip->extractTo($tmpModuleDirFull);
            $zip->close();
        } else {
            $this->addError('unable to open zip archive');
            return false;
        }

        if (!file_exists($tmpModuleDirFull . "app/manifest.json")) {
            $this->addError('manifest.json not found');
            return false;
        }

        $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "app/manifest.json"));
        $manifest = Manifest::getWithVars($manifestJson);

        $fileHelper = new Files();
        if (isset($manifest['app_module_path'])) {
            $fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', $manifest['app_module_path'] . '/manifest.json');
        } else {
            $fileHelper->copy_folder($tmpModuleDirFull . 'app/manifest.json', APP_DIR . '/modules/' . $manifest['slug'] . '/manifest.json');
        }

        if (isset($manifest['kernel_module_path'])) {
            $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', $manifest['kernel_module_path']);
        } else {
            $fileHelper->copy_folder($tmpModuleDirFull . 'kernel', KERNEL_APP_MODULES_DIR . '/' . $manifest['slug']);
        }
        $fileHelper->recursiveRemoveDir($tmpModuleDirFull);

        unlink(ROOT_DIR . $path);

        return true;
    }

    /**
     * @return array
     */
    public function getDependencies(): array
    {
        $modules_info = $this->getActiveModules();
        $dependence_array = [];
        foreach ($modules_info as $mod) {
            if (isset($mod['dependence'])) {
                $dependence_array[$mod['slug']] = explode(',', $mod['dependence']);
            }
        }

        return $dependence_array;
    }

    public function isInstall(string $slug): bool
    {
        $module_paths = Option::where("key", "module_paths")->first();
        $dirs = [];
        if ($module_paths) {
            $path = json_decode($module_paths->value);
            foreach ($path->paths as $p) {
                $dirs[] = getConst($p);
            }
        }
        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot()) continue;
                if ($this->getModuleInfo($fileInfo->getPathname())['slug'] === $slug) {
                    return true;
                };
            }
        }

        return false;
    }

    public function isLastVersion(string $slug): bool
    {
        if ($this->isServerAvailable()) {
            $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');

            $modules_info = json_decode($modules_info->getBody()->getContents(), true);

            $mod_info = $this->getModuleInfoBySlug($slug);

            $currentVersion = Version::getIntVersionByString($mod_info['version']);
            foreach ($modules_info as $mod) {
                $modVersion = Version::getIntVersionByString($mod['version']);
                if ($mod['slug'] === $mod_info['slug'] && $modVersion <= $currentVersion) {
                    return true;
                }
            }
        }

        return false;
    }

    public function isKernelModule(string $slug): bool
    {
        $modules_info = $this->getKernelModules();
        foreach ($modules_info as $mod) {
            if ($mod['slug'] === $slug) {
                return true;
            }
        }

        return false;
    }

    public function isShopModule(string $slug): bool
    {
        if ($this->isServerAvailable()) {
            $modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');

            if (!$this->issetModuleShopToken()) {
                return false;
            }

            $modules_info = json_decode($modules_info->getBody()->getContents(), true);
            if (isset($modules_info)) {
                $mod_info = $this->getModuleInfoBySlug($slug);
                foreach ($modules_info as $mod) {
                    if ($mod['slug'] === $mod_info['slug']) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public function getKernelModules(): array
    {
        $modules_info = [];
        foreach (new DirectoryIterator(KERNEL_MODULES_DIR) as $fileInfo) {
            if ($fileInfo->isDot()) continue;
            $modules_info[] = $this->getModuleInfo($fileInfo->getPathname());
        }

        return $modules_info;
    }

    public function isServerAvailable(): bool
    {
        if (null !== $this->serverAvailable) {
            return $this->serverAvailable;
        }

        try {
            RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
            $this->serverAvailable = true;

            return true;
        } catch (\Exception $e) {
            $this->serverAvailable = false;
            return false;
        }
    }

    public function issetModuleShopToken(): bool
    {
        if (!empty($_ENV['MODULE_SHOP_TOKEN'])) {
            return true;
        }

        return false;
    }

    public function createDirs(string $slug): void
    {
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/controllers");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/migrations");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/services");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/models/forms");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/routs");
        mkdir(KERNEL_APP_MODULES_DIR . "/$slug/views");

        mkdir(APP_DIR . "/modules/$slug");
        mkdir(APP_DIR . "/modules/$slug/controllers");
        mkdir(APP_DIR . "/modules/$slug/routs");
    }

    public function createModuleByParams(array $params): void
    {
        $slug = $params['slug'];
        $model = $params['model'];

        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/manifests/manifest_template', APP_DIR . "/modules/$slug/manifest.json", $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/kernel_controller_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/controllers/app_controller_template', APP_DIR . '/modules/' . $slug . '/controllers/' . $model . 'Controller.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/kernel_routs_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/routs/' . $slug . '.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/routs/app_routs_template', APP_DIR . '/modules/' . $slug . '/routs/' . $slug . '.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/kernel_module_file_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/' . $model . 'Module.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/module_files/app_module_file_template', APP_DIR . '/modules/' . $slug . '/' . $model . 'Module.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/model_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/' . $model . '.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/models/forms/create_form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/models/forms/Create' . $model . 'Form.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/services/service_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/services/' . $model . 'Service.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/index_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/index.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/view_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/view.php', $params);
        $this->createModuleFileByTemplate(KERNEL_TEMPLATES_DIR . '/views/form_template', KERNEL_APP_MODULES_DIR . '/' . $slug . '/views/form.php', $params);
    }

    public function createModuleFileByTemplate(string $templatePath, string $filePath, array $params): void
    {
        $data = file_get_contents($templatePath);

        foreach ($params as $key => $param){
            $data = str_replace("{" . $key . "}", $param, $data);
        }

        file_put_contents($filePath, $data);
    }

}