<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ModulesGarden\HostingQuotaNotifications\App\UI\Groups\Providers;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Groups;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\ProductGroups;

/**
 * Description of GroupDeleteProvider
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class GroupDeleteProvider extends BaseModelDataProvider
{
    public function __construct($model = null)
    {
        parent::__construct($model);
    }
    
    public function read()
    {
         $this->data['id'] = $this->actionElementId;
    }

    public function update()
    {
        if ($this->getMassActionsValues())
        {
            return $this->deleteMass($this->getMassActionsValues());
        }
        return $this->deleteGroup($this->formData['id']);
    }

    public function deleteGroup($id = false)
    {
        if(!$id)
        {
            $id = $this->formData['id'];
        }
        if ($id)
        {
            Groups\Manage::delete($id);
//            ProductGroups\Manage::deleteRelations($id);
//            GroupConfiguration\Manage::deleteStepsForGroup($id);
            return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('groupDeleted')->setStatusSuccess();
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('noGroupId')->setStatusError();
    }

    public function deleteMass($groups = [])
    {
        foreach($groups as $groupId)
        {
            $this->deleteGroup($groupId);
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('massGroupDeleted')->setStatusSuccess();
    }

}
