<?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 MGProvision\Proxmox\v2\repository\NodeRepository;
use ModulesGarden\ProxmoxAddon\App\Jobs\Vps\MigrateVmJob;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
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;
use ModulesGarden\ProxmoxAddon\Core\Queue\Models\Job;

class MigrateProvider extends BaseDataProvider implements AdminArea
{
    use ProductService;
    use ApiService;

    public function read()
    {
        $nodeRepository = new NodeRepository();
        $nodeRepository->setApi($this->api());
        $nodeRepository->findOnline(true);
        foreach ($nodeRepository->fetch() as $item)
        {
            if ($item->getNode() == $this->vm()->getNode())
            {
                continue;
            }
            $this->availableValues['target'][$item->getNode()] =  $item->getNode();
        }
        if ($this->vm()->isRunning())
        {
            $this->data['online'] = "on";
        }
    }

    public function update()
    {
        $attributes = [
            "targetNode" => $this->formData['target'],
            "online"     => $this->formData['online'] == "on" ? 1 : 0
        ];
        if (Job::where("job", MigrateVmJob::class. '@handle')->whereIn("status", ['waiting', "running", ""])
                                                  ->where("rel_id", $this->getWhmcsParamByKey("serviceid"))
                                                  ->where("rel_type", "hosting")->count())
        {
            return (new HtmlDataJsonResponse())
                ->setStatusError()
                ->setMessageAndTranslate("Task 'MigrateVm' already exist");
        }
        queue(MigrateVmJob::class, $attributes, null, "hosting", $this->getWhmcsParamByKey("serviceid"));
        sl("lang")-> addReplacementConstant("vmid",$this->vm()->getVmid())-> addReplacementConstant("node", $this->formData['target']);
        return (new HtmlDataJsonResponse())
            ->setStatusSuccess()
            ->setMessageAndTranslate("Starting migration of VM :vmid: to :node:")
            ->addData('refreshState', 'serverinformationTable')
            ->setCallBackFunction('refreshTable');
    }


}