<?php

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

class XmlVoicemailApp extends AbstractXmlVoicemailApp
{

    private $screen = null;

    private function getScreenAttributes($changeAttribute = array())
    {
        $this->setVoicemailLanguage();
        $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 renderVoicemailStartMenu()
    {
        $URI = $this->getVoicemailURI();

        $screenAttributes = $this->getScreenAttributes();

        $screen = new XmlMenuScreen($screenAttributes);

        $screen->setTitle(__("XML_VOICEMAIL_APP_MENU_START_TITLE") . ": $this->vmTotalCount");

        $screen->addMenuItem(__('XML_VOICEMAIL_APP_NEW_MESSAGES') . ": $this->vmNewCount", $URI, "vmTyp=new");
        $screen->addMenuItem(__('XML_VOICEMAIL_APP_OLD_MESSAGES') . ": $this->vmOldCount", $URI, "vmTyp=old");

        $screen->addButton("1", __('XML_GLOBAL_EXIT_BTN'), $URI . "?exit=1");
        $screen->addButton("4", __('XML_GLOBAL_SELECT_BTN'));

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

    public function renderSelectedVoicemailFolder($selectedVoicemails, $voicemailType)
    {
        $URI = $this->getVoicemailURI();
        $currentOffset = $this->offset;
        $currentPage = $this->page;
        if ($currentOffset == 0 || $currentPage == 0) {
            $currentPage = 1;
        }
        $screenAttributes = $this->getScreenAttributes();

        $screen = new XmlMenuScreen($screenAttributes);

        $title = 'XML_VOICEMAIL_APP_NEW_MESSAGE_TITLE';
        $vmCount = $this->vmNewCount;
        if ($voicemailType === 'old') {
            $title = 'XML_VOICEMAIL_APP_OLD_MESSAGE_TITLE';
            $vmCount = $this->vmOldCount;
        }
        $totalPage = $this->getTotalPage($vmCount);

        $fullTitle = __($title, true, 'messages', array("{count}" => $vmCount));
        $titleWithPage = $fullTitle;

        foreach ($selectedVoicemails as $voicemail) {
            $vmID = $voicemail->id;
            $dateTime = $this->getVoicemailDateTime($vmID);

            $date = PhonalisaDate::userDate($dateTime);
            $time = PhonalisaDate::userTime($dateTime);

            $extension = $voicemail->from;
            $screen->addMenuItem("$date $time $extension", $this->getVoicemailTypURI() . "&vmID=$vmID", '', $this->getUCItemDialToListenNumber($vmID));
        }

        if ($currentPage != $totalPage) {
            $titleWithPage = $fullTitle . "   $currentPage/$totalPage";
            $screen->addButton("3", __("-->"), $this->getNextVoicemails($currentOffset, $currentPage));
        }

        if ($currentPage > 1) {
            if ($this->offset < $this->vmTotalCount) {
                $titleWithPage = $fullTitle . "   $currentPage/$totalPage";
                $screen->addButton("2", __("<--"), $this->getPrevVoicemails($currentOffset, $currentPage));
            }
        }
        $screen->addButton("6", __("XML_GLOBAL_DIAL_BTN"), "SoftKey:Dial");
        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), $URI);
        $screen->addButton("5", __("XML_VOICEMAIL_APP_DETAILS_BTN"));

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

    public function renderVoicemailDetails($voicemail)
    {
        $URI = $this->getVoicemailTypURI();

        $changedAttributes = array('doneAction' => $this->getDialVoicemailURI($voicemail->id));
        $screenAttributes = $this->getScreenAttributes($changedAttributes);

        $dateTime = $this->getVoicemailDateTime($voicemail->id);

        $date = PhonalisaDate::userDate($dateTime);
        $time = PhonalisaDate::userTime($dateTime, true);

        $extension = $voicemail->from;

        $dateTranslation = __("XML_VOICEMAIL_APP_DATE");
        $timeTranslation = __("XML_VOICEMAIL_APP_TIME");
        $extensionTranslation = __("XML_VOICEMAIL_APP_EXTENSION");
        $nameTranslation = __('XML_VOICEMAIL_APP_CALLER_NAME');

        $screen = new XmlScreen('YealinkIPPhoneTextScreen', $screenAttributes);

        $findExtension = array(
            'tel_work_ic' => $extension,
            'tel_cell_ic' => $extension,
            'tel_home_ic' => $extension,
            'tel_cell_private_ic' => $extension,
            'tel_assistent_ic' => $extension
        );

        $text = "$dateTranslation: $date\n$timeTranslation: $time\n$extensionTranslation: $extension";

        if (is_numeric($extension)) {
            $name = $this->getNameFromExtension($extension);
            if ($name != '') {
                $text = $text . "\n$nameTranslation: $name";
            }
        }

        $screen->setTitle(__('XML_VOICEMAIL_APP_VOICEMAIL_DETAIL_TITLE'), 'normal', 'center');
        $screen->addText($text);

        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), $URI);
        $screen->addButton("3", __("XML_GLOBAL_DELETE_BTN"), $this->getDeleteVoicemailURI($voicemail->id));
        $screen->addButton("4", __("XML_VOICEMAIL_APP_DIAL_BTN"), $this->getDialVoicemailURI($voicemail->id));

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

    public function renderVoicemailRequestMenu($useCase)
    {
        $URI = $this->getVoicemailDetailURI();

        $screenAttributes = $this->getScreenAttributes();

        $screen = new XmlMenuScreen($screenAttributes);

        $screen->addButton("1", __("XML_VOICEMAIL_APP_NEGATE_REQUEST_BTN"), $URI);

        if ($useCase === 'delete') {
            $screen->addMenuItem(__("XML_VOICEMAIL_APP_DELETE_CONFIRM_MESSAGE"), '', '');
            $dialCmd = $URI . "&deleteVm=true";
        } else {
            $screen->addMenuItem(__("XML_VOICEMAIL_APP_DIAL_CONFIRM_MESSAGE"), '', '', $this->getUCItemDialToListenNumber());
            $dialCmd = "SoftKey:Dial";
        }
        $screen->addButton("4", __("XML_VOICEMAIL_APP_CONFIRM_REQUEST_BTN"), $dialCmd);
        $screen->printScreen($this->getPhoneModel()->type);
    }

    public function renderNoEntryFound()
    {
        $URI = $this->getVoicemailURI();

        $changedAttributes = array(
            'style' => 'none',
            'doneAction' => $URI
        );
        $screenAttributes = $this->getScreenAttributes($changedAttributes);

        $screen = new XmlScreen('YealinkIPPhoneTextScreen', $screenAttributes);

        $screen->addText(__("XML_VOICEMAIL_APP_NO_MESSAGES_FOUND"));
        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), $URI);

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

    public function renderVoicemailDeleted()
    {
        $URI = $this->getVoicemailTypURI();

        $changedAttributes = array(
            'style' => 'none',
            'doneAction' => $URI
        );
        $screenAttributes = $this->getScreenAttributes($changedAttributes);

        $screen = new XmlScreen('YealinkIPPhoneTextScreen', $screenAttributes);

        $screen->addText(__("XML_VOICEMAIL_APP_MESSAGE_DELETED_INFO"));
        $screen->addButton("1", __("XML_GLOBAL_BACK_BTN"), $URI);

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

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