<?php
namespace Misuzu\Comments;

use Index\DateTime;
use Index\Data\IDbResult;

class CommentsPostInfo {
    private string $id;
    private string $categoryId;
    private ?string $userId;
    private ?string $replyingTo;
    private string $body;
    private int $created;
    private ?int $pinned;
    private ?int $updated;
    private ?int $deleted;
    private int $replies;
    private int $votesTotal;
    private int $votesPositive;
    private int $votesNegative;

    public function __construct(
        IDbResult $result,
        bool $includeRepliesCount = false,
        bool $includeVotesCount = false
    ) {
        $args = 0;
        $this->id = (string)$result->getInteger($args);
        $this->categoryId = (string)$result->getInteger(++$args);
        $this->userId = $result->isNull(++$args) ? null : (string)$result->getInteger($args);
        $this->replyingTo = $result->isNull(++$args) ? null : (string)$result->getInteger($args);
        $this->body = $result->getString(++$args);
        $this->created = $result->getInteger(++$args);
        $this->pinned = $result->isNull(++$args) ? null : $result->getInteger($args);
        $this->updated = $result->isNull(++$args) ? null : $result->getInteger($args);
        $this->deleted = $result->isNull(++$args) ? null : $result->getInteger($args);

        $this->replies = $includeRepliesCount ? $result->getInteger(++$args) : 0;

        if($includeVotesCount) {
            $this->votesTotal = $result->getInteger(++$args);
            $this->votesPositive = $result->getInteger(++$args);
            $this->votesNegative = $result->getInteger(++$args);
        } else {
            $this->votesTotal = 0;
            $this->votesPositive = 0;
            $this->votesNegative = 0;
        }
    }

    public function getId(): string {
        return $this->id;
    }

    public function getCategoryId(): string {
        return $this->categoryId;
    }

    public function hasUserId(): bool {
        return $this->userId !== null;
    }

    public function getUserId(): ?string {
        return $this->userId;
    }

    public function isReply(): bool {
        return $this->replyingTo !== null;
    }

    public function getReplyingTo(): ?string {
        return $this->replyingTo;
    }

    public function getBody(): string {
        return $this->body;
    }

    public function getCreatedTime(): int {
        return $this->created;
    }

    public function getCreatedAt(): DateTime {
        return DateTime::fromUnixTimeSeconds($this->created);
    }

    public function getPinnedTime(): ?int {
        return $this->pinned;
    }

    public function getPinnedAt(): DateTime {
        return $this->pinned === null ? null : DateTime::fromUnixTimeSeconds($this->pinned);
    }

    public function isPinned(): bool {
        return $this->pinned !== null;
    }

    public function getUpdatedTime(): ?int {
        return $this->updated;
    }

    public function getUpdatedAt(): DateTime {
        return $this->updated === null ? null : DateTime::fromUnixTimeSeconds($this->updated);
    }

    public function isEdited(): bool {
        return $this->updated !== null;
    }

    public function getDeletedTime(): ?int {
        return $this->deleted;
    }

    public function getDeletedAt(): DateTime {
        return $this->deleted === null ? null : DateTime::fromUnixTimeSeconds($this->deleted);
    }

    public function isDeleted(): bool {
        return $this->deleted !== null;
    }

    public function hasRepliesCount(): bool {
        return $this->replies > 0;
    }

    public function getRepliesCount(): int {
        return $this->replies;
    }

    public function hasVotesCount(): bool {
        return $this->votesTotal > 0;
    }

    public function getVotesTotal(): int {
        return $this->votesTotal;
    }

    public function getVotesPositive(): int {
        return $this->votesPositive;
    }

    public function getVotesNegative(): int {
        return $this->votesNegative;
    }
}
