<?php

/*
 * Copyright (C) 2021 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 Post {

    private string $Id;
    private string $UserId;
    private string $Title;
    private string $Date;
    private string $Image;
    private string $Content;
    private array $Comments;

    public function __construct(string $Id, string $UserId, string $Title, string $Date, string $Image, string $Content, array $Comments) {
        $this->Id = $Id;
        $this->UserId = $UserId;
        $this->Title = $Title;
        $this->Date = $Date;
        $this->Image = $Image;
        $this->Content = $Content;
        $this->Comments = $Comments;
    }

    public function GetId(): string {
        return $this->Id;
    }

    public function GetUserId(): string {
        return $this->Id;
    }

    public function GetTitle(): string {
        return $this->Title;
    }

    public function GetDate(): string {
        return $this->Date;
    }

    public function GetImage(): string {
        return $this->Image;
    }

    public function GetContent(): string {
        return $this->Content;
    }

    public function GetComments(): array {
        return $this->Comments;
    }

    public function SetId(string $Id): void {
        $this->Id = $Id;
    }

    public function SetUserId(string $Id): void {
        $this->Id = $Id;
    }

    public function SetTitle(string $Title): void {
        $this->Title = $Title;
    }

    function SetDate(string $Date): void {
        $this->Date = $Date;
    }

    public function SetImage(string $Image): void {
        $this->Image = $Image;
    }

    public function SetContent(string $Content): void {
        $this->Content = $Content;
    }

    public function SetComments(array $Comments): void {
        $this->Comments = $Comments;
    }

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

        $HTML .= '<div>' . PHP_EOL;
        $HTML .= '<strong><a href=".?post=' . $this->Id . '">' . $this->Title . '</a></strong>' . PHP_EOL;
        $HTML .= '<span>' . $this->Date . '</span>' . PHP_EOL;
        $HTML .= '<div>' . PHP_EOL;
        $HTML .= substr($this->Content, 0, 60) . ' ...' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        return $HTML;
    }

    public function RenderComplete(string $UserName, array $UserNamesList, array $CommentsList): string {
        $HTML = '';

        $HTML .= '<div>' . PHP_EOL;
        $HTML .= '<h2>' . $this->Title . '</h2>' . PHP_EOL;
        $HTML .= '<span><a href=".?user=' . $this->UserId . '">' . $UserName . '</a></span>' . PHP_EOL;
        $HTML .= '<span>' . $this->Date . '</span>' . PHP_EOL;
        $HTML .= '<img src="' . $this->Image . '" alt="Post image"/>' . PHP_EOL;
        $HTML .= '<div>' . PHP_EOL;
        $HTML .= $this->Content . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '<div>' . PHP_EOL;
        $HTML .= '<h3>Comments</h3>' . PHP_EOL;

        if (count($CommentsList) !== 0) {
            for ($Position = 0; $Position < count($CommentsList); $Position++) {
                $Comment = $CommentsList[$Position];
                $UserName = $UserNamesList[$Position];

                $HTML .= $Comment->Render($UserName);
            }
        } else {
            $HTML .= '<span>' . $CommentsList .  '</span>'. PHP_EOL;
        }

        $HTML .= '</div>' . PHP_EOL;
        $HTML .= '</div>' . PHP_EOL;

        return $HTML;
    }

}
