<?php
/**
 *-------------------------------------------------------------------------------------
 * @package    VP Payment by Shipment Plugin for VirtueMart
 *-------------------------------------------------------------------------------------
 * @copyright  Copyright (C) 2012-2019 VirtuePlanet Services LLP. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 * @authors    Abhishek Das
 * @email      info@virtueplanet.com
 * @link       https://www.virtueplanet.com
 *-------------------------------------------------------------------------------------
 */

defined ('_JEXEC') or die;

class vmPSPlugin extends vmPSPluginOriginal
{
	// Property to flag this class is our class
	public $isOverride = true;
	
	// Override original getPluginMethods function
	protected function getPluginMethods($vendorId)
	{
		// Get the original methods.
		$result = parent::getPluginMethods($vendorId);
		$u = (int) self::_getSystemInfo('u', 0);

		if(!empty($this->methods) && ($u > 0 || self::_isLocalhost()))
		{
			JPluginHelper::importPlugin('vpsystem');
			
			$dispatcher       = JDispatcher::getInstance();
			$disabled_methods = array();
			$results          = $dispatcher->trigger('plgOnVmGetDisabledPSMethods', array($this->_psType, &$disabled_methods));
			
			// Get the final unique list of payment methods to be disabled
			$disabled_methods = $this->_arrayFilterUniqueInteger($disabled_methods);

			if(!empty($disabled_methods))
			{
				foreach($this->methods as $key => &$method)
				{
					$pkey      = $this->_idName;
					$method_id = (int) $method->$pkey;
					
					if(in_array($method_id, $disabled_methods))
					{
						unset($this->methods[$key]);
					}
				}
				
				$this->methods = array_values($this->methods);
				
				$result = count($this->methods);
			}
		}
		
		return $result;
	}
	
	/**
	 * Normally this function is inherited from parent but we need to override
	 * 
	 * Checks if this plugin should be active by the trigger
	 *
	 * @param int/array $id the registered plugin id(s) of the joomla table
	 */
	function selectedThisByMethodId($id = 'type')
	{
		if($id != 'type')
		{
			JPluginHelper::importPlugin('vpsystem');
			
			$dispatcher       = JDispatcher::getInstance();
			$disabled_methods = array();
			$results          = $dispatcher->trigger('plgOnVmGetDisabledPSMethods', array($this->_psType, &$disabled_methods));
			
			// Get the final unique list of payment methods to be disabled
			$disabled_methods = $this->_arrayFilterUniqueInteger($disabled_methods);
			
			if(!empty($disabled_methods) && in_array($id, $disabled_methods))
			{
				return false;
			}
		}
		
		return parent::selectedThisByMethodId($id);
	}
	
	private function _arrayFilterUniqueInteger($array)
	{
		if(empty($array))
		{
			return array();
		}
		
		$array = array_unique($array);
		$array = !empty($array) ? array_filter(array_map('intval', $array)) : $array;
		
		return $array;
	}
	
	private static function _getSystemInfo($property, $default = 'NA')
	{
		$file  = dirname(__DIR__) . '/vppaymentbyshipment.xml';
		$value = $default;
		
		if(version_compare(JVERSION, '3.0.0', 'ge'))
		{
			$xml = JFactory::getXML($file);
			
			if(property_exists($xml, $property))
			{
				$value = (string) $xml->$property;
			}
		}
		else
		{
			$parser = JFactory::getXMLParser('Simple');
			$parser->loadFile($file);
			
			$doc     = $parser->document;
			$element = $doc->getElementByPath($property);
			
			$value = (string) $element->data();
		}
		
		return $value;
	}
	
	private static function _isLocalhost()
	{
		$parts = parse_url(JUri::root());
		
		if(!empty($parts['host']) && strtolower($parts['host']) == 'localhost' || $parts['host'] == '127.0.0.1')
		{
			return true;
		}
		
		return false;
	}
}