<?php namespace Tussendoor\Reloadify;

use ReloadifyScoped\Adbar\Dot;

class Plugin
{
    protected static $config, $tasks;

    /**
     * Load config and tasks
     */
    public static function setup()
    {
        static::$config = new Dot;
        static::$tasks = new Dot;
        self::loadConfig(dirname(__FILE__, 2).'/config/config.php');
        self::loadTasks(dirname(__FILE__, 2).'/config/tasks.php');
    }
    
    /**
     * Load config file in our Dot property
     *
     * @param  string $config
     * @return bool
     */
    public static function loadConfig(string $config) : bool
    {
        if (!file_exists($config)) {
            return false;
        }

        self::$config->merge(require $config);
        return true;
    }
    
    /**
     * Static method to get or set data from the config
     *
     * @param  string $key
     * @param  mixed $value
     * @return mixed
     */
    public static function config(string $key, $value = null)
    {
        if ($value) {
            return self::$config->set($key, $value);
        }
        return self::$config->get($key);
    }
    
    /**
     * Helper method to check if plugin has specific key in config
     * Returns only true if the key exists AND if the value isnt empty
     *
     * @param  string $key
     * @return bool
     */
    public static function hasConfig(string $key) : bool
    {
        return ! self::$config->isEmpty($key);
    }
    
    /**
     * Load tasks file in our Dot property
     *
     * @param  mixed $file
     * @return bool
     */
    public static function loadTasks($file) : bool
    {
        if (!file_exists($file)) {
            return false;
        }

        self::$tasks->merge(require $file);
        return true;
    }
    
    /**
     * Get specific tasks
     *
     * @param  string $key
     * @return mixed
     */
    public static function tasks(string $key)
    {
        return self::$tasks->get($key);
    }
}
