<?php
/**
 * @controller Dashboard
 */

class Dashboard_Controller extends MVC_Controller
{
    public function index()
    {
        if(!$this->session->has("logged"))
            $this->header->redirect(site_url);
        else
            set_template("dashboard");

        $page = ($this->sanitize->string($this->url->segment(3)) ?: "default");
        
        if(!$this->sanitize->isValidToken($_GET))
            $this->header->redirect(($page == "default" ? site_url("dashboard") : site_url("dashboard/{$page}")));

        if(!$this->smarty->templateExists(template . "/pages/{$page}.tpl"))
            $this->header->redirect(site_url("dashboard"));

        $this->cache->container("system.settings");

        if($this->cache->empty()):
            $this->cache->setArray($this->system->getSystemSettings());
        endif;

        set_system($this->cache->getAll());

        set_logged($this->session->get("logged"));

        $this->cache->container("system.language." . logged_language);

        if($this->cache->empty()):
            $this->cache->setRaw($this->system->getTranslations(logged_language));
        endif;

        set_language($this->cache->getRaw());

        $this->cache->container("system.blocks");

        if($this->cache->empty()):
            foreach($this->system->getBlocks() as $key => $value):
                $blocks[$key] = $this->smarty->fetch("string: {$this->sanitize->htmlDecode($value)}");
            endforeach;
            $this->cache->setArray($blocks);
        endif;

        set_blocks($this->cache->getAll());

        switch($page){
            case "messages":
                $vars = [
                    "title" => lang_dashboard_title_messages
                ];

                break;
            case "contacts":
                $vars = [
                    "title" => lang_dashboard_title_contacts
                ];

                break;
            case "devices":  
                $vars = [
                    "title" => lang_dashboard_title_devices
                ];

                break;
            case "tools": 
                $vars = [
                    "title" => lang_dashboard_title_tools
                ];

                break;
            case "administration": 
                if(!logged_admin)
                    $this->header->redirect(site_url("dashboard"));

                $vars = [
                    "title" => lang_dashboard_title_admin
                ];

                break;
            default:
                $this->cache->container("user." . logged_hash);

                if(!$this->cache->has("total")):
                    $this->cache->set("total", [
                        "sent" => number_format($this->system->getTotalSent(logged_id)),
                        "received" => number_format($this->system->getTotalReceived(logged_id))
                    ]);
                endif;

                if(!$this->cache->has("recent")):
                    $this->cache->set("recent", [
                        "sent" => $this->system->getRecentSent(logged_id, 5),
                        "received" => $this->system->getRecentReceived(logged_id, 5)
                    ]);
                endif;

                if(!$this->cache->has("subscription")):
                    $this->cache->set("subscription", [
                        "used" => [
                            "sent" => $this->system->getSentToday(logged_id),
                            "received" => $this->system->getReceivedToday(logged_id),
                            "devices" => $this->system->getTotalDevices(logged_id),
                            "keys" => $this->system->getTotalKeys(logged_id),
                            "webhooks" => $this->system->getTotalWebhooks(logged_id)
                        ],
                        "package" => ($this->system->getSubscriptionByUID(logged_id) ?: $this->system->getPackageDefault())
                    ]);
                endif;

                $vars = [
                    "title" => lang_dashboard_title_default,
                    "data" => [
                        "total" => $this->cache->get("total"),
                        "recent" => $this->cache->get("recent"),
                        "subscription" => $this->cache->get("subscription")
                    ]
                ];
        }

        $vars["page"] = $page;

        $this->smarty->assign($vars);
        $this->smarty->display(template . "/header.tpl");
        $this->smarty->display(template . "/pages/{$page}.tpl");
        $this->smarty->display(template . "/footer.tpl");
    }
}
