<?php

namespace XZD\AI\V5\Theme;

use XZD\AI\V5\Theme\Adapters\ZibllAdapter;
use XZD\AI\V5\Theme\Adapters\DefaultAdapter;

/**
 * 主题适配器注册表 — 管理适配器的注册、检测、缓存和获取
 */
class AdapterRegistry
{
    private static ?self $instance = null;

    /** @var array<string, string|AdapterInterface> */
    private array $adapters = [];

    /** @var array<string, string> 主题模板 => 适配器类名 */
    private array $mappings = [];

    /** 缓存当前适配器 */
    private ?AdapterInterface $current = null;
    private bool $detected = false;

    public static function instance(): self
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 注册一个适配器
     */
    public function register(string $themeTemplate, string $adapterClass): self
    {
        $this->mappings[$themeTemplate] = $adapterClass;
        return $this;
    }

    /**
     * 获取当前主题的适配器
     */
    public static function getCurrent(): AdapterInterface
    {
        return self::instance()->resolveCurrent();
    }

    private function resolveCurrent(): AdapterInterface
    {
        if ($this->detected && $this->current !== null) {
            return $this->current;
        }

        $theme    = wp_get_theme();
        $template = $theme->get_template() ?: $theme->get_stylesheet();

        // 注册默认适配器
        if (empty($this->mappings)) {
            $this->mappings['zibll'] = ZibllAdapter::class;
        }

        $adapterClass = $this->mappings[$template] ?? DefaultAdapter::class;

        if (!class_exists($adapterClass)) {
            $adapterClass = DefaultAdapter::class;
        }

        $this->current  = new $adapterClass();
        $this->detected = true;

        return $this->current;
    }

    /**
     * 获取当前主题信息（兼容旧 ThemeDetector 接口）
     */
    public static function detect(): array
    {
        $adapter  = self::getCurrent();
        $theme    = wp_get_theme();
        $template = $theme->get_template() ?: $theme->get_stylesheet();

        return [
            'adapter'       => $adapter instanceof ZibllAdapter ? 'zibll' : ($adapter instanceof DefaultAdapter ? 'generic' : 'custom'),
            'adapter_class' => get_class($adapter),
            'template'      => $template,
            'name'          => $adapter->getThemeName(),
            'version'       => $adapter->getThemeVersion(),
            'is_dark'       => $adapter->isDarkMode(),
            'capabilities'  => $adapter->getCapabilities(),
            'colors'        => $adapter->getThemeColors(),
            'bubble'        => $adapter->getBubblePosition(),
        ];
    }

    /**
     * 是否为 Zibll 子比主题
     */
    public static function isZibll(): bool
    {
        return self::getCurrent() instanceof ZibllAdapter;
    }

    /**
     * 已注册的适配器列表（供调试用）
     */
    public function getRegistered(): array
    {
        return array_keys($this->mappings);
    }

    /**
     * 重置缓存（主题切换后调用）
     */
    public function reset(): self
    {
        $this->current  = null;
        $this->detected = false;
        return $this;
    }
}
