<?php

include dirname(__FILE__) . '/../../../lib/AbstractXmlPhonebookApp.php';
require_once "lib/XmlMenuScreen.php";
require_once "lib/XmlFormattedTextScreen.php";
require_once "lib/XmlExecuteCommand.php";

class XmlPhonebookApp extends AbstractXmlPhonebookApp
{

    private $screen = null;

    private function getScreenAttributes($changeAttribute = array())
    {
        $this->setPhonebookLanguage();
        $screenAttributes = array();
        $screenAttributes["defaultIndex"] = "1";
        $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 renderPhoneBooks($books)
    {
        $URI = $this->getPhonebookURI();
        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlMenuScreen($screenAttributes);
        $screen->setTitle(__("XML_PHONEBOOK_CHOOSE_PHONEBOOK") . ':');

        foreach ($books as $book) {
            $screen->addMenuItem($book->title, $URI . "&" . "pbid=" . $book->id, '');
        }
        $screen->addButton("1", __('XML_GLOBAL_EXIT_BTN'), $URI . "&exit=1");
        $screen->addButton("4", __("XML_GLOBAL_SELECT_BTN"));
        $screen->printScreen();
    }

    public function renderPhoneAdresses($bookName, $addresses, $addressCount)
    {
        if ($addressCount === '0') {
            return $this->renderNoEntryFound();
        }

        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlMenuScreen($screenAttributes);
        $screen->setTitle($bookName);

        foreach ($addresses as $address) {
            $screen->addMenuItem($address->phoneContactNames(), $this->getAddressesURI(array("addressid" => $address->id)), '');
        }
        $screen->addButton("1", __("XML_GLOBAL_SEARCH_BTN"), $this->getSearchScreenURI());
        $screen->addButton("2", __("XML_GLOBAL_BACK_BTN"), "SoftKey:Exit");
        if ($this->offset + AbstractXMLPhonebookApp::$limit < $addressCount) {
            $screen->addButton("3", __("XML_PHONEBOOK_NEXT"), $this->getNextPageURI());
        }
        $screen->addButton("4", __("XML_GLOBAL_SELECT_BTN"));
        $screen->printScreen();
    }

    public function renderPhoneAddress($address)
    {
        if (count($address->phoneNumbers()) === 0) {
            return $this->renderNoEntryFound();
        }
        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlMenuScreen($screenAttributes);
        $screen->setTitle($address->phoneContactNames());
        foreach ($address->phoneNumbers() as $name => $number) {
            if ($name == 'tel_work' && $address->contact_owner == 'intern') {
                $dialNumber = $number;
            } else {
                $dialNumber = $address->getCanonizedNumber($number);
            }
            $screen->addMenuItem($address->getAttributeLabel($name) . ": $number", $this->getAddressNumberURI() . "&dial=$number", "", $dialNumber, $address->getAttributeLabel($name));
        }
        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), "SoftKey:Exit");
        $screen->addButton("4", __("XML_GLOBAL_DIAL_BTN"), "SoftKey:Dial");
        $screen->printScreen();
    }

    public function renderSearchResult($header, $addresses, $addressesCount, $searchRequestText)
    {
        if (count($addresses) === 0) {
            return $this->renderNoEntryFound();
        }
        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlMenuScreen($screenAttributes);
        $screen->setTitle($header);

        foreach ($addresses as $address) {
            $screen->addMenuItem($address->phoneContactNames(), $this->getAddressesURI(array("addressid" => $address->id)), '');
        }
        $screen->addButton("1", __("XML_GLOBAL_SEARCH_BTN"), $this->getSearchScreenURI());
        $screen->addButton("2", __("XML_GLOBAL_BACK_BTN"), "SoftKey:Exit");
        if ($addressesCount > 20) {
            $screen->addButton("3", __("XML_PHONEBOOK_NEXT"), $this->getNextSearchPageURI($searchRequestText));
        }
        $screen->addButton("4", __("XML_GLOBAL_SELECT_BTN"));
        $screen->printScreen();
    }

    public function renderSearch()
    {
        require_once "lib/XmlInputScreen.php";
        $URI = $this->getSearchURI();
        $changedAttributes = array(
            'type' => 'string',
            'Password' => 'yes',
            'displayMode' => 'normal'
        );
        $screenAttributes = $this->getScreenAttributes($changedAttributes);
        $screen = new XmlInputScreen($screenAttributes);
        $screen->setTitle(__("XML_GLOBAL_SEARCH_BTN")); //Suche
        $screen->xw->writeElement("URL", $URI);
        $screen->addInputField("string", "no", "yes", __("XML_PHONEBOOK_SEARCH_TEXT") . ':', "search_request", "", "");

        $screen->addButton("1", __('XML_GLOBAL_EXIT_BTN'), "SoftKey:Exit");
        $screen->addButton("2", "ABC", "SoftKey:ChangeMode");
        $screen->addButton("3", __('XML_GLOBAL_DELETE_BTN'), "SoftKey:BackSpace");
        $screen->addButton("4", __('XML_GLOBAL_SELECT_BTN'), "SoftKey:Submit");

        $screen->printScreen();
    }

    public function renderNoEntryFound()
    {
        $screenAttributes = $this->getScreenAttributes();
        $screen = new XmlScreen("TiptelIPPhoneTextScreen", $screenAttributes);
        $screen->setTitle(__('XML_CONFIG_APP_ERROR_TITLE'));
        $screen->addText(__('XML_GLOBAL_NO_ENTRIES_FOUND'));
        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), "SoftKey:Exit");
        $screen->printScreen();
    }

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