<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration;

use \ModulesGarden\HostingQuotaNotifications\App\Models;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Lang\Lang;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\GroupConfiguration\Formatter;
use ModulesGarden\HostingQuotaNotifications\Core\Helper as CoreHelper;
/**
 * Description of Manage
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Manage
{

    public static function getConfigurationForGroupByType($type, $gid, $replaceIdToNames = true)
    {
        $configuration = Models\GroupsConfiguration::where('kind', '=', $type)->where('groupid', '=', $gid)->get()->toArray();

        $lang = CoreHelper\sl('lang');
        $data = Formatter::getReplaceData();
        
        foreach ($configuration as &$conf)
        {
            $step = Formatter::formatRow($conf['configuration'], $data, $lang, $replaceIdToNames);
            $conf = Formatter::mergeFormatAndBase($conf, $step);
        }
        return $configuration;
    }

    public static function addStep($configuration, $groupId)
    {
        $groupsConfiguration                = new Models\GroupsConfiguration();
        $groupsConfiguration->groupid       = $groupId;
        $groupsConfiguration->kind          = $configuration['id'];
        unset($configuration['id']);
        $groupsConfiguration->configuration = serialize($configuration);
        $groupsConfiguration->save();
    }

    public static function deleteStepsForGroup($groupid)
    {
        Models\GroupsConfiguration::where('groupid', '=', $groupid)->delete();
    }

    public static function deleteStep($id)
    {
        Models\GroupsConfiguration::where('id', '=', $id)->delete();
    }

    public static function getStepConfiguration($id)
    {
        $step = Models\GroupsConfiguration::select('configuration')->where('id', '=', $id)->value('configuration');
        return unserialize($step);
    }

    public static function updateStep($id, $update)
    {
        Models\GroupsConfiguration::where('id', '=', $id)->update($update);
    }
    
    public static function getGroupsConfigurations()
    {
        return Models\GroupsConfiguration::all();
    }
}
