<?php
declare(strict_types=1);

namespace mystic\forum;

use mystic\forum\utils\StaticClass;

final class Messaging {
    use StaticClass;

    const ENT_FLAGS = \ENT_COMPAT | \ENT_HTML401 | \ENT_SUBSTITUTE;

    protected static function message(array $items, string $headerText, string $headerColor, string $headerTextColor): void {
        //echo "<HTML><BODY>\n";
        echo "<TABLE cellspacing=2 cellpadding=0 bgcolor=gray><TR><TD>\n";

        echo "<TABLE width=\"100%\" cellspacing=0 cellpadding=4 bgcolor=\"".htmlentities($headerColor, self::ENT_FLAGS)."\"><TR><TD align=center>\n";
        echo "<FONT face=Arial size=3 color=\"".htmlentities($headerTextColor, self::ENT_FLAGS)."\"><B>".htmlentities($headerText, self::ENT_FLAGS)."</B></FONT>\n";
        echo "</TD></TR></TABLE>\n";
        
        echo "</TD></TR><TR><TD>\n";

        echo "<TABLE width=\"100%\" cellspacing=0 cellpadding=4 bgcolor=WHITE><TR><TD align=left><FONT size=2 face=Arial color=BLACK>\n";
        
        foreach ($items as $item) {
            if (is_scalar($item)) {
                echo "<P>" . htmlentities(strval($item), self::ENT_FLAGS) . "</P>\n";
            } elseif (is_array($item)) {
                if (count(array_keys($item)) === 2 && isset($item["___!type"]) && isset($item["___!content"])) {
                    // special item
                    switch ($item["___!type"]) {
                        case "HTML":
                            echo $item["___!content"];
                            break;
                        default:
                            echo "invalid";
                            break;
                    }
                } elseif (array_is_list($item)) {
                    echo "<UL>\n";
                    foreach ($item as $i)
                        echo "<LI>" . htmlentities($i, self::ENT_FLAGS) . "</LI>\n";
                    echo "</UL>\n";
                } else {
                    echo "<TABLE cellspacing=0 cellpadding=2 border=1>\n";
                    foreach ($item as $k => $i) {
                        echo "<TR>\n";
                        echo "<TH align=left>" . htmlentities($k, self::ENT_FLAGS) . "</TH>\n";
                        echo "<TD>";
                        if (is_scalar($i)) {
                            echo htmlentities($i, self::ENT_FLAGS);
                        } else {
                            echo gettype($i);
                        }
                        echo "</TD>";
                        echo "</TR>\n";
                    }
                    echo "</TABLE>\n";
                }
            } else {
                echo gettype($item);
            }
        }

        echo "</FONT></TD></TR></TABLE>\n";

        echo "</TD></TR></TABLE>\n";
        //echo "</BODY></HTML>\n";
    }

    public static function bold(string $contents): array {
        return self::html("<P><B>" . htmlentities($contents, self::ENT_FLAGS) . "</B></P>\n");
    }

    public static function italic(string $contents): array {
        return self::html("<P><I>" . htmlentities($contents, self::ENT_FLAGS) . "</I></P>\n");
    }

    public static function bold_italic(string $contents): array {
        return self::html("<P><B><I>" . htmlentities($contents, self::ENT_FLAGS) . "</I></B></P>\n");
    }

    public static function html(string $contents): array {
        return [
            "___!type" => "HTML",
            "___!content" => $contents,
        ];
    }

    public static function info(string|array $contents): void {
        if (is_string($contents))
            $contents = [$contents];
        self::message($contents, "INFORMATION", "SKYBLUE", "BLACK");
    }

    public static function error(string|array $contents): void {
        if (is_string($contents))
            $contents = [$contents];
        self::message($contents, "ERROR", "RED", "WHITE");
    }
}