<?php

namespace FreePBX\modules;

use BMO;

class Incrediblepbx implements BMO
{
	const BRAND = 'Incredible PBX';
	const RAWNAME = 'incrediblepbx';
	private $FreePBX;
	private $db;

	private $advancedSettings = array(
		'BRAND_IMAGE_FAVICON' => 'modules/incrediblepbx/assets/images/favicon.ico',
		'BRAND_IMAGE_TANGO_LEFT' => 'modules/incrediblepbx/assets/images/oem-top-left-image.png',
		'BRAND_TITLE' => 'Incredible PBX Admin',
		'BRAND_IMAGE_FREEPBX_FOOT' => '',
		'BRAND_FREEPBX_ALT_LEFT' => 'Incredible PBX®',
		'BRAND_FREEPBX_ALT_FOOT' => 'Incredible PBX®',
		'BRAND_IMAGE_FREEPBX_LINK_LEFT' => 'http://incrediblepbx.com/community/forums/',
		'BRAND_IMAGE_FREEPBX_LINK_FOOT' => 'http://incrediblepbx.com/',
		'BRAND_CSS_ALT_MAINSTYLE' => '',
		'BRAND_CSS_ALT_POPOVER' => '',
		'BRAND_CSS_CUSTOM' => 'modules/incrediblepbx/assets/css/custom.css',
		'DASHBOARD_FREEPBX_BRAND' => 'Incredible PBX',
		'MODULE_REPO' => 'https://mirror.clearlyip.com',
		'VIEW_LOGIN' => 'modules/incrediblepbx/views/login.php',
		'VIEW_UCP_FOOTER_CONTENT' => 'modules/incrediblepbx/views/ucp_footer_content.php',
		'VIEW_UCP_ICONS_FOLDER' => 'modules/incrediblepbx/assets/icons',
		'VIEW_FOOTER_CONTENT' => 'modules/incrediblepbx/views/footer_content.php',
		'SIPUSERAGENT' => 'IncrediblePBX',
		'RSSFEEDS' => "http://incrediblepbx.com/rssfeed.xml\nhttp://nerdvittles.com/wp-rss2.php\nhttp://www.freepbx.org/rss.xml\nhttp://feeds.feedburner.com/InsideTheAsterisk",
	);

	public function __construct($freepbx = null)
	{
		if ($freepbx == null) {
			throw new Exception("Not given a FreePBX Object");
		}
		$this->FreePBX = $freepbx;
		$this->db = $freepbx->Database;
	}

	public function install()
	{
		$settings = $this->FreePBX->Config;

		$set['value'] = 'incredible';
		$set['defaultval'] = $set['value'];
		$set['options'] = '';
		$set['readonly'] = 1;
		$set['hidden'] = 1;
		$set['level'] = 0;
		$set['module'] = 'incrediblepbx';
		$set['category'] = _('Branding');
		$set['emptyok'] = 0;
		$set['name'] = _('Brand ID');
		$set['description'] = _("ID for this brand");
		$set['type'] = CONF_TYPE_TEXT;
		$set['sortorder'] = 98;
		$settings->define_conf_setting('MIRROR_BRAND', $set);
		unset($set);

		$set['value'] = '15.19.11.001';
		$set['defaultval'] = 'latest';
		$set['options'] = '';
		$set['readonly'] = 1;
		$set['hidden'] = 0;
		$set['level'] = 0;
		$set['module'] = 'incredible';
		$set['category'] = _('Branding');
		$set['emptyok'] = 0;
		$set['name'] = _('Lock Version');
		$set['description'] = _("Version to set this brand to");
		$set['type'] = CONF_TYPE_TEXT;
		$set['sortorder'] = 98;
		$settings->define_conf_setting('MIRROR_BRAND_VERSION', $set);
		unset($set);

		$set['value'] = 'https://incrediblesip.clearlyip.com';
		$set['defaultval'] = 'https://incrediblesip.clearlyip.com';
		$set['options'] = '';
		$set['readonly'] = 1;
		$set['hidden'] = 1;
		$set['level'] = 0;
		$set['module'] = 'incredible';
		$set['category'] = _('Branding');
		$set['emptyok'] = 0;
		$set['name'] = _('Trunking URL');
		$set['description'] = _("URL to use for trunking store");
		$set['type'] = CONF_TYPE_TEXT;
		$set['sortorder'] = 98;
		$settings->define_conf_setting('MIRROR_TRUNKING_URL', $set);
		unset($set);

		unset($settings);

		$this->removeConflictingModules();
		$this->setSettings($this->FreePBX);
		$this->clearModuleCache();
	}

	public function uninstall()
	{
		$settings = array_keys($this->advancedSettings);

		$this->FreePBX->Config->reset_conf_settings($settings, true);
		$this->clearModuleCache();
	}

	/**
	 * During an enable, let's make sure all our settings are set
	 */
	public function enable()
	{
		$this->setSettings($this->FreePBX);
	}

	/**
	 * When you disable this module, revert the advanced settings to prevent any issues with viewing things in the UI (in case it's removed)
	 */
	public function disable()
	{
		$settings = array_keys($this->advancedSettings);

		$this->FreePBX->Config->reset_conf_settings($settings, true);
	}

	public function backup()
	{
	}

	public function restore($backup)
	{
	}
	//We do this because if we call get on a freepbx setting and it doesn't exist we get back int(0),
	//which could also be a valid value, so this allows us to know if it's valid or not
	private function getAvailableAdvancedSettings()
	{
		$advancedSettingKeys = array_keys($this->advancedSettings);
		$whereInArray  = str_repeat('?,', count($advancedSettingKeys) - 1) . '?';
		$sql = "SELECT * FROM freepbx_settings WHERE keyword IN ($whereInArray)";
		$stmt = \FreePBX::Database()->prepare($sql);
		$stmt->execute($advancedSettingKeys);
		$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);

		$keysAvailable = array();
		foreach ($data as $d) {
			$keysAvailable[$d['keyword']] = $d;
		}

		return $keysAvailable;
	}

	public function setSettings($FreePBX)
	{
		$settings = $FreePBX->Config;

		$keysAvailable = $this->getAvailableAdvancedSettings();

		foreach ($this->advancedSettings as $setting => $value) {
			//If the setting does not exist we can not change it
			if (!isset($keysAvailable[$setting])) {
				//We don't exist so we can't set the conf values
				continue;
			}
			$value = str_replace('__RAWNAME__', self::RAWNAME, $value);
			$value = str_replace('__BRAND__', self::BRAND, $value);
			$settings->set_conf_values(array($setting => $value), true, true);
		}
	}

	public function removeConflictingModules()
	{
		$mf = \module_functions::create();

		$modules = array(
			'oembranding'
		);

		foreach ($modules as $module) {
			//Delete tries to do an uninstall and then removes it
			$mf->delete($module, true);
		}
	}

	public function clearModuleCache()
	{
		$this->FreePBX->Config->set_conf_values(array('MODULEADMIN_SKIP_CACHE' => true), true, true);
		$mf = \module_functions::create();
		$mf->getonlinexml();
		$this->FreePBX->Config->reset_conf_settings(array('MODULEADMIN_SKIP_CACHE'), true);
	}

	public function showView($view = '')
	{
	}

	public function doConfigPageInit()
	{
	}

	public function ajaxRequest($command, $setting)
	{
		return false;
	}

	public function ajaxHandler()
	{
	}
}
