<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Aug 23, 2018)
 * *
 *
 *  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\ProxmoxAddon\App\UI\ServerGroup\Providers;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;

/**
 *
 * Description of RangeTemplateProvider
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class ServerGroupProvider extends main\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider implements AdminArea
{

    use main\App\Services\BaseService;

    /**
     * ServerGroupProvider constructor.
     */
    public function __construct()
    {
        parent::__construct(main\App\Models\ServerGroup::class);
    }

    public function read()
    {
        //read for update & delete
        if (is_numeric($this->actionElementId))
        {
            parent::read();
            $this->data['node'] = main\App\Models\ServerGroupItem::ofGroupId($this->actionElementId)->pluck("node")->toArray();
        }
        //server id
        $this->data['server_id'] = $this->getRequestValue('id');
        $api = $this->setServerId($this->getRequestValue('id'))->getApi();
        //node
        $nodeRepository = new proxmox\repository\NodeRepository();
        $nodeRepository->setApi($api);
        $nodeRepository->findOnline(true);
        foreach ($nodeRepository->fetch() as $node){
            $this->availableValues['node'][$node->getNode()] = $node->getNode();
        }
    }

    public function create()
    {
        parent::create();
        foreach ($this->formData['node']  as $node){
            $serverGroupItem = new main\App\Models\ServerGroupItem();
            $serverGroupItem->group_id = $this->model->id;
            $serverGroupItem->node = $node;
            $serverGroupItem->save();
        }
        main\Core\Helper\sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('Server group :name: has been created successfully.');
    }

    public function delete()
    {
        main\App\Models\ServerGroup::destroy([$this->formData['id']]);
        main\Core\Helper\sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('Server group :name: has been deleted successfully.');
    }


    public function deleteMass()
    {
        if (!$this->getRequestValue('massActions'))
        {
            return (new HtmlDataJsonResponse())->setMessageAndTranslate('Bug no massActions data!')->setStatusError();
        }
        main\App\Models\ServerGroup::destroy($this->getRequestValue('massActions') );
        return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('The selected server group have been deleted.');
    }

    public function update()
    {
        //update
        parent::update();
        //update server group
        main\App\Models\ServerGroupItem::ofGroupId($this->formData['id'])
             ->ofNodeNotIn($this->formData['node'])
             ->delete();
        foreach ($this->formData['node'] as $node){
            if(main\App\Models\ServerGroupItem::ofGroupId($this->formData['id'])->ofNode($node)->count()){
                continue;
            }
            $sgi = new main\App\Models\ServerGroupItem();
            $sgi->fill([
                'group_id' => $this->formData['id'],
                "node" => $node ]
            )->save();
        }
        main\Core\Helper\sl('lang')->addReplacementConstant('name', $this->formData['name']);
        return (new main\Core\UI\ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('Server group :name: has been updated successfully.');
    }


}
