<?php

namespace kernel\controllers;

use DirectoryIterator;
use Josantonius\Session\Facades\Session;
use kernel\AdminController;
use kernel\helpers\Debug;
use kernel\models\Option;
use kernel\modules\user\service\UserService;
use kernel\Request;
use kernel\services\ModuleService;

class ModuleController extends AdminController
{
    protected ModuleService $moduleService;

    protected function init(): void
    {
        parent::init();
        $this->cgView->viewPath = KERNEL_DIR . "/views/module/";

        $this->moduleService = new ModuleService();
    }

    public function actionIndex($page_number = 1): void
    {
        $per_page = 8;
        $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);
            }
        }

        $modules_info = [];
        $i = 1;
        foreach ($dirs as $dir){
            foreach (new DirectoryIterator($dir) as $fileInfo) {
                $info = [];
                if($fileInfo->isDot()) continue;
                $mi = $this->moduleService->getModuleInfo($fileInfo->getPathname());
                if (isset($mi['show_in_admin'])){
                    if ($mi['show_in_admin'] == 0){
                        continue;
                    }
                }
                $info['id'] = $i;
                $modules_info[] = array_merge($info, $mi);
                $i++;
            }
        }
        $module_count = count($modules_info);
        $modules_info = array_slice($modules_info, $per_page*($page_number-1), $per_page);
        $this->cgView->render("index.php", [
            'modules_info' => $modules_info,
            'moduleService' => $this->moduleService,
            'page_number' => $page_number,
            'module_count' => $module_count,
            'per_page' => $per_page,
        ]);
    }

    public function actionActivate(): void
    {
        $request = new Request();
        $active_res = $this->moduleService->setActiveModule($request->get("slug"));
        if (!$active_res){
            Session::start();
            Session::set("error", implode(";", $this->moduleService->getErrors()));
            $this->redirect("/admin", 302);
        }
        $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));

        $this->cgView->render("view.php", ['data' => $mod_info]);
    }

    public function actionDeactivate(): void
    {
        $request = new Request();
        $active_res = $this->moduleService->deactivateModule($request->get("slug"));
        if (!$active_res){
            Session::start();
            Session::set("error", implode(";", $this->moduleService->getErrors()));
            $this->redirect("/admin", 302);
        }
        $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));

        $this->cgView->render("view.php", ['data' => $mod_info]);
    }

    public function actionView(): void
    {
        $request = new Request();
        $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));

        $this->cgView->render("view.php", ['data' => $mod_info]);
    }

    public function actionUpdate(): void
    {
        $request = new Request();
        $active_res = $this->moduleService->updateModule($request->get("slug"));
        if (!$active_res){
            Session::start();
            Session::set("error", implode(";", $this->moduleService->getErrors()));
            $this->redirect("/admin", 302);
        }
        $mod_info = $this->moduleService->getModuleInfoBySlug($request->get('slug'));

        $this->cgView->render("view.php", ['data' => $mod_info]);
    }

}