<?php
class IppPhone {
	
	public $xw;
	public $displayUsed = false;
	public $screens = array();
	
	
	public function __construct () {

		$this->xw = new xmlWriter();
		$this->xw->openMemory();
		$this->xw->setIndent(TRUE);
		$this->xw->startDocument('1.0', 'UTF-8'); // start document
		
		$attributes = array();
		
		$this->xw->startElement("IppPhone");
		if ($attributes && is_array($attributes)) {
				foreach($attributes as $key => $value) {
					$this->xw->writeAttribute($key, $value);
				}
		}
	
	}
	
	private function enableDisplay() {
	
		$this->displayUsed = true;
//		$this->xw->startElement('IppDisplay');  // <IppDisplay> with content
		//$this->xw->writeAttribute($attributes,$value);
//		$this->xw->endElement();
	
	}
	
	/**
	 * 
	 * adds a screen object into this current page
	 * 
	 * @param unknown $screen the screen which will be rendered.
	 */
	
	public function addScreen($screen, $autoAddDisplay = true) {
		
		if ($autoAddDisplay == true) {
			$this->displayUsed = true;
		}
		
		$this->screens[] = $screen;
	}
	
	
	private function finishDocument() {
			
		// is display enabled ? start display
		if ($this->displayUsed) {
			$this->xw->startElement("IppDisplay"); // FIXME may set some attributes?
		}

		// loop all screens, and call render methods to get XML Elements
		foreach ($this->screens as $screen) {
			$screen->renderXML($this->xw);
		}
		
		
		// is display enabled ? end display
		if ($this->displayUsed) {
			$this->xw->endElement(); // FIXME may set some attributes?
		}
		
		$this->xw->endElement(); // close node
		$this->xw->endDocument(); // end document
	}
	
	public function printScreen() {
		$this->finishDocument();
		echo $this->xw->outputMemory();	
	}	
}