<?php
//require_once 'PhoneModelFactory.php' ;
include dirname(__FILE__)."/../../../lib/AbstractXmlPhoneConfigApp.php";
require_once "lib/IppScreen.php"; 
require_once "lib/IppList.php";
require_once "lib/IppAlert.php";
require_once "lib/Text.php";
require_once "lib/Option.php";
require_once "lib/OptionText.php";
require_once "lib/IppCommand.php";
require_once "lib/Image.php";
require_once "lib/IppAction.php";
require_once "lib/AltText.php";
require_once "lib/IppPhoneNumber.php";
require_once "lib/IppKey.php";
require_once "lib/IppForm.php";
require_once "lib/IppImageItem.php";

require_once "lib/IppTextBox.php";
require_once "lib/IppTextField.php";

require_once "lib/GenericList/GenericList.php";
require_once "lib/GenericList/GenericCommand.php";

class XmlPhoneConfigApp extends AbstractXMLPhoneConfigApp {

    private $screen = null; 
    
    public function renderConfigMenuList($menuentries) {
        
        Log::d('renderConfigMenuList','siemens-config');
        // FIXME implement feature list ... 
        unset($menuentries[1]);
        $URL = $this->appURL();

        $screen = new IppScreen(1,0,5);
        $ippList = new IppList(IppList::TypeImplicit,1);
        $phone = new IppPhone();
        $listTitle = "Einstellungen";
        $ippList->setTitle($listTitle);
        $ippList->setUrl($URL);
         
        foreach ($menuentries as $key => $values) {
        
            $option= new Option(0,'section', $values['url']);
            $optionText = new OptionText($values['title'], $URL.
            $values['url'], $key."=".$key);
            $option->setText($optionText );
            $ippList->addOption($option);
        }
         
        $screen->addUIElement($ippList);
        $phone->addScreen($screen);
        $phone->printScreen();
       
    }
    
    
    public function renderCallForwardingsList($list,$activeID) {
//     edit
        Log::d('renderCallForwardingsList','siemens-config');
        
        $URI = $this->getForwardMenuURI();
        
        $screen = new IppScreen(1,0,5);
        $ippList = new IppList(IppList::TypeImplicit,1);
        $phone = new IppPhone();
        $listTitle = "Rufweiterleitung";
        $ippList->setTitle($listTitle);
        $ippList->setUrl($URI."&siemensStartAlert=start");

        foreach ($list as $key => $model) {
        	
            $target = $model->getSimpleTargetNumber();
   
            if ($target ) {
                $text = $model->name." (".$target.")";
            } else {
                $text = $model->name;
            }

            $isset = Option::NoneSelected;
            $image = new Image($this->getCallForwardStatusIcons('notSelected'));
            if ($model->id === $activeID) {
            	$isset = Option::Selected;
            	$image = new Image($this->getCallForwardStatusIcons('selected'));
            }
             
            $option= new Option($isset,'setItem', $model->id);
            $optionText = new OptionText($text ,$target, $key."=".$key);
            $option->setText($optionText );
            $option->addUIElement($image);
            $ippList->addOption($option);
            
        }
        
        $screen->addUIElement($ippList);
        $phone->addScreen($screen);
        $phone->printScreen();        
    }
    
    
    public  function renderCallForwardingItemSetConfirm($item) {
      
            Log::d('renderCallForwardingItemSetConfirm','siemens-config');
            $URI = $this->getForwardMenuURI()."&setItem=".$item->id;

            $screen = new IppScreen(4,0,5);
            
            $command = new GenericCommand($screen,array(
                'alert'=>array(
                    'type' => IppAlert::TypeConfirmation,
                    'delay' => IppAlert::DelayForever,
                    'title'=>$item->name,
                    'url'=>$URI,
                    'text' =>null,
                ),
                'commands' => array(
                    'Übernehmen'=>array(
                        'type'=>IppCommand::TypeSelect,
                        'displayOn'=>IppCommand::DisplayOnBoth,
                        'commandValue2'=>'siemensConfirmValue',
                        'commandValue'=>'use',
                    ),
                    'Bearbeiten'=>array(
                        'type'=>IppCommand::TypeUpdate,
                        'displayOn'=>IppCommand::DisplayOnBoth,
                        'commandValue2'=>'editMode',
                        'commandValue'=>'1',
                    ),
                    'Abbrechen'=>array(
                        'type'=>IppCommand::TypeCancel,
                        'displayOn'=>IppCommand::DisplayOnBoth,
                        'commandValue2'=>'siemensConfirmValue',
                        'commandValue'=>'cancel',
                    ),
                	'Beenden'=>array(
                			'type'=>IppCommand::TypeExit,
                			'displayOn'=>IppCommand::DisplayOnBoth,
                	)
                )
            ),IppCommand::TypeSelect, IppCommand::DisplayOnBoth);
            $command->printXml();
    }
    
    
    public function renderCallForwardingItemSet($item) {

            Log::d('renderCallForwardingItemSetConfirm','siemens-config');
            $URI = $this->getForwardMenuURI();

            $screen = new IppScreen(4,0,5);
            
            $command = new GenericCommand($screen,array(
		                'alert'=>array(
		                    'type' => IppAlert::TypeInfo,
		                    'delay' => IppAlert::DelayForever,
		                    'title' => $item->name,
		                    'url' => $URI,
		                    'text' =>"Übernahme gespeichert",
		                ),
		                'commands' => array(
		                    'OK' => array(
		                    	'type' => IppCommand::TypeUpdate,
		                        'displayOn' => IppCommand::DisplayOnBoth,
		                    		'commandValue2' => 'use',
		                    		'commandValue' => '1',
		                    ),
		                )
            		),
            		IppCommand::TypeSelect,
            		IppCommand::DisplayOnBoth
            );
            $command->printXml();
    }
    
    
    public function renderCallForwardingItemEditTargetNumberScreen($number, $itemId) {
        Log::d('renderCallForwardingItemEditTargetNumberScreen','siemens-config');
        
        $url = $this->appURL(
            array(
            'setItem'=>$itemId,
            'saveItem'=>$itemId,
            'section'=>'forwarding'
            
            )
        );
        
        $screen = new IppScreen(1,0,5);
        $phone = new IppPhone();

        $ippForm = new IppForm(IppForm::Proportion50_50, 2);
        $ippForm->setUrl($url);
        $ippForm->setTitle("Bearbeiten");
        
        
        $ippCommand = new IppCommand(IppCommand::TypeSelect, IppCommand::DisplayOnBoth, 0);
        $ippCommand->setLabel("Speichern");
        
        $ippCommandBack = new IppCommand(IppCommand::TypeBack, IppCommand::DisplayOnBoth, 0);
        $ippCommandBack->setLabel("Abbrechen");
        
        
        $IppTextField = new IppTextField("number");
        $IppTextField->setLabel("Zielrufnummer:");
        $IppTextField->addUIElement(new Text($number));
        
        $ippForm->addUIElement($IppTextField);
        
        $screen->addUIElement($ippForm);
        $screen->addUIElement($ippCommand);
        $screen->addUIElement($ippCommandBack);
        $phone->addScreen($screen);
        $phone->printScreen();
    }
    
    
    public function renderError($title, $text) {
    	
    	$URI = $this->appURL();

    	$screen = new IppScreen(1,0,5);
    	$phone = new IppPhone();

    	$ippTextBox = new IppTextBox(
    			'error',
    			$URI,
    			IppTextBox::ConstraintANY,
    			1024,
    			IppTextField::PasswordNO,
    			IppTextField::DefaultTEXT,
    			IppTextField::UneditableYES
    			);
    	$ippTextBox->setTitle($title);
    	$ippTextBox->setText( new Text($text));
    	
    	$command = new IppCommand(IppCommand::TypeSelect, IppCommand::DisplayOnListItem, 0);
    	$command->setLabel('OK');
    	  	
    	$screen->addUIElement($ippTextBox);
    	$screen->addUIElement($command);
    	$phone->addScreen($screen);
    	$phone->printScreen();
    }
    
    
    public function renderPhoneInput($input) {
        Log::d('renderPhoneInput','siemens-config');
    }
}
?>