<?php

namespace kernel\modules\themes\controllers;

use DirectoryIterator;
use GuzzleHttp\Exception\GuzzleException;
use JetBrains\PhpStorm\NoReturn;
use Josantonius\Session\Exceptions\HeadersSentException;
use Josantonius\Session\Exceptions\SessionNotStartedException;
use Josantonius\Session\Exceptions\SessionStartedException;
use Josantonius\Session\Exceptions\WrongSessionOptionException;
use Josantonius\Session\Facades\Session;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\Request;
use kernel\services\ModuleService;
use kernel\services\ThemeService;

class ThemeController extends AdminController
{
    public ThemeService $themeService;

    public ModuleService $moduleService;
    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_MODULES_DIR . "/themes/views/";
        $this->themeService = new ThemeService();
        $this->moduleService = new ModuleService();
    }

    public function actionIndex(): void
    {
        $themePaths = Option::where("key", "theme_paths")->first();

        $dirs = [];
        if ($themePaths){
            $path = json_decode($themePaths->value);
            foreach ($path->paths as $p){
                $dirs[] = getConst($p);
            }
        }

        $infoToTable = [];
        $meta = [];
        $meta['columns'] = [
            "preview" => "Превью",
            "name" => "Название",
            "author" => "Автор",
            "version" => "Версия",
            "description" => "Описание"
        ];
        $meta['params'] = ["class" => "table table-bordered"];
        $meta['perPage'] = 10;
        $meta['baseUrl'] = "/admin/settings/themes";
        $meta['currentPage'] = 1;

        $infoToTable['meta'] = $meta;

        $themesInfo = [];
        foreach ($dirs as $dir){
            $i = 1;
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                $info = [];
                if($fileInfo->isDot()) continue;
                $info['id'] = $i;
                $themesInfo[] = array_merge($info, $this->themeService->getThemeInfo($fileInfo->getPathname()));
                $i++;
            }
        }
        $infoToTable['meta']['total'] = count($themesInfo);
        $infoToTable['data'] = $themesInfo;

        $this->cgView->render("index.php", ['json' => json_encode($infoToTable, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)]);
    }

    /**
     * @throws HeadersSentException
     * @throws WrongSessionOptionException
     * @throws SessionStartedException
     * @throws GuzzleException
     * @throws SessionNotStartedException
     */
    #[NoReturn] public function actionActivate(): void
    {
        $request = new Request();
        if(!$this->themeService->setActiveTheme($request->get("p"))){
            $this->redirect("/admin/settings/themes/", 302);
        }

        $this->cgView->render("view.php", ['data' => $this->themeService->getThemeInfo($request->get("p"))]);
    }

}