<?php

//require_once 'PhoneModelFactory.php' ;
include dirname(__FILE__) . '/../../../lib/AbstractXmlPhonebookApp.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/IppTicker.php";
require_once "lib/IppHidden.php";


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

class XmlPhonebookApp extends AbstractXMLPhonebookApp
{

    private $screen = null;

    //protected static $limit = 20;

    public function renderPhoneBooks($books)
    {
        $screen = new IppScreen(1, 0, 5);
        $title = count($books) == 1 ? __('PHONEBOOK') : __('PHONEBOOKS');
        $list = new GenericList($screen, IppList::TypeImplicit, 1, $this->getPhonebookURI($this->offset), $title);
        foreach ($books as $book) {
            $item = new GenericListItem($book, 'title', 'pbid', 'id', $book->getIconPath());

            $list->addItem($item);
        }
        $list->printXml();
    }

    public function renderPhoneAdresses($bookName, $addresses, $addressesCount, $search = null, $search_done = null)
    {
        $screen = new IppScreen(2, 1, 5);
        $list = new GenericList($screen, IppList::TypeImplicit, 1, $this->getAddressesURI(), $bookName);

        $ippKey = new IppKey(IppKey::KeypadTrue, IppKey::SendKeyTrue, 'keypadSearch', IppKey::TermKeyTest, IppKey::BufferKeysSubsequent);

        if (isset($search_done)) {
            $ippHidden = new IppHidden(IppHidden::TypeVALUE, 'search_done');
            $ippHidden->setHiddenValue($search_done . '+');
            $screen->addUIElement($ippHidden);
        }

        $list->addCustomItem('Suche', "search", "1", $this->phoneNumberIcons("search"));

        //prev page button
        if ($this->offset > 0) {
            $offset = $this->offset - AbstractXMLPhonebookApp::$limit;
            $list->addCustomItem("vorherige Seite", 'offset', $offset, $this->phoneNumberIcons("back_arrow"));
        }

        $list->addCustomItem("Zurück zu den Adressbüchern", "backToPhonebooks", "pressed", $this->phoneNumberIcons("back_arrow"));

        foreach ($addresses as $key => $address) {
            $address->n_fn = getFormatedNameToUser($address, 'addressbook');
            //FIXME ME ! phoneContactPhotos($address)
            $item = new GenericListItem($address, 'n_fn', 'addressid', 'id', $address->getPhotoUrl($size = 40));
            $list->addItem($item);
        }

        //next page button
        if (count($addresses) >= 20) {
            $offset = $this->offset + AbstractXMLPhonebookApp::$limit;
            $list->addCustomItem("nächste Seite", 'offset', $offset, $this->phoneNumberIcons("forward_arrow"));
        }

        $screen->addUIElement($ippKey);
        $list->printXml();
    }

    public function renderPhoneAddress($address)
    {
        $screen = new IppScreen(3, 0, 5);
        $list = new GenericList($screen, IppList::TypeImplicit, 1, $this->getAddressNumberURI(), getFormatedNameToUser($address, 'addressbook'));
        $list->addCustomItem("Zurück zum Adressbuch", "backToContacts", "pressed", $this->phoneNumberIcons("back_arrow"));
        foreach ($address->phoneNumbers() as $name => $number) {
            if ($name == 'tel_work' && $address->contact_owner == 'intern') {
                $dialNumber = $number;
            } else {
                $dialNumber = $address->getCanonizedNumber($number);
            }
            $list->addCustomItem($number, "contactnumber", $dialNumber, $this->phoneNumberIcons($name));
        }
        $list->printXml();
    }

    public function renderAddressNumber($address)
    {
        $screen = new IppScreen(4, 0, 5);
        $command = new GenericCommand($screen, array(
            'alert' => array(
                'type' => IppAlert::TypeConfirmation,
                'delay' => IppAlert::DelayForever,
                'title' => getFormatedNameToUser($address, 'addressbook') . ' anrufen?',
                //FIXME refactor getAddressNumberURI()
                'url' => $this->getAddressNumberURI() . "&makeCall=1&contactnumber=" . $this->contactNumber,
                'text' => "Rufnummer: " . $this->contactNumber,
                'image' => $address->getPhotoUrl($size = 40)
            ),
            'commands' => array(
                'Anrufen' => array(
                    'type' => IppCommand::TypeSelect,
                    'displayOn' => IppCommand::DisplayOnBoth,
                ),
                'Abbrechen' => array(
                    'type' => IppCommand::TypeCancel,
                    'displayOn' => IppCommand::DisplayOnBoth
                )
            )
                ), IppCommand::TypeSelect, IppCommand::DisplayOnBoth);
        $command->printXml();
    }

    public function renderMakeCall($address)
    {
        $screen = new IppScreen(5, 0, 0);
        $command = new GenericCommand($screen, array(
            'alert' => array(
                'type' => IppAlert::TypeConfirmation,
                'delay' => '1', // Delay in miliseconds
                'title' => getFormatedNameToUser($address, 'addressbook') . ' anrufen.',
                'text' => "Rufnummer: " . $this->contactNumber,
            ),
            'action' => array(
                'type' => IppAction::TypeMakeCall,
                'number' => $this->contactNumber,
            )
                ), IppCommand::TypeSelect, IppCommand::DisplayOnBoth);
        $command->printXml();
    }

    public function renderSearch()
    {
        $URI = $this->getSearchScreenURI();

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

        $ippForm = new IppForm(IppForm::Proportion40_60, 2);
        $ippForm->setUrl($URI);
        $ippForm->setTitle("Suche");

        $ippCommand = new IppCommand(IppCommand::TypeSelect, IppCommand::DisplayOnBoth, 0);
        $ippCommand->setLabel("Suchen");

        $ippCommandBack = new IppCommand(IppCommand::TypeBack, IppCommand::DisplayOnBoth, 0);
        $ippCommandBack->setLabel("Abbrechen");

        $searchForNumberField = new IppTextField("search_request", 30, IppTextField::ConstraintANY);
        $searchForNumberField->setLabel("Suchanfrage:");

        $ippForm->addUIElement($searchForNumberField);

        $screen->addUIElement($ippForm);
        $screen->addUIElement($ippCommand);
        $screen->addUIElement($ippCommandBack);
        $phone->addScreen($screen);
        $phone->printScreen();
    }

    public function renderSearchResult($header, $addresses, $addressesCount, $searchRequestText)
    {
        $screen = new IppScreen(2, 1, 5);
        $list = new GenericList($screen, IppList::TypeImplicit, 1, $this->getAddressesURI(), $header);

        $ippKey = new IppKey(IppKey::KeypadTrue, IppKey::SendKeyTrue, 'keypadSearch', IppKey::TermKeyTest, IppKey::BufferKeysSubsequent);

        //prev page button
        if ($this->offset > 0) {
            $offset = $this->offset - AbstractXMLPhonebookApp::$limit;
            $list->addCustomItem("vorherige Seite", 'offset', $offset, $this->phoneNumberIcons("back_arrow"));
        }

        $list->addCustomItem("Zurück zu den Adressbüchern", "backToPhonebooks", "pressed", $this->phoneNumberIcons("back_arrow"));

        foreach ($addresses as $key => $address) {
            $address->n_fn = getFormatedNameToUser($address, 'addressbook');
            //FIXME ME ! phoneContactPhotos($address)
            $item = new GenericListItem($address, 'n_fn', 'addressid', 'id', $address->getPhotoUrl($size = 40));
            $list->addItem($item);
        }

        //next page button
        if (count($addresses) >= 20) {
            $offset = $this->offset + AbstractXMLPhonebookApp::$limit;
            $list->addCustomItem("nächste Seite", 'offset', $offset, $this->phoneNumberIcons("forward_arrow"));
        }

        $screen->addUIElement($ippKey);
        $list->printXml();
    }
}
