<?php

include dirname(__FILE__) . '/../../../lib/AbstractXmlPhoneConfigApp.php';
require_once "lib/XmlMenuScreen.php";
require_once "lib/XmlInputScreen.php";
require_once "lib/XmlExecuteCommand.php";

class XmlPhoneConfigApp extends AbstractXMLPhoneConfigApp
{

    private $screen = null;

    private function getScreenAttributes($changeAttribute = array())
    {
        $this->setConfigAppLanguage();
        $screenAttributes = array();
        $screenAttributes["defaultIndex"] = "";
        $screenAttributes["style"] = "radio";
        $screenAttributes["Beep"] = "no";
        $screenAttributes["cancelAction"] = "";
        $screenAttributes["wrapList"] = "yes";
        $screenAttributes["Timeout"] = "30";
        $screenAttributes["LockIn"] = "no";

        if (!empty($changeAttribute)) {
            foreach ($changeAttribute as $key => $value) {
                $screenAttributes[$key] = $value;
            }
        }
        return $screenAttributes;
    }

    public function renderConfigMenuList($menuentries)
    {
        $URI = $this->getMainMenuItemURI();
        $screenAttributes = $this->getScreenAttributes();

        $screen = new XmlMenuScreen($screenAttributes);

        $screen->setTitle(__("Konfiguration"));

        foreach ($menuentries as $menuentry) {
            $screen->addMenuItem($menuentry["title"], $URI, "section=" . $menuentry["url"]);
        }

        $screen->addButton("4", "Select");

        $screen->printScreen($this->getPhoneModel()->type);
    }

    public function renderCallForwardingsList($list, $activeID)
    {
        if (empty($list)) {
            return $this->renderError('', __('XML_GLOBAL_NO_ENTRIES_FOUND'));
        }

        $URI = $this->getForwardMenuURI();
        $cnt = 1;
        foreach ($list as $model) {
            if ($model->id === $activeID) {
                break;
            }
            ++$cnt;
        }
        $screenAttributes = $this->getScreenAttributes(array('defaultIndex' => $cnt));

        $screen = new XmlMenuScreen($screenAttributes);

        $screen->setTitle(__("XML_CONFIG_APP_CALL_FORWARDING_TITLE"));

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

            $screen->addMenuItem($text, $URI, "k=" . $key . "&setItem=" . $model->id);
        }

        $screen->addButton("1", __('XML_GLOBAL_EXIT_BTN'), $URI . "&exit=1");
        $screen->addButton("3", __('XML_CONFIG_APP_EDIT_BTN'), $URI . "&editMode=1"); //"Edit."
        $screen->addButton("4", __('XML_CONFIG_APP_APPLY_SETTINGS'));

        $screen->printScreen($this->getPhoneModel()->type);
    }

    public function renderCallForwardingItemEditTargetNumberScreen($number, $itemId)
    {
        // render input field for number
        $URI = $this->getForwardMenuURI();
        $attributes = array(
            'type' => 'string',
            'Password' => 'yes',
            'displayMode' => 'normal'
        );
        $screenAttributes = $this->getScreenAttributes($attributes);

        $screen = new XmlInputScreen($screenAttributes);
        $screen->setTitle(__('XML_CONFIG_APP_CALL_DESTINATION_TITLE'));

        $screen->xw->writeElement("URL", $URI . "&saveItem=" . $itemId . "&setItem=" . $itemId);
        $screen->addInputField("number", "no", "yes", __('XML_CONFIG_APP_CALL_DESTINATION_INPUT_TEXT') . ":", "number", "", $number);

        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), "SoftKey:Exit");
        $screen->addButton("3", __('XML_GLOBAL_DELETE_BTN'), "SoftKey:BackSpace");
        $screen->addButton("4", __('XML_CONFIG_APP_SAVE_BTN'), "SoftKey:Submit");

        $screen->printScreen($this->getPhoneModel()->type);
    }

    public function renderError($title, $text)
    {
        $attributes = array(
            'Beep' => 'yes'
        );
        $screenAttributes = $this->getScreenAttributes($attributes);

        $screen = new XmlScreen("YealinkIPPhoneTextScreen", $screenAttributes);
        $screen->setTitle($title);
        $screen->addText($text);

        $screen->addButton("1", __('XML_CONFIG_APP_CONFIRM_BTN'), $this->getMainMenuItemURI());
        $screen->printScreen();
    }

    public function renderPhoneInput($input)
    {
        $URI = $this->appURL();

        $screenAttributes = array();
        $screenAttributes['type'] = "string";
        $screenAttributes['Beep'] = "yes";
        $screenAttributes["cancelAction"] = "Wav.Play:http://10.1.0.27:9/auto/zhongguo.wav";
        $screenAttributes["Password"] = "yes";
        $screenAttributes["Timeout"] = "15";
        $screenAttributes["LockIn"] = "no";
        $screenAttributes["inputLanguage"] = "German";
        $screenAttributes["displayMode"] = "normal";
        $screenAttributes["defaultIndex"] = "";

        $screen = new XmlInputScreen($screenAttributes);

        $screen->setTitle("Input Test Screen");

        $screen->xw->writeElement("URL", $URI);

        $screen->addInputField("IP", "no", "yes", "IP-Test", "1", "IP", "192.168.24.160");

        $screen->addButton("1", "Exit", $URI = "SoftKey:Exit");
        $screen->addButton("2", "Button2", $URI = "SoftKey:Submit");
        $screen->addButton("4", "Button3", $URI = "SoftKey:Backspace");
        $screen->addButton("4", "Button4", $URI = "SoftKey:Submit");

        $screen->printScreen();
    }

    public function renderExit()
    {
        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlExecuteCommand($screenAttributes);
        $screen->setExecuteItem();
        $screen->printScreen($this->getPhoneModel()->type);
    }
}
