<?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 ManageComments {

    private static $Instance = null;
    private FirebaseCon $Connection;
    private string $SpecificPath;

    private function __construct(FirebaseCon $Connection, string $SpecificPath) {
        $this->Connection = $Connection;
        $this->SpecificPath = $SpecificPath;
    }

    public static function GetInstance(FirebaseCon $Connection, string $SpecificPath) {
        if (self::$Instance === null) {
            self::$Instance = new ManageComments($Connection, $SpecificPath);
        }

        return self::$Instance;
    }

    public function GetComments() {
        $Result = $this->Connection->GetList($this->SpecificPath);

        if (isset($Result['error'])) {
            return 'General error while requesting';
        } else if (count($Result) === 0) {
            return 'No comments found';
        } else {
            $CommentsList = [];

            foreach ($Result as $CommentData) {
                $Comment = new Comment($CommentData['id'], $CommentData['user_id'],
                        $CommentData['summary'], $CommentData['date'],
                        $CommentData['content']);

                array_push($CommentsList, $Comment);
            }

            return $CommentsList;
        }
    }

    public function GetComment(string $Id) {
        $Result = $this->Connection->GetListElement($this->SpecificPath, $Id);

        if (isset($Result['error'])) {
            return 'General error while requesting';
        } else if (count($Result) === 0) {
            return 'Comment does not exist';
        } else {
            $Comment = new Comment($Result['id'], $Result['user_id'],
                    $Result['summary'], $Result['date'],
                    $Result['content']);


            return $Comment;
        }
    }

    public function AddComment(Comment $Comment): string {
        $CommentArray = [
            'id' => $Comment->GetId(),
            'user_id' => $Comment->GetUserId(),
            'summary' => $Comment->GetSummary(),
            'date' => $Comment->GetDate(),
            'content' => $Comment->GetContent()
        ];

        if ($this->Connection->AddListElement($this->SpecificPath, $CommentArray)) {
            return 'Comment added successfully';
        } else {
            return 'Comment already exists';
        }
    }

    public function RemoveComment(string $Id): string {
        if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
            return 'Comment removed successfully';
        } else {
            return 'Comment does not exist';
        }
    }

}
