<?php

namespace kernel\services;

use DirectoryIterator;
use Josantonius\Session\Facades\Session;
use kernel\Flash;
use kernel\helpers\Debug;
use kernel\helpers\Files;
use kernel\helpers\Manifest;
use kernel\helpers\RESTClient;
use kernel\models\Option;
use ZipArchive;

class ThemeService
{
    protected array $errors = [];

    protected Option $option;
    protected string $active_theme = "";

    protected ModuleService $moduleService;

    protected ModuleShopService $moduleShopService;


    public function __construct()
    {
        $this->option = new Option();
        $this->findActiveTheme();
        $this->moduleService = new ModuleService();
        $this->moduleShopService = new ModuleShopService();
    }

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

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

    public function findActiveTheme(): void
    {
        $model = $this->option::where("key", "active_theme")->first();
        $this->active_theme = $model->value;
    }

    public function getActiveTheme(): string
    {
        return $this->active_theme;
    }

    /**
     * @param string $theme
     * @return bool
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function setActiveTheme(string $theme): bool
    {
        $activeTheme = $this->option::where("key", "active_theme")->first();

        $themeInfo = $this->getThemeInfo(getConst($theme));
        $dependenceArray = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/get_all_dependencies/' . $themeInfo['slug']);
        $dependenceArray = json_decode($dependenceArray->getBody()->getContents(), true);

        foreach ($dependenceArray as $depend) {
            if (!$this->moduleService->isInstall($depend)) {
                if (!$this->moduleShopService->existsInModuleShop($depend)) {
                    Flash::setMessage('error', "Модуль $depend не найден в IT Guild Framework Shop.");
                    return false;
                }
            }
        }

        if (!$this->moduleService->recursiveActivateDependencies($dependenceArray)){
            return false;
        }

        $this->runDeactivateScript($this->getActiveThemeInfo());
        $this->runInitScript($themeInfo);

        $activeTheme->value = getConst($theme);
        $activeTheme->save();
        return true;
    }

    public function runInitScript($themeInfo): void
    {
        if (isset($themeInfo['theme_class'])) {
            if (isset($themeInfo['theme_class_file'])) {
                require_once $themeInfo['theme_class_file'];
            }
            $themeClass = new $themeInfo['theme_class']();
            $themeClass->init();
        }
    }

    /**
     * @param $themeInfo
     * @return void
     */
    public function runDeactivateScript($themeInfo): void
    {
        if (isset($themeInfo['theme_class'])) {
            if (isset($themeInfo['theme_class_file'])) {
                require_once $themeInfo['theme_class_file'];
            }
            $themeClass = new $themeInfo['theme_class']();
            $themeClass->deactivate();
        }
    }

    public function getActiveThemeInfo(): false|array|string
    {
        return $this->getThemeInfo($this->active_theme);
    }

    public function getThemeInfo(string $theme): false|array|string
    {
        $info = [];
        $theme = getConst($theme);
        $info['path'] = $theme;
        if (file_exists($theme . "/manifest.json")) {
            $manifest = file_get_contents($theme . "/manifest.json");
            $manifest = Manifest::getWithVars($manifest);
            $manifest['preview'] = $manifest['resource'] . "/" . $manifest['preview'];
            $info = array_merge($info, $manifest);
        }

        return $info;
    }

    public function getThemeInfoBySlug(string $slug): false|array|string
    {
        $dirs = $this->getThemeDirs();
        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot()) continue;
                if ($this->getThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
                    return $this->getThemeInfo($fileInfo->getPathname());
                }
            }
        }

        return false;
    }

    public function getThemeDirs(): array
    {
        $ThemePaths = Option::where("key", "theme_paths")->first();
        $dirs = [];
        if ($ThemePaths) {
            $path = json_decode($ThemePaths->value);
            foreach ($path->paths as $p) {
                $dirs[] = getConst($p);
            }
        }
        return $dirs;
    }

    public function isInstall(string $slug): bool
    {
        $dirs = $this->getThemeDirs();
        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot()) continue;
                if ($this->getThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
                    return true;
                }
            }
        }

        return false;
    }

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

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

            $themeInfo = $this->getThemeInfoBySlug($slug);
            foreach ($modulesInfo as $mod) {
                if ($mod['slug'] === $themeInfo['slug'] && $mod['version'] === $themeInfo['version']) {
                    return true;
                }
            }
        }

        return false;
    }

    public function pack(string $path): void
    {
        $themeName = basename($path);

        $tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $themeName . "/";

        $fileHelper = new Files();
        $fileHelper->copy_folder(ROOT_DIR . $path, $tmpThemeDirFull . 'meta/app/');
        $fileHelper->copy_folder(KERNEL_DIR . '/app_themes/' . $themeName, $tmpThemeDirFull . 'meta/kernel/');
        $fileHelper->copy_folder(RESOURCES_DIR . '/themes/' . $themeName, $tmpThemeDirFull . 'resources/');

        if (!is_dir(RESOURCES_DIR . '/tmp/themes')) {
            $old_mask = umask(0);
            mkdir(RESOURCES_DIR . '/tmp/themes', 0775, true);
            umask($old_mask);
        }
        $fileHelper->pack($tmpThemeDirFull, RESOURCES_DIR . '/tmp/themes/' . $themeName . '.igt');
    }

    public function install(string $path): bool
    {
        $zip = new ZipArchive;
        $tmpThemeDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpThemeDir . "/";
            $zip->extractTo($tmpThemeDirFull);
            $zip->close();
        } else {
            $this->addError('unable to open zip archive');
            return false;
        }

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

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

        $fileHelper = new Files();
        if (isset($manifest['theme_app_path'])) {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/app", $manifest['theme_app_path']);
        } else {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/app", APP_DIR . '/themes/' . $manifest['slug']);
        }

        if (isset($manifest['theme_kernel_path'])) {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", $manifest['theme_app_path']);
        } else {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", APP_DIR . '/themes/' . $manifest['slug']);
        }

        if (isset($manifest['resource_path'])) {
            $fileHelper->copy_folder($tmpThemeDirFull . "resources", $manifest['resource_path']);
        } else {
            $fileHelper->copy_folder($tmpThemeDirFull . "resources", RESOURCES_DIR . '/themes/' . $manifest['slug']);
        }

        $fileHelper->recursiveRemoveDir($tmpThemeDirFull);
        unlink(ROOT_DIR . $path);
        return true;
    }

    public function uninstall(string $path): void
    {
        $themeInfo = $this->getThemeInfo(APP_DIR . '/themes/' . basename($path));

        $active_theme = $this->option::where("key", "active_theme")->first();
        if ($active_theme->value === ROOT_DIR . $path) {
            $this->setActiveTheme(KERNEL_DIR . '/themes/default');
        }
        $fileHelper = new Files();
        if (file_exists($path)) {
            $fileHelper->recursiveRemoveDir($path);
        }
        if (file_exists(KERNEL_DIR . '/app_themes/' . $themeInfo['slug'])) {
            $fileHelper->recursiveRemoveDir(KERNEL_DIR . '/app_themes/' . $themeInfo['slug']);
        }
        if (file_exists(RESOURCES_DIR . '/themes/' . $themeInfo['slug'])) {
            $fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/themes/' . $themeInfo['slug']);
        }
    }

    public function update(string $path): bool
    {
        $zip = new ZipArchive;
        $tmpThemeDir = md5(time());
        $res = $zip->open(ROOT_DIR . $path);
        if ($res === TRUE) {
            $tmpThemeDirFull = RESOURCES_DIR . '/tmp/ad/' . $tmpThemeDir . "/";
            $zip->extractTo($tmpThemeDirFull);
            $zip->close();
        } else {
            $this->addError('unable to open zip archive');
            return false;
        }

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

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

        $fileHelper = new Files();
        if (isset($manifest['theme_kernel_path'])) {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", $manifest['theme_app_path']);
        } else {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta/kernel", APP_DIR . '/themes/' . $manifest['slug']);
        }

        $fileHelper->recursiveRemoveDir($tmpThemeDirFull);
        unlink(ROOT_DIR . $path);
        return true;
    }

    public function getThemeRout(string $path)
    {
        if (file_exists($path . "/manifest.json")){
            $manifest = json_decode(file_get_contents($path . "/manifest.json"), true);
            if ($manifest['routs']) {
                return $manifest['routs'];
            }
        }

        return false;
    }

}