<?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\ProxmoxCloudVps\App\UI\Home\Providers;

use ModulesGarden\ProxmoxAddon\App\Jobs\Cloud\RebootVmJob;
use ModulesGarden\ProxmoxAddon\App\Models\Job;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\HighAvailabilityClusterService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\CloudService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Helpers\UrlServiceHelper;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use ModulesGarden\Servers\ProxmoxCloudVps\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()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $vm->start();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual Machine has been booted successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function reboot()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $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
        {
            $vm->restart();
            return (new HtmlDataJsonResponse())
                ->setStatusSuccess()
                ->setMessageAndTranslate('The Virtual Machine has been rebooted successfully')
                ->addData('refreshState', 'serverinformationTable')
                ->addRefreshTargetId('serviceInformationDataTable');
        }

    }

    public function stop()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $vm->stop();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual Machine has been stopped successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function shutdown()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $vm->shutdown();
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate('The Virtual Machine has been shut down successfully')
            ->addData('refreshState', 'serverinformationTable')
            ->addRefreshTargetId('serviceInformationDataTable');
    }

    public function deleteVm()
    {
        $vmModel = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel();
        //destory vm
        (new CloudService())->delete($vmModel);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setCallBackFunction('pcVmDeltedAjaxDone')
            ->addData('redirectUrl' , (new UrlServiceHelper())->home())
            ->setMessageAndTranslate('The Virtual Machine has been deleted successfully');
    }

    public function read()
    {
    }

    public function update()
    {
    }
}