<?php


namespace ModulesGarden\Servers\ProxmoxCloudVps\App\Http\Client;


use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\AppParams;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\ProxmoxAddonValidator;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Graph\Pages\ServiceCpuGraph;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Graph\Pages\VmSmallGraphs;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Home\Pages\AccountSummary;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Home\Pages\AccountSummaryBars;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Home\Pages\ServiceActions;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Home\Pages\ServiceDetails;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Vm\Pages\VmTabs;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Forms\VmCreateForm;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmUpdate\Forms\VmUpdateForm;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\Http\AbstractClientController;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Traits\WhmcsParams;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\view;

class Vm extends AbstractClientController
{
    use WhmcsParams;
    use ProductService;
    use BaseClientController;
    use ApiService;

    /**
     * details
     */
    public function index()
    {
        if ($this->getWhmcsParamByKey('status') != 'Active')
        {
            return;
        }
        $this->initVm();
        (new AppParams())->initFromWhmcsParams();
        $this->api();
        if (!ProxmoxAddonValidator::isInstalled())
        {
            ProxmoxAddonValidator::isInstalledOrFail();
        }
        else
        {
            if (!$this->isVmCreated(\ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()))
            {
                return $this->onVpsBuild();
            }
        }
        $view = view();
        $view->initCustomAssetFiles();
        $view->addElement(ServiceDetails::class);
        $view->addElement(ServiceActions::class);
        $view->addElement(VmTabs::class);

        return $view;
    }

    public function create()
    {
        (new AppParams())->initFromWhmcsParams();
        $view = view();
        $view->initCustomAssetFiles();
        /**
         * @deprecated
         *     $view->addElement(\ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VmCreate\Sections\AccountSummary::class);
         */

        return $view->addElement(VmCreateForm::class);
    }

    public function update()
    {
//        sl("sidebar")->getSidebar("virtualMachinesProxmoxCloudVps")->getChild("vmupdate")->setActive(true);
        $this->initVm();
        (new AppParams())->initFromWhmcsParams();
        $this->api();
        return view()->addElement(VmUpdateForm::class);
    }

}