<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.03.19)
* *
*
*  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\Servers\ProxmoxVps\App\UI\Home\Pages;


use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\UrlService;
use ModulesGarden\Servers\ProxmoxVps\App\Helpers\UrlServiceHelper;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\MigrateButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\RebootButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\RedirectButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\ShutdownButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\StartButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\Home\Buttons\StopButton;
use ModulesGarden\Servers\ProxmoxVps\Core\Helper\BuildUrl;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Builder\BaseContainer;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\isAdmin;


class ServiceActions extends BaseContainer implements ClientArea, AdminArea
{
    use ProductService;
    use ApiService;

    public function initContent()
    {
        $this->initIds('serviceActions');
        //Start
        if (isAdmin() || $this->configuration()->isPermissionStart())
        {
            $this->addButton(new StartButton("startButton"));
        }
        //Reboot
        if (isAdmin() || $this->configuration()->isPermissionReboot())
        {
            $this->addButton(new RebootButton("rebootButon"));
        }
        //Stop
        if (isAdmin() || $this->configuration()->isPermissionStop())
        {
            $this->addButton(new StopButton("stopButton"));
        }
        //Shutdown
        if (isAdmin() || $this->configuration()->isPermissionShutdown())
        {
            $this->addButton(new ShutdownButton("shutdownButton"));
        }
        //noVNC

        $serviceUrl = new UrlServiceHelper();
        if (isAdmin() || $this->configuration()->isPermissionNovnc())
        {

            $redirectButton = new RedirectButton('novnc');
            $redirectButton->setImageUrl(BuildUrl::getAppAssetsURL() . DS . 'img' . DS . 'buttons' . DS . 'novnc.png');
            $redirectButton->addHtmlAttribute("onclick", "window.open('{$serviceUrl->getNoVncConsoleUrl()}', '', 'width=900,height=700'); return false;");
            $redirectButton->setHref("#");
            $this->addButton($redirectButton);
        }
        //Spice Console
        if (isAdmin() || $this->configuration()->isPermissionSpice())
        {
            $redirectButton = new RedirectButton('spice');
            $redirectButton->setImageUrl(BuildUrl::getAppAssetsURL() . DS . 'img' . DS . 'buttons' . DS . 'spice.png');
            $redirectButton->setHref($serviceUrl->getSpiceConsoleUrl());
           if(!$this->hasVm()  || !$this->vm()->hasSpiceproxy()){
               $redirectButton->addHtmlAttribute("disabled",true);
           }
            $this->addButton($redirectButton);
        }
        //Xterm.js Console
        if (isAdmin() || $this->configuration()->isPermissionXtermjs())
        {
            $redirectButton = new RedirectButton('xtermjs');
            $redirectButton->setImageUrl(BuildUrl::getAppAssetsURL() . DS . 'img' . DS . 'buttons' . DS . 'xtermjs.png');
            $redirectButton->addHtmlAttribute("onclick", "window.open('{$serviceUrl->getXTermConsoleUrl()}', '', 'width=900,height=700'); return false;");
            $this->addButton($redirectButton);
        }
        //Migration
        if (isAdmin())
        {
            $this->addButton(new MigrateButton());
        }
    }


}