<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Helper;

use \Illuminate\Database\Capsule\Manager as DB;

/**
 * Description of DBModelsHelper
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class DBModelsHelper
{


    public static function submoduleSettings($submodule, $settings)
    {
        $model   = new \ModulesGarden\SmsCenter\App\Models\Configuration\SubmoduleSettings();
        $setting = $model->where('submodule', '=', $submodule)->where('settingName', '=', $settings)->get();
        return reset($setting);
    }

    public static function getWHMCSConfigValue($config)
    {
        $conf = DB::table('tblconfiguration')
                ->select("value")
                ->where('setting', $config)
                ->first();

        return reset($conf);
    }

    public static function getSystemURL()
    {
        $syssslurl = self::getWHMCSConfigValue('SystemSSLURL');
        return $syssslurl ? $syssslurl : self::getWHMCSConfigValue('SystemURL');
    }

    public static function getGeneralSettingValue($setting)
    {
        $model = self::genneralSettingValue($setting);

        return $model[0]->value;
    }

    public static function clientSettingEnabled($id, $message)
    {
        $sett = self::genneralSettingValue('enableSmsCenter');

        if ($sett[0]->value != 'on')
        {
            return true;
        }

        if (self::checkCientConfiguration($id, $message))
        {//and client template != false
            return true;
        }
        return false;
    }

    protected static function checkCientConfiguration($id, $message)
    {
        $tid = TemplatesHelper::getTplId($message);

        $model = (new ClientConfiguration())->where('cid', $id)->where('tpl_id', $tid)->first();
        if (!$model)
        {
            return false;
        }
        $model->toArray();
        if ($model['enabled'] == 'on')
        {
            return true;
        }
        return false;
    }
}
