<?php

namespace kernel\services;

use kernel\models\Option;

class AdminThemeService
{
    protected Option $option;
    protected string $active_theme;


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

    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 = $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 = [];
        $info['path'] = $theme;
        if (file_exists($theme . "/manifest.json")){
            $manifest = file_get_contents($theme . "/manifest.json");
            $manifest = json_decode($manifest, true);
            $manifest['preview'] = $manifest['resource'] . "/" . $manifest['preview'];
            $info = array_merge($info, $manifest);
        }

        return $info;
    }

}