<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.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\Providers;

use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\RebootVmJob;
use ModulesGarden\ProxmoxAddon\App\Models\Job;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\HighAvailabilityClusterService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use function ModulesGarden\ProxmoxAddon\Core\Helper\queue;


class StatusProvider extends BaseDataProvider implements ClientArea
{
    use ProductService;
    use ApiService;

    public function start()
    {
        $this->vm()->start();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The container has been booted successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function reboot()
    {
        $ha = new HighAvailabilityClusterService();
        if ($ha->exist())
        {
            //reboot
            if (!Job::waiting()->ofJob(RebootVmJob::class)->ofHostingId($this->getWhmcsParamByKey("serviceid"))->count())
            {
                queue(RebootVmJob::class, [], null, "hosting", $this->getWhmcsParamByKey("serviceid"));
            }
            return (new HtmlDataJsonResponse())
                ->setStatusSuccess()
                ->setMessageAndTranslate('Rebooting container in progress')
                ->addData('refreshState', 'serverinformationTable')
                ->addRefreshTargetId('serviceInformationDataTable');
        }
        else
        {
            $this->vm()->restart();
            return (new HtmlDataJsonResponse())
                ->setStatusSuccess()
                ->setMessageAndTranslate('The container has been rebooted successfully')
                ->addData('refreshState', 'serverinformationTable')
                ->addRefreshTargetId('serviceInformationDataTable');
        }

    }

    public function stop()
    {
        $this->vm()->stop();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The container has been stopped successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function shutdown()
    {
        $this->vm()->shutdown();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The container has been shut down successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function read()
    {
    }

    public function update()
    {
    }
}