<?php

namespace lib\Core;

use eftec\bladeone\BladeOne;

class View
{
    private array $_paths = [];
    private array $_values = [];
    private string $_layout = 'default_layout.php';
    private ?BladeOne $_blade = null;
    public function __construct()
    {
        if (!is_dir(APP_VIEW_PATH)) {
            mkdir(APP_VIEW_PATH, APP_VIEW_PATH_PERMISSION, true);
        }
        if (!is_dir(APP_VIEW_CACHE_PATH)) {
            mkdir(APP_VIEW_CACHE_PATH, APP_VIEW_CACHE_PATH_PERMISSION, true);
        }
        // BladeOne 객체 초기화
        $this->_blade = new BladeOne(APP_VIEW_PATH, APP_VIEW_CACHE_PATH, BladeOne::MODE_DEBUG);
        $this->_blade->setFileExtension('.php');
    }

    public function __set(string $name, mixed $value): void
    {
        $this->_values[$name] = $value;
    }

    public function __get(string $name): mixed
    {
        return $this->_values[$name] ?? null;
    }

    public function setLayout(string $layout): void
    {
        $this->_layout = $layout;
    }

    public function setPath(string $path): void
    {
        $this->_paths[] = $path;
    }
    public function render($file): string
    {
        // Blade 템플릿 렌더링
        $file = implode('.', $this->_paths) . '.' . $file;
        if (APP_VIEW_DEBUG) {
            echo "<pre>VIwe DEBUG:" . __METHOD__ . "에서 [" . $file . "]를 렌더링합니다.</pre>";
        }
        return $this->_blade->run($file, $this->_values);
    }
}
