<?php

namespace Korkz\Demirteknoloji\Controllers\Home;

use Exception;
use Korkz\Demirteknoloji\Models\Home\AboutModel;
use Korkz\Demirteknoloji\Router\View;
use Korkz\Demirteknoloji\traits\Lang;
use Korkz\Demirteknoloji\traits\TokenConfig;

class AboutController extends AboutModel
{
    use TokenConfig;
    use Lang;

    /**
     * @param $page
     * @return void
     * @throws Exception
     */
    public function show($page): void
    {
        $baseUrl = $this->baseUrl();
        $lang = $this->getLang();
        print $this->views(["baseUrl" => $baseUrl, "token" => $this->tokenCreate(), "lang" => $this->getLang(), "data" => ["settings" => $this->settingData($lang), "head" => '', "footer" => '', "page" => $page]]);

    }

    private function views(array $dataBinds = []): array|string
    {

        ob_start();
        $lang = "";
        if (!empty($dataBinds)) {
            foreach ($dataBinds as $key => $dataBind) {
                View::bind($key, $dataBind);
                if (str_starts_with($key, "lang")) {
                    $lang = $dataBind;
                }
            }
        }
        View::render("home/about", ["header" => "home/layout/header", "footer" => "home/layout/footer"]);
        $content = ob_get_clean();
        return $this->langViews("home", "about", $content, $lang);
    }

    /**
     * @param string $langActive
     * @return array
     * @throws Exception
     */
    private function settingData(string $langActive): array
    {
        $settings = $this->getData("settings", ["*"], ["Lang" => $langActive]);
        if ($settings) {
            return $settings[0];
        } else {
            return [];
        }
    }


}