<?php
namespace WpBookingEvent;
use NBAppHelper;
use RuntimeException;
use wpbookingeventconfig;
use WpBookingEvent\Application\Application as ApplicationAlias;
use WpBookingEvent\Document\Document;
use WpBookingEvent\FOFInput\FOFInput;
use WpBookingEvent\Language\Language;
use WpBookingEvent\Registry\Registry;
use WpBookingEvent\Uri\NBUri;
use WpBookingEvent\WbDate\WbDate;
use WpBookingEvent\Wordpress\Wordpress;
use WpBookingEvent\Database\driver\DatabaseDriverMysqli;


/**
 * Class Factory
 * @package WpBookingEvent
 */
class Factory
{

    private static $input;
    private static $application;
    private static $document;
    private static $dates;
    private static $open_source=null;

    public static function getInput()
    {
        if (!self::$input) {
            self::$input = new FOFInput();
        }
        return self::$input;
    }
    public static function getApplication($client)
    {
        if (!self::$application) {

            self::$application = ApplicationAlias::getInstance($client);
        }

        return self::$application;
    }

    public static function getConfig(){
        $file=WPBOOKINGEVENT_PATH_ROOT.DS."wpbookingeventconfig.php";
        if (is_file($file))
        {
            include_once $file;
        }
        $registry = new Registry;
        $config=new wpbookingeventconfig();
        $registry->loadObject($config);

        return $registry;
    }
    public static function getDocument():Document
    {
        if (!isset(static::$document)) {
            static::$document = new Document();
        }
        return static::$document;
    }

    public static function getWordpress($client=0):Wordpress
    {
        $app=ApplicationAlias::getInstance($client);
        return $app->getWordpress();

    }
    public static function getDate($time = 'now', $tzOffset = null)
    {
        if (!isset(self::$dates[$time]))
        {
            self::$dates[$time] = new WbDate($time, $tzOffset);
        }

        $date = clone self::$dates[$time];
        return $date;
    }

    public static function getRootUrlPlugin()
    {
        return WPBOOKINGEVENT_PLUGIN_DIR_URL;
    }

    public static function getRootUrl()
    {
        return WPBOOKINGEVENT_SITE_URL;
    }
    public static function getRootUrlBackend()
    {
        return WPBOOKINGEVENT_SITE_URL."/wp-admin";
    }
    public static function getDBO()
    {
        $host = DB_HOST;
        $user = DB_USER;
        $password = DB_PASSWORD;
        $database = DB_NAME;
        $prefix = "";
        $driver = "mysqli";
        $debug = true;
        $options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);


        try {
            $db = DatabaseDriverMysqli::getInstance($options);
        } catch (RuntimeException $e) {
            if (!headers_sent()) {
                header('HTTP/1.1 500 Internal Server Error');
            }
            exit('Database Error: ' . $e->getMessage());
        }

        $db->setDebug($debug);
        return $db;
    }

    public static function getSession()
    {
    }

    public static function getAppConfig()
    {
        $db=Factory::getDBO();
        $query=$db->getQuery(true);
        $query->select("*")
            ->from(WPBOOKINGEVENT_PREFIX_TABLE."config")
            ->where('id=1')
        ;
        $item=$db->setQuery($query)->loadObject();
        $param=$item->params;
        $reg=new Registry();
        $reg->loadString($param);
        return $reg;
    }

    public static function getUri()
    {
        $uri=NBUri::getInstance();
        return $uri;
    }

    public static function getLanguage()
    {
        $conf = self::getConfig();
        $locale = $conf->get('language');
        $debug = $conf->get('debug_lang');
        $lang = Language::getInstance($locale, $debug);
        return $lang;
    }
    public static function getOpenSource(){
        if (!isset(self::$open_source))
        {

            self::$open_source = Wordpress::getInstance();
        }
        return self::$open_source;
    }
}