<?php

/* * ********************************************************************
 * Wordpress_Manager Product developed. (Nov 17, 2017)
 * *
 *
 *  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\HostingQuotaNotifications\App\UI\Groups\Providers\Configuration;

use ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider as BaseModelDataProvider;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Traits\RequestObjectHandler;
use ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Groups;


/**
 * Description of StepsProvider
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class StepsProvider extends BaseModelDataProvider implements AdminArea
{

    use RequestObjectHandler;

    public function __construct($type = null)
    {
        parent::__construct(null);
    }

    public function read()
    {
        $this->data['id'] = $this->getActionElementIdValue();
        
        $this->data['email_template']['value'] = (int) Groups\Manage::getValueForGroup(Request::getValue('groupid'), 'defaultEmailTemplate');
        if($this->getRequestValue('index') == 'editStepButton')
        {
            $this->loadDataForEdit();
        }
    }

    public function getSteps($type)
    {
        return GroupConfiguration\Manage::getConfigurationForGroupByType($type, Request::getValue('groupid'));
    }

    public function addStep()
    {
        $this->loadFormDataFromRequest();
        GroupConfiguration\Manage::addStep($this->formData, Request::getValue('groupid'));
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('stepAdded')->setStatusSuccess();
    }
    
    public function massStepDelete()
    {
        foreach($this->getMassActionsValues() as $stepId)
        {
            GroupConfiguration\Manage::deleteStep($stepId);
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('stepsDeleted');
    }

    public function deleteStep()
    {
        $id = $this->formData['id'];
        if ($id)
        {
            GroupConfiguration\Manage::deleteStep($id);
            return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('stepDeleted')->setStatusSuccess();
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('noGroupId')->setStatusError();
    }
    
    public function loadDataForEdit()
    {
        $selects = ['unit', 'email_template', 'ticket_template', 'ticket_status', 'action'];
        $configuration = GroupConfiguration\Manage::getStepConfiguration($this->getActionElementIdValue());
        
        foreach($configuration as $key => $value)
        {
            if(in_array($key, $selects))
            {
                $this->data[$key]['value'] = is_numeric($value) ? (int) $value : $value;
                continue;
            }
            $this->data[$key] = $value;
        }
        
    }
    
    public function updateStep()
    {
        $id = $this->formData['id'];
        unset($this->formData['id']);
        GroupConfiguration\Manage::updateStep($id, ['configuration' => serialize($this->formData)]);
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('stepUpdated')->setStatusSuccess();
    }
}
