<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Aug 14, 2018)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 * ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\Http\Admin;

use ModulesGarden\ProxmoxAddon\App\Services\RecoveryVmDumpService;
use ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\Subscription;
use ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\Summary;
use ModulesGarden\ProxmoxAddon\Core\Helper;
use ModulesGarden\ProxmoxAddon\Core\Http\AbstractController;
use Symfony\Component\HttpFoundation\StreamedResponse;

/**
 * Example admin home page controler
 */
class Home extends AbstractController
{

    /**
     * Example of static page
     * @return type
     */
    public function index()
    {
        return Helper\redirect('home', 'servers');
    }

    public function servers()
    {
        return Helper\view()->addElement(\ModulesGarden\ProxmoxAddon\App\UI\Servers\Pages\ServersDataTable::class);
    }

    public function vms()
    {
        return Helper\view()->addElement(\ModulesGarden\ProxmoxAddon\App\UI\Vms\Pages\VmsDataTable::class);
    }

    public function recoveryVms()
    {
        return Helper\view()->addElement(\ModulesGarden\ProxmoxAddon\App\UI\RecoveryVms\Pages\RecoveryVmsDataTable::class);
    }

    public function recoveryToFile()
    {
        $response = new StreamedResponse();
        $response->setStatusCode(200);
        $response->headers->set('Content-Type', 'application/x-pem-file; charset=utf-8');
        $response->setCallback(function ()
        {
            echo (new RecoveryVmDumpService())->generate();
        });
        $response->headers->set(
            'Content-Disposition', 'attachment; filename="recover_vm_list_' . date("Y-m-d H:i:s") . '.txt"'
        );
        $response->send();
    }

    public function taskHistory()
    {
        return Helper\view()->addElement(\ModulesGarden\ProxmoxAddon\App\UI\TaskHistory\Pages\TaskHistoryDataTable::class);
    }

    public function serverDetail()
    {
        return Helper\view()->addElement(\ModulesGarden\ProxmoxAddon\App\UI\ServerDetail\Pages\ServerDetailContainer::class);
    }

    public function nodeDetail()
    {
        return Helper\view()->setCustomJsCode()
            ->addElement(Summary::class)
            ->addElement(Subscription::class)
            ->addElement(\ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\CpuGraph::class)
            ->addElement(\ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\ServerLoadGraph::class)
            ->addElement(\ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\MemoryGraph::class)
            ->addElement(\ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages\NetworkGraph::class);
    }

}
