<?php
declare(strict_types=1);

namespace Archive\StreamWrapper\Wrappers;

use App\Framework\Config\ConfigManager;
use App\Framework\StreamWrapper\Helper\StreamWrapperHelper;
use Archive\StreamWrapper\Context\StreamContext;
use Archive\StreamWrapper\StreamWrapperInterface;

/**
 * Stream-Wrapper für das Config-System
 * Syntax: config://source/key.path
 */
class ConfigStreamWrapper implements StreamWrapperInterface
{
    public $context;
    private string $content = '';
    private int $position = 0;
    private string $mode = 'r';
    private array $parsedUrl = [];
    private ?StreamContext $streamContext = null;

    private ConfigManager $config;
    private string $configKey;
    private string $source;

    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
    {
        $this->streamContext = StreamWrapperHelper::initializeContext($this->context);
        $this->parsedUrl = StreamWrapperHelper::parseUrl($path);
        $this->mode = $mode;

        $this->source = $this->parsedUrl['host'] ?: 'default';
        $this->configKey = $this->parsedUrl['path'];

        // Config-Manager initialisieren
        $this->config = new ConfigManager();

        // Konfigurationswert laden
        if (str_contains($mode, 'r') || str_contains($mode, '+')) {
            $this->content = $this->loadContent($this->configKey);
        }

        return true;
    }

    public function stream_read(int $count): string|false
    {
        return StreamWrapperHelper::streamRead($this->content, $this->position, $count);
    }

    public function stream_write(string $data): int|false
    {
        return StreamWrapperHelper::streamWrite($this->content, $this->position, $data);
    }

    public function stream_tell(): int|false
    {
        return $this->position;
    }

    public function stream_eof(): bool
    {
        return $this->position >= strlen($this->content);
    }

    public function stream_seek(int $offset, int $whence = SEEK_SET): bool
    {
        return StreamWrapperHelper::streamSeek($this->content, $this->position, $offset, $whence);
    }

    public function stream_close(): void
    {
        $this->content = '';
        $this->position = 0;
    }

    public function stream_stat(): array|false
    {
        return StreamWrapperHelper::streamStat($this->content);
    }

    public function url_stat(string $path, int $flags): array|false
    {
        $parsedUrl = StreamWrapperHelper::parseUrl($path);
        $source = $parsedUrl['host'] ?: 'default';
        $configKey = $parsedUrl['path'];

        $config = new ConfigManager();

        if (!$config->has($configKey)) {
            return false;
        }

        $value = $config->get($configKey);
        $content = is_string($value) ? $value : json_encode($value);
        $size = strlen($content);

        return StreamWrapperHelper::createDefaultStat($size);
    }

    // Nicht unterstützte Operationen
    public function mkdir(string $path, int $mode, int $options): bool { return false; }
    public function rmdir(string $path, int $options): bool { return false; }
    public function dir_opendir(string $path, int $options): bool { return false; }
    public function dir_readdir(): string|false { return false; }
    public function dir_rewinddir(): bool { return false; }
    public function dir_closedir(): bool { return false; }
    public function rename(string $path_from, string $path_to): bool { return false; }
    public function unlink(string $path): bool { return false; }

    private function loadContent(string $path): string
    {
        $value = $this->config->get($path);

        if ($value === null) {
            return '';
        }

        return is_string($value) ? $value : json_encode($value);
    }
}
