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

    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 ManageUsers($Connection, $SpecificPath);
        }

        return self::$Instance;
    }

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

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

            foreach ($Result as $UserData) {
                $User = new User($UserData['id'], $UserData['name'],
                        $UserData['email'], $UserData['date'],
                        $UserData['privileges']);

                array_push($UserList, $User);
            }

            return $UserList;
        }
    }

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

        if (isset($Result['error'])) {
            return 'General error while requesting';
        } else if (count($Result) === 0) {
            return 'User does not exist';
        } else {
            $User = new User($Result['id'], $Result['name'],
                    $Result['email'], $Result['date'],
                    $Result['privileges']);


            return $User;
        }
    }

    public function AddUser(User $User): string {
        $UserArray = [
            'id' => $User->GetId(),
            'name' => $User->GetName(),
            'email' => $User->GetEMail(),
            'date' => $User->GetDate(),
            'privileges' => $User->GetPrivileges()
        ];
        
        if ($this->Connection->AddListElement($this->SpecificPath, $UserArray)) {
            return 'User added successfully';
        } else {
            return 'User already exists';
        }
    }
    
    public function RemoveUser(string $Id): string {
        if ($this->Connection->RemoveListElement($this->SpecificPath, $Id)) {
            return 'User removed successfully';
        } else {
            return 'User does not exist';
        }
    }
}
