<?php

namespace Shm\ShmChat;

use Shm\Shm;
use Shm\ShmChat\SchemaChat\ShmChannelMembers;
use Shm\ShmChat\SchemaChat\ShmChannels;
use Shm\ShmChat\ShmChatBlueprints\ShmChatBlueprintsClientRPC;
use Shm\ShmDB\mDB;
use Shm\ShmTypes\BaseType;
use Shm\ShmTypes\StructureType;
use Shm\ShmUtils\ShmInit;
use Shm\ShmUtils\ShmTwig;

class ShmChat
{

    private static function url($path = "")
    {


        $scheme =  (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : 'http';

        $host = $_SERVER['HTTP_HOST'] ?? "";

        $currentURL = $scheme . '://' . $host;

        return $currentURL . $path;
    }


    private static function html()
    {


        $apiUrl =  self::url() . $_SERVER['REQUEST_URI'];


        $url = self::url();



        $js =  $url . "/static/chat.js?shm=" . ShmInit::$shmVersionHash;
        $css = $url . "/static/chat.css?shm=" . ShmInit::$shmVersionHash;

        return ShmTwig::render('@shm/chat', [
            'js' => $js,
            'css' => $css,
        ]);
    }

    public static  function rpc(array $authStructures = [])
    {

        if (!isset($_GET['schema']) && $_SERVER['REQUEST_METHOD'] === 'GET') {
            header_remove("X-Frame-Options");
            $html = self::html();

            header('Content-Type: text/html; charset=utf-8');
            echo $html;
            exit;
        }





        ShmChatBlueprintsClientRPC::init($authStructures);
    }
}