<?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\App\Models;
use ModulesGarden\HostingQuotaNotifications\App\Helper as AppHelper;
use \ModulesGarden\HostingQuotaNotifications\Core\Helper;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Libs;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\Groups;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\ProductGroups;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;


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

    use RequestObjectHandler;
    
    public function __construct()
    {
        parent::__construct(null);
        $this->model = new Models\Groups();
    }

    public function read()
    {
        $this->data['id'] = $this->getActionElementIdValue();
        $this->loadDataToForm();
    }

    public function update()
    {
        $groupid = Request::getValue('groupid');
        $products = $this->formData['products'];
        unset($this->formData['products']);
        
        $changedType = Groups\Manage::checkIfTypeChanged($this->formData['type'], $groupid);
        
        Groups\Manage::update('id', '=', $groupid, $this->formData);
        ProductGroups\Manage::saveRelations($groupid, $products);
        if($changedType) // IT IS NEEDED TO RELOAD TABLES FOR FIXED OR TIERED TYPE
        {
            return $this->reloadConfigurationPage($groupid);    
        }
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('configurationUpdated')->setStatusSuccess();
    }

    public function loadDataToForm()
    {
        $group = Groups\Manage::getGroupWithProducts(AppHelper\Request::getValue('groupid'));
        $selects = ['type', 'products', 'defaultEmailTemplate'];
        foreach ($group 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 getSteps($type)
    {
        return GroupConfiguration\Manage::getConfigurationForGroupByType($type, Request::getValue('groupid'));
    }
    
    public function create()
    {
        parent::create();
    }
    
    public function reloadConfigurationPage($groupid)
    {
        return (new ResponseTemplates\HtmlDataJsonResponse())
                ->setMessageAndTranslate('configurationUpdated')
                ->setCallBackFunction('mgCustomRedirect')
                ->setData(["redirectUrl" => Helper\BuildUrl::getUrl('groups', 'configuration', ['groupid' => $groupid])]);
    }
        
    
}
