<?php
namespace App\Dashboard\Templates;

/**
 * Base abstract of templates
 */
abstract class BaseAbstract
{
	/**
	 * Stores the references to the singleton instances
	 * @var array
	 */
	static protected $instance = [];

	/**
	 * Store dashboard object
	 * @var \Engine\Dashboard\GeneralAbstract
	 */
	protected $dashboard = NULL;

	/**
	 * The singleton constructor for the current class
	 * @return \App\Dashboard\Templates\BaseAbstract
	 */
	static public function getInstance(\Engine\Dashboard\GeneralAbstract $dashboard)
	{
		// the instantiated class
		$templateClassName = get_called_class();

		$dashboardClassName = get_class($dashboard);

		// apply the singleton
		if (!isset(static::$instance[$templateClassName][$dashboardClassName])) {
			static::$instance[$templateClassName][$dashboardClassName] = new static($dashboard);
		}

		// return the new object
		return static::$instance[$templateClassName][$dashboardClassName];
	}

	/**
	 * By default constructor class is protected
	 */
	final protected function __construct(\Engine\Dashboard\GeneralAbstract $dashboard)
	{
		$this->dashboard = $dashboard;
	}

	/**
	 * Get internal dashboard
	 * @return \Engine\Dashboard\GeneralAbstract
	 */
	public function getDashboard()
	{
		return $this->dashboard;
	}

	/**
	 * Store dashboard in database
	 */
	public function saveDashboard()
	{
		$storageObj = new \App\Dashboard\Storage\Database();
		$storageObj->set(get_class($this->dashboard), $this->dashboard);
	}

	/**
	 * Template name (translated)
	 * @return string
	 */
	public function getName()
	{
		\App\Loader_Lang::getInstance()->loadAppFile("_customLoad/dashboard/templates/_general.lang.php");
		$_LANG = \App\Loader_Lang::getInstance()->getGlobal();

		return $_LANG['dashboard']['templates']['\\'.get_called_class()]['name'];
	}

	/**
	 * Template description (HTML)
	 * @return string
	 */
	public function getDescription()
	{
		return '';
	}

	/**
	 * Populate dashboard with elements
	 */
	abstract public function configureDashboard();

}