<?php

namespace App\Controllers;
use Config\Database;
use CodeIgniter\RESTful\ResourceController;

class TransferController extends ResourceController
{
    /*
        Created: 2025-02-27
        Author: Kārlis Grīnvalds
        Last edited: 2025-12-04
        Edited by: Kārlis Grīnvalds
    */

    // Used for viewing certain HTML files on the website

    // Shows the index page
    public function Index() {
        return view("index.html");
    }

    // Shows the admin panel page, parameters needs topic name
    public function AdminPanel($name = null) {
        $db = Database::connect();
        // Checks if the name is given
        if (empty($name)) {
            return view("errors/error404.html");
        }
        // Checks if topic exists
        $query = $db->query('CALL get_topic_by_name('.$db->escape($name).')')->getResult();
        if (empty($query)) {
            return view("errors/error404.html");
        }
        $session = service("session");
        if ($session->has("userdata")) {
            $session->close();
            return view("admin_panel.html");
        }
        $session->close();
        return view("errors/error403.html");
    }

    // Topic page
    public function ViewTopic($name = null) {
        $db = Database::connect();
        // Checks if the name is given
        if (empty($name)) {
            return view("errors/error404.html");
        }
        // Checks if topic exists
        $query = $db->query('CALL get_topic_by_name('.$db->escape($name).')')->getResult();
        if (empty($query)) {
            return view("errors/error404.html");
        }
        $info = explode("/topic", $_SERVER["REQUEST_URI"])[1];
        if (strlen($info) > 0) {
            if ($info == "?a=edit") {
                // User must have an open session to view this page
                if ($this->CheckSession()) {
                    return view("topic.html");
                }
                return view("errors/error403.html");
            }
            return view("errors/error404.html");
        }
        return view("topic.html");
    }

    // Entry page
    public function ViewEntry() {
        $info = explode("/entry?a=", $_SERVER["REQUEST_URI"])[1];
        if (strlen($info) > 0) {
            if ($info == "create") {
                if ($this->CheckSession()) {
                    return view("entry.html");
                }
                return view("errors/error403.html");
            }
            else {
                $action = explode("&", $info)[0];
                $id = explode("&i=", $info)[1];
                $db = Database::connect();
                $query = $db->query('CALL get_entry('.$db->escape($id).')')->getResult();
                if (empty($query)) {
                    return view("errors/error404.html");
                }
                switch ($action) {
                    case "view":
                        return view("entry.html");
                        break;
                    case "edit":
                        if ($this->CheckSession()) {
                            return view("entry.html");
                        }
                        return view("errors/error403.html");
                        break;
                    default:
                        return view("errors/error404.html");
                        break;
                }
            }
        }
        else {
            return view("errors/error404.html");
        }
    }

    // Create account page
    public function ViewCreateAccountPage() {
        // User must not have an open session to view this page
        if ($this->CheckSession()) {
            return view("errors/error403.html");
        }
        return view("create_account.html");
    }

    // Log in page
    public function ViewLogInPage() {
        // User must not have an open session to view this page
        if ($this->CheckSession()) {
            return view("errors/error403.html");
        }
        return view("log_in.html");
    }

    // Search result page
    public function SearchPage() {
        return view("search.html");
    }

    // File upload page
    public function FileUploadPage() {
        // User must have an open session to view this page
        if ($this->CheckSession()) {
            return view("file_upload.html");
        }
        return view("errors/error403.html");
    }

    // File view page
    public function FileViewPage() {
        return view("view_file.html");
    }

    // File edit page
    public function FileEditPage() {
        return view("edit_file.html");
    }

    // Error page, parameters needs error number
    public function ErrorPage($number = null) {
        if (is_null($number)) {
            return view("errors/error404.html");
        }
        else {
            return view("errors/error".$number.".html");
        }
    }

    // Checks if user has an open session and returns a boolean
    function CheckSession() {
        $session = service("session");
        $hasSession = false;
        if ($session->has("userdata")) {
            $hasSession = true;
        }
        $session->close();
        return $hasSession;
    }
}