<?php 
class GenericCommand {

    protected $phone;
    protected $screen;
    protected $alert;
    private   $comands;
    protected $xmlElement;

    public function __construct($screen,$params)
    {
        $this->phone = new IppPhone();
        $this->screen = $screen;
        $this->setAlert($params);
        if ($this->alert) {
            $this->screen->addUIElement($this->alert);
        } 
        $this->setCommands($params);
        if (isset($params['action'])) {
            $this->setActions($params);
        }
                
    }    

    public function setActions($params) {

        foreach ($params['action'] as $key => $value) {
            $$key = $value;
        }
        if ($type) {
            $action = new IppAction($type);
            if ($type == IppAction::TypeMakeCall) {
                $action->setNumber($number); 
            } 
            $this->xmlElement[] = $action;
        } 
    }

    private function setCommands($params) {

        if ( isset($params['commands']) ) {
            $prio = 0;
            $commands = array();
            $actions = array();
            foreach ($params['commands'] as $label => $commandAttributes) {
                foreach($commandAttributes as $key => $value) {
                    $$key = $value;
                }

                if (!isset($priority)) {
                    $priority = $prio;
                }
                if (isset($type)) {
                    if (isset($commandValue2)) {
                        $command = new IppCommand($type,$displayOn,$prio,$commandValue2,$commandValue);
                    } else {
                        $command = new IppCommand($type,$displayOn,$prio);
                    }
                    
                } 
                if (isset($command)) {
                    $command->setLabel($label);
                    if ($prio == 0) {
                        $command->setScreenID($this->screen->attributes['ID']);
                    }
                    $commands[] = $command;
                }
                if (isset($action) && is_array($action)) {
                    
                    foreach ($action as $key => $value) {
                        $$key = $value;
                    }
                    $ippAction = new IppAction($type);
                    if ($callByClick) {
                        $ippAction->setNumber($phoneNumber);
                    } 
                    $actions[] = $ippAction;
                    unset($action);
                    unset($callByClick);
                }
                $prio++;
            }
            $this->xmlElement =array_merge($actions,$commands);
        }
    }

    public function setAlert($params) {

        $defaultParams = array(
            'type' => IppAlert::TypeConfirmation,
            'delay' => IppAlert::DelayForever,
        );
        if (isset($params['alert'])) {
                $params = $params['alert'];
            if (!is_array($params)) {
                $params = $defaultParams;
            }
            foreach ($params as $key => $value) {
                $$key = $value;
            }

            $this->alert = new IppAlert($type,$delay);
            if (isset($title)) {
                $this->alert->setTitle($title);
            }
            if (isset($url)) {
                $this->alert->setUrl($url);
            }
            if (isset($text)) {
                $this->alert->addUIElement(new Text($text));
            }
            $hostInfo = app()->request->getHostInfo();
            if (isset($image))  {
                $this->alert->addUIElement(new Image($hostInfo.$image));
            } 
        } 
    }

    public function printXml() {
        if (isset($this->xmlElement)) {
            foreach ($this->xmlElement as $element) {
            
                $this->screen->addUIElement($element);
            } 
        } 
        $this->phone->addScreen($this->screen);
        $this->phone->printScreen();
    }

}
?>