<?php

namespace kernel\services;

use DirectoryIterator;
use kernel\console\Out;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\models\Option;
use ZipArchive;

class ModuleService
{

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

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

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

        return false;
    }

    public function toggleModule(string $module): void
    {
        $active_modules = Option::where("key", "active_modules")->first();
        $path = json_decode($active_modules->value);
        if (in_array($module, $path->modules)) {
            unset($path->modules[array_search($module, $path->modules)]);
            $path->modules = array_values($path->modules);
            $this->runDeactivateScript($this->getModuleInfoBySlug($module));
        } else {
            $path->modules[] = $module;
            $this->runInitScript($this->getModuleInfoBySlug($module));
        }
        $active_modules->value = json_encode($path, JSON_UNESCAPED_UNICODE);
        $active_modules->save();
    }

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

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

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

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

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

        return $routs;
    }

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

    /**
     * @throws \Exception
     */
    public function installModule(string $path): void
    {
        $zip = new ZipArchive;
        $tmpModuleDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
            $zip->extractTo($tmpModuleDirFull);
            $zip->close();
//            $out->out->r('Архив распакован', 'green');
//        } else {
//            $this->out->r('Message: Ошибка чтения архива', 'red');
        } else {
            $zip->close();
            throw new \Exception("unable to open zip archive");
        }

        if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
            throw new \Exception('manifest.json not found');
        }

        $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
        $manifest = Manifest::getWithVars($manifestJson);
//            $this->out->r('manifest.json инициализирован', 'green');

        $fileHelper = new Files();
        $fileHelper->copy_folder($tmpModuleDirFull . '/app', $manifest['app_module_path']);
        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']);
        }

//            $this->out->r("Удаляем временные файлы", 'green');
        $fileHelper->recursiveRemoveDir($tmpModuleDirFull);
        var_dump($tmpModuleDirFull);

//            $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');
    }

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

        if ($this->isActive($moduleInfo['slug'])) {
            $this->toggleModule($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']);
        }
    }

    public function packModule(string $path): void
    {
        $moduleName = basename($path);

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

        $fileHelper = new Files();
        $fileHelper->copy_folder(APP_DIR . '/modules/' . $moduleName, $tmpModuleDirFull . 'app/');
        $fileHelper->copy_folder(KERNEL_APP_MODULES_DIR . '/' . $moduleName, $tmpModuleDirFull . 'kernel/');

//        $this->out->r("Модуль собран во временную папку", 'green');

        $fileHelper->pack($tmpModuleDirFull, RESOURCES_DIR . '/tmp/modules/' . $moduleName . '.itguild');

//        $this->out->r("Архив создан", 'green');
        $fileHelper->recursiveRemoveDir($tmpModuleDirFull);

//        $this->out->r("Временная папка удалена", 'green');
    }

    /**
     * @throws \Exception
     */
    public function updateModule(string $path): void
    {
        $zip = new ZipArchive;
        $tmpModuleDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpModuleDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpModuleDir . "/";
            $zip->extractTo($tmpModuleDirFull);
            $zip->close();
//            $out->out->r('Архив распакован', 'green');
//        } else {
//            $this->out->r('Message: Ошибка чтения архива', 'red');
        } else {
            $zip->close();
            throw new \Exception("unable to open zip archive");
        }

        if (!file_exists($tmpModuleDirFull . "/app/manifest.json")) {
            throw new \Exception('manifest.json not found');
        }

        $manifestJson = getConst(file_get_contents($tmpModuleDirFull . "/app/manifest.json"));
        $manifest = Manifest::getWithVars($manifestJson);
//            $this->out->r('manifest.json инициализирован', 'green');

        $fileHelper = new Files();
        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']);
        }

//            $this->out->r("Удаляем временные файлы", 'green');
        $fileHelper->recursiveRemoveDir($tmpModuleDirFull);

//            $this->out->r("Модуль " . $manifest['name'] . " установлен", 'green');

    }

}