<?php

namespace kernel\services;

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

class AdminThemeService
{
    protected array $errors = [];

    protected Option $option;
    protected string $active_theme;

    protected ModuleService $moduleService;


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

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

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


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

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

    public function setActiveAdminTheme(string $theme): void
    {
        $active_admin_theme = Option::where("key", "active_admin_theme")->first();
        $active_admin_theme->value = getConst($theme);
        $active_admin_theme->save();
    }

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

    public function getAdminThemeInfo(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 getAdminThemeInfoBySlug(string $slug): false|array|string
    {
        $dirs = $this->getAdminThemeDirs();
        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot()) continue;
                if ($this->getAdminThemeInfo($fileInfo->getPathname())['slug'] === $slug) {
                    return $this->getAdminThemeInfo($fileInfo->getPathname());
                }
            }
        }

        return false;
    }

    public function getAdminThemeDirs(): array
    {
        $adminThemePaths = Option::where("key", "admin_theme_paths")->first();
        $dirs = [];
        if ($adminThemePaths) {
            $path = json_decode($adminThemePaths->value);
            foreach ($path->paths as $p) {
                if (!is_dir(getConst($p))){
                    $old_mask = umask(0);
                    mkdir(getConst($p), permissions: 0775, recursive: true);
                    umask($old_mask);
                }
                $dirs[] = getConst($p);
            }
        }
        return $dirs;
    }

    public function isInstall(string $slug): bool
    {
        $dirs = $this->getAdminThemeDirs();
        foreach ($dirs as $dir) {
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                if ($fileInfo->isDot()) continue;
                if ($this->getAdminThemeInfo($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->getAdminThemeInfoBySlug($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/');
        $fileHelper->copy_folder(RESOURCES_DIR . '/admin_themes/' . $themeName, $tmpThemeDirFull . 'resources/');

        if (!is_dir(RESOURCES_DIR . '/tmp/admin_themes')) {
            $old_mask = umask(0);
            mkdir(RESOURCES_DIR . '/tmp/admin_themes', 0775, true);
            umask($old_mask);
        }
        $fileHelper->pack($tmpThemeDirFull, RESOURCES_DIR . '/tmp/admin_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_path'])) {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta", $manifest['theme_path']);
        } else {
            $fileHelper->copy_folder($tmpThemeDirFull . "meta", APP_DIR . '/admin_themes/' . $manifest['slug']);
        }

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

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

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

        $active_admin_theme = Option::where("key", "active_admin_theme")->first();
        if ($active_admin_theme->value === ROOT_DIR . $path) {
            $this->setActiveAdminTheme(KERNEL_ADMIN_THEMES_DIR . '/default');
        }
        $fileHelper = new Files();
        if (file_exists($path)) {
            $fileHelper->recursiveRemoveDir($path);
        }
        if (file_exists(RESOURCES_DIR . '/admin_themes/' . $themeInfo['slug'])) {
            $fileHelper->recursiveRemoveDir(RESOURCES_DIR . '/admin_themes/' . $themeInfo['slug']);
        }
    }

    public function update(string $path): bool
    {
        if ($this->install($path)) {
            return true;
        }

        return false;
    }
}