<?php

namespace App\GenDocument\SpecialObject;

class General_MultipleImages extends GeneralAbstract
{

	public function render()
	{

		$return = [];

		///////////////////////////////////////////////////////////////////////////////////////////////
		// MAIN TABLE PROPERTIES //////////////////////////////////////////////////////////////////////
		///////////////////////////////////////////////////////////////////////////////////////////////
		$matched = false;
		for($i=0; $i<100;$i++){
			if(!$this->settings["image_image{$i}"]){
				continue;
			}

			////////////////////////////////////////////////////////////////////////////////////////////
			$objectSettings = [];
			$objectSettings['image'] = 		$this->settings["image_image{$i}"];
			$objectSettings['condition'] = 	$this->settings["image_condition{$i}"];
			$objectSettings['variable']['original'] = 	$this->settings["image_simpleVariable"];
			$objectSettings['variable']['parsed'] = 	$this->textProcessor->parse($this->settings["image_simpleVariable"]);

			$extraFlags = substr($objectSettings['condition'], 0, 2);
			switch($extraFlags){
				case '!r':					// use regexp
					$objectSettings['condition'] = substr($objectSettings['condition'], 2);
					if(preg_match("/{$objectSettings['condition']}/u", $objectSettings['variable']['parsed'])){
						$matched = true;
					}
				break;

				case '*':
					$matched = true;
				break;

				default:
					if($objectSettings['condition'] == $objectSettings['variable']['parsed']){
						$matched = true;
					}
				break;
			}


			if($matched){
				// create the IMG tag here and return it
				$img = new \DTMControl_image();
				$img->attributes['src'] = $objectSettings['image'];

				$img->attributes['paddingTop'] = 	$this->settings['paddingTop'];
				$img->attributes['paddingBottom'] = $this->settings['paddingBottom'];
				$img->attributes['paddingLeft'] = 	$this->settings['paddingLeft'];
				$img->attributes['paddingRight'] = 	$this->settings['paddingRight'];

				$img->attributes['Xmm'] = 			$this->settings['Xmm'];
				$img->attributes['Ymm'] = 			$this->settings['Ymm'];

				$img->attributes['opacity'] = 		100;
				$img->attributes['Wmm'] = 			0;		// auto
				$img->attributes['Hmm'] = 			0;		// auto
				$img->attributes['scaleX'] = 		100;
				$img->attributes['scaleY'] = 		100;

				$img->attributes['maxW'] = 			$this->settings['image_maxW'];
				$img->attributes['maxH'] = 			$this->settings['image_maxW'];

				$img->attributes['fixed'] = 		$this->settings['fixed'];
				$img->attributes['block'] = 		true;
				$img->attributes['layer'] = 		$this->settings['layer'];
				$img->attributes['orderIndex'] = 	$this->settings['orderIndex'];

				$return[]=$img->exportAsControl();
				break;
			}
		}

		return $return;
	}
}