<?php

/*
 * Copyright (C) 2022 Echedey López Romero <elr@disroot.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

class ManageBlog {

    private static $Instance = null;
    private string $Title;
    private ManageUsers $ManageUsers;
    private ManageComments $ManageComments;
    private ManagePosts $ManagePosts;

    function __construct(string $Title, ManageUsers $ManageUsers, ManageComments $ManageComments, ManagePosts $ManagePosts) {
        $this->Title = $Title;
        $this->ManageUsers = $ManageUsers;
        $this->ManageComments = $ManageComments;
        $this->ManagePosts = $ManagePosts;
    }

    public static function GetInstance(string $Title, ManageUsers $ManageUsers, ManageComments $ManageComments, ManagePosts $ManagePosts) {
        if (self::$Instance === null) {
            self::$Instance = new ManageBlog($Title, $ManageUsers, $ManageComments, $ManagePosts);
        }

        return self::$Instance;
    }

    public function ShowPosts(): string {
        $HTML = '';

        $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;

        $Posts = $this->ManagePosts->GetPosts();

        if (gettype($Posts) === 'array') {
            foreach ($Posts as $Post) {
                $HTML .= $Post->RenderCard();
            }
        } else {
            $HTML .= $Posts;
        }

        return $HTML;
    }

    public function ShowPost(string $Id): string {
        $HTML = '';

        $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;

        $Post = $this->ManagePosts->GetPost($Id);

        if (gettype($Post) === 'object') {

            $UserName = $this->ManageUsers->GetUser($Post->GetUserId())->GetName();
            $CommentIdsList = $Post->GetComments();
            $CommentsList = [];
            $UserNamesList = [];

            foreach ($CommentIdsList as $CommentId) {
                $Comment = $this->ManageComments->GetComment($CommentId);
                $UserNameComment = $this->ManageUsers->GetUser($Comment->GetUserId())->GetName();
                
                array_push($CommentsList, $Comment);
                array_push($UserNamesList, $UserNameComment);
            }

            $HTML .= $Post->RenderComplete($UserName, $UserNamesList,
                    $CommentsList);
        } else {
            $HTML .= $Post;
        }

        return $HTML;
    }

    public function ShowUser(string $Id): string {
        $HTML = '';

        $HTML .= '<h1>' . $this->Title . '</h1>' . PHP_EOL;

        $User = $this->ManageUsers->GetUser($Id);

        if (gettype($User) === 'object') {
            $HTML .= $User->Render();
        } else {
            $HTML .= $User;
        }

        return $HTML;
    }

}
