<?php

include dirname(__FILE__) . '/../../../lib/AbstractXmlPhonebookApp.php';
require_once "lib/XmlMenuScreen.php";
require_once "lib/XmlPhoneDirectoryScreen.php";
require_once "lib/XmlInputScreen.php";
require_once "lib/XmlButtons.php";
require_once "lib/XmlTextScreen.php";
require_once "lib/XmlExit.php";

class XmlPhonebookApp extends AbstractXMLPhonebookApp
{

    private $screen = null;

    public function renderPhoneBooks($books)
    {
        $this->setPhonebookLanguage();
        $screen = new XmlMenuScreen();
        $screen->setTitle(__("XML_PHONEBOOK_CHOOSE_PHONEBOOK") . ':');

        foreach ($books as $book) {
            $screen->addMenuItem($book->title, $this->appURL(array('pbid' => $book->id)));
        }

        $screen->addButton(XmlIndexButtons::F1, __("EXIT"), $this->appURL(array('exit' => '1')));
        $screen->addButton(XmlIndexButtons::F4, __("XML_GLOBAL_SELECT_BTN"), "", XmlSoftkeyButtons::ENTER);
        $screen->printScreen();
    }

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

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

        foreach ($addresses as $address) {
            $screen->addMenuItem($address->phoneContactNames(), $this->getAddressesURI(array('addressid' => $address->id)));
        }

        $screen->addButton(XmlIndexButtons::F1, __("XML_GLOBAL_SEARCH_BTN"), $this->getSearchScreenURI());
        $screen->addButton(XmlIndexButtons::F2, __("XML_GLOBAL_BACK_BTN"), "", XmlSoftkeyButtons::CANCEL);
        if ($this->offset >= AbstractXMLPhonebookApp::$limit) {
            $screen->addButton(XmlIndexButtons::F2, __("<--"), "", XmlSoftkeyButtons::CANCEL);
        }

        if ($this->offset + AbstractXMLPhonebookApp::$limit < $addressCount) {
            $screen->addButton(XmlIndexButtons::F3, __("-->"), $this->getNextPageURI());
        }
        $screen->addButton(XmlIndexButtons::F4, __("XML_GLOBAL_SELECT_BTN"), "", XmlSoftkeyButtons::ENTER);
        $screen->printScreen();
    }

    public function renderPhoneAddress($address)
    {
        $this->setPhonebookLanguage();
        $screen = new XmlPhoneDirectoryScreen();
        $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->addDirectoryEntry($address->getAttributeLabel($name) . ": $number", $dialNumber);
        }
        $screen->addButton(XmlIndexButtons::F1, __("XML_GLOBAL_BACK_BTN"), "", XmlSoftkeyButtons::CANCEL);
        $screen->addButton(XmlIndexButtons::F4, __("XML_GLOBAL_SELECT_BTN"), "", XmlSoftkeyButtons::ENTER);
        $screen->printScreen();
    }

    public function renderSearch()
    {
        $this->setPhonebookLanguage();
        $URI = $this->getSearchURI();
        $screen = new XmlInputScreen();
        $screen->setURl($URI);
        $screen->setInputItem(__("XML_GLOBAL_SEARCH_BTN"), 'search_request', XmlInputFlags::EQUATIONS);
        $screen->printScreen();
    }

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

        foreach ($addresses as $address) {
            $screen->addMenuItem($address->phoneContactNames(), $this->appURL(array('addressid' => $address->id)));
        }

        $screen->addButton(XmlIndexButtons::F1, __("XML_GLOBAL_SEARCH_BTN"), $this->getSearchScreenURI());
        $screen->addButton(XmlIndexButtons::F2, __("XML_GLOBAL_BACK_BTN"), $this->getAddressesURI());
        if ($this->offset >= AbstractXMLPhonebookApp::$limit) {
            $screen->addButton(XmlIndexButtons::F2, __("<--"), "");
        }

        if ($addressesCount > 20) {
            $screen->addButton(XmlIndexButtons::F3, __("-->"), $this->getNextSearchPageURI($searchRequestText));
        }
        $screen->addButton(XmlIndexButtons::F4, __("XML_GLOBAL_SELECT_BTN"), "", XmlSoftkeyButtons::ENTER);
        $screen->printScreen();
    }

    public function renderNoEntryFound()
    {
        $this->setPhonebookLanguage();
        $screen = new XmlTextScreen();
        $screen->setText(__('XML_GLOBAL_NO_ENTRIES_FOUND'));
        $screen->addButton(XmlIndexButtons::F1, __("XML_GLOBAL_BACK_BTN"), "", XmlSoftkeyButtons::CANCEL);
        $screen->printScreen();
    }

    public function renderExit()
    {
        $screen = new XmlExit();
        $screen->printScreen();
    }
}
