<?php

require_once dirname(__FILE__) . '/../../lib/AbstractPhoneModel.php';

class Snom extends AbstractPhoneModel
{

    const EXTENSION_MODULE_MAX = 3;

    public function __construct($manufactorName = "Snom", $phoneModelName = "", $phoneUserAgent = "")
    {
        $this->mManufactorName = $manufactorName;
        $this->mPhoneModelName = $phoneModelName;
        $this->mPhoneUserAgent = $phoneUserAgent;
    }

    public static function getManufactorName()
    {
        return "Snom";
    }

    public static function getFunctionPlaceholders()
    {
        return array(
            'no' => 'PHONE_TIPTEL_FUNCTION_NOT_OVERRIDE',
            '0' => 'PHONE_SNOM_FUNCTION_EMPTY', // 'Leer'
            'speed' => 'PHONE_SNOM_FUNCTION_EXTERNAL_DESTINATION', //'externes Ziel'
            'dest' => 'PHONE_SNOM_FUNCTION_EXTENSION', //'Nebenstelle'
            'blf' => 'PHONE_SNOM_FUNCTION_BLF', //'BLF'
            'line' => 'PHONE_SNOM_FUNCTION_LINE', //'Leitung'
            'f_transfer' => 'PHONE_SNOM_FUNCTION_TRANSFER', //'Weiterleiten'
            'f_mute' => 'PHONE_SNOM_FUNCTION_MUTE', //'Mute'
            'f_conference' => 'PHONE_SNOM_FUNCTION_CONFERENCE', //'Konferenz'
            'f_call_list' => 'PHONE_SNOM_FUNCTION_CALL_LIST', //'Anrufliste'
            'X01' => 'PHONE_TIPTEL_FUNCTION_FORWARD', //Call Forward
            'X02' => 'PHONE_TIPTEL_FUNCTION_NIGHT_CIRCUT', // Night Circuit
            'X03' => 'PHONE_TIPTEL_FUNCTION_QUEUE_LOGIN', // Queue Login/Logout
            'X04' => 'PHONE_TIPTEL_FUNCTION_SPEEDDIAL_PHONALISA', // SpeedDial
            'X05' => 'PHONE_TIPTEL_FUNCTION_VOICEMAIL', // Voicemail
//            'X06' => 'PHONE_TIPTEL_FUNCTION_CALL_PICKUP', // Direct Pickup
            'X07' => 'PHONE_TIPTEL_FUNCTION_INTERCOM', // Intercom
            'X08' => 'PHONE_TIPTEL_FUNCTION_DND', // DND
            'X09' => 'PHONE_TIPTEL_FUNCTION_LISTENING', // Group Listening
            'X10' => 'PHONE_TIPTEL_FUNCTION_ALL_QUEUES_LOGOUT', // Logout from all Queues
            'X11' => 'PHONE_TIPTEL_FUNCTION_HOT_DESKING', // Hot Desking
            'X12' => 'PHONE_TIPTEL_FUNCTION_ALL_QUEUES_LOGIN', // Login to all Queues
            'X13' => 'PHONE_TIPTEL_FUNCTION_CALLGROUP_LOGIN', // Call Group Login/Logout
        );
    }

    public static function getFunctions($translated = true)
    {
        if ($translated) {
            $functions = array();
            foreach (self::getFunctionPlaceholders() as $key => $value) {
                $functions[$key] = __($value);
            }
            return $functions;
        } else {
            return self::getFunctionPlaceholders();
        }
    }

    public function validateFirmwareFile($filename, $fileTmpPath, &$errorText)
    {
        // Filename ist like: snom870-8.7.3.19-SIP-r.bin
        //                    snom870-8.4.35-SIP-r.bin
        if ($filename == "") {
            $errorText = __("FIRMWARE_VALIDATION_ERROR_FILE_NOT_SET");
            return false;
        }

        $errorText = __("FIRMWARE_VALIDATION_ERROR_WRONG_FILE_IS_NOT_VALID");
        // check filename
        if (!$this->endsWith($filename, ".bin")) {
            return false;
        }

        $filename = substr($filename, 0, strlen($filename) - 4);
        $parts = explode("-", $filename);
        //$errorText = print_r($parts, true);

        if (count($parts) != 4) {
            return false;
        }

        $ok = false;
        if ($this->mPhoneModelName != $parts[0]) {
            $errorText = __("FIRMWARE_VALIDATION_ERROR_WRONG_FILE_IS_NOT_A_FIRMWARE_FOR") . ": " . $this->mPhoneModelNameHuman;
            return false;
        }

        if ($parts[2] != "SIP") {
            return false;
        }

        if ($parts[3] != "r" && $parts[3] != "f") {
            return false;
        }

        $vParts = explode(".", $parts[1]);
        $vPartsNormal = array();
        $vPartsNormal[] = str_pad($vParts[0], 2, '0', STR_PAD_LEFT);
        $vPartsNormal[] = str_pad($vParts[1], 2, '0', STR_PAD_LEFT);
        $vPartsNormal[] = str_pad($vParts[2], 2, '0', STR_PAD_LEFT);
        if (count($vParts) > 3) {
            $vPartsNormal[] = str_pad($vParts[3], 2, '0', STR_PAD_LEFT);
        } else {
            $vPartsNormal[] = "00";
        }

        $firmwareVersion = implode(".", $vPartsNormal);

        return $firmwareVersion;
    }

    public function getAvailableKeys($level)
    {
        $phones = $this->getSupportedPhoneModels();
        if (!array_key_exists($this->mPhoneModelName, $phones)) {
            return array();
        }

        $phone = $phones[$this->mPhoneModelName];
        $ret = array();

        if ($level == "" || $level == "memory") {
            for ($i = $phone->getLineKeys(); $i < $phone->getKeys() + $phone->getLineKeys(); $i++) {
                $button = new PhoneKey();
                $button->key_idx = $i;
                $button->name = $i + 1;
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'memory';
                $button->snomKeyLabel = $button->name;
                $button->availableFunctions = $this->getFunctions();
                $ret[$button->name] = $button;
            }
        }

        if ($level == "" || $level == "line") {
            for ($i = 0; $i < $phone->getLineKeys(); $i++) {
                $button = new PhoneKey();
                $button->key_idx = $i;
                $button->name = $i + 1;
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'line';
                $button->snomKeyLabel = $button->name;
                $button->availableFunctions = $this->getFunctions();
                $ret[$button->name] = $button;
            }
        }

        $extKeys = $phone->getExtKeys();
        if ((int) $extKeys == 0) {
            return $ret;
        }
        $extModules = $phone->getExtModules();
        $extStartNum = $this->getKeys() + $extModules - 1;
        $extEndNum = $extKeys + $this->getKeys() - 1;
        $buttonLabel = 1;
        for ($i = 0; $i <= $extModules - 1; $i++) {
            if ($level == "" || $level == 'ext' . $i) {
                for ($j = $extStartNum; $j <= $extEndNum; $j++) {
                    $button = new PhoneKey();
                    $button->phone = $this->mPhoneModelName;
                    $button->function = '';
                    $button->level = 'ext' . $i;
                    $button->key_idx = "ext" . $i . '.' . $j;
                    $button->name = $j + 1;
                    $button->snomKeyLabel = $buttonLabel++;
                    $button->availableFunctions = $this->getFunctions(true, $button->phone, $button->level);
                    $ret[$button->name] = $button;
                }
            }
        }

        $usbExtStartNum = ($phone->getExtKeys() * self::EXTENSION_MODULE_MAX) + $extStartNum;
        $usbExtEndNum = (($usbExtStartNum + $phone->getUsbExtKeys() ) * $phone->getUsbExtModules()) - 1;
        $buttonLabel = 1;
        for ($i = 0; $i <= $phone->getUsbExtModules() - 1; $i++) {
            if ($level == "" || $level == 'usb' . $i) {
                for ($j = $usbExtStartNum; $j <= $usbExtEndNum; $j++) {
                    $button = new PhoneKey();
                    $button->phone = $this->mPhoneModelName;
                    $button->function = '';
                    $button->level = 'usb' . $i;
                    $button->key_idx = "usb" . $i . '.' . $j;
                    $button->name = $j + 1;
                    $button->snomKeyLabel = $buttonLabel++;
                    $button->availableFunctions = $this->getFunctions(true, $button->phone, $button->level);
                    $ret[$button->name] = $button;
                }
            }
        }

        return $ret;
    }

    public function getAvailableKeyLevels()
    {
        $phones = $this->getSupportedPhoneModels();
        if (!array_key_exists($this->mPhoneModelName, $phones)) {
            return array();
        }
        $phone = $phones[$this->mPhoneModelName];

        $keyLevels = array();

        if ($phone->getLineKeys() != 0) {
            $lineKeyLevel = array();
            $lineKeyLevel["level"] = "line";
            $lineKeyLevel["name"] = "TIPTEL_KEYLEVEL_LINE_KEYS_NAME";
            $lineKeyLevel["descr"] = "TIPTEL_KEYLEVEL_LINE_KEYS_DESCR";
            $keyLevels[] = $lineKeyLevel;
        }

        if ($phone->getKeys() != 0) {
            $memoryKeyLevel = array();
            $memoryKeyLevel["level"] = "memory";
            $memoryKeyLevel["name"] = "TIPTEL_KEYLEVEL_MEMORY_KEYS_NAME";
            $memoryKeyLevel["descr"] = "TIPTEL_KEYLEVEL_MEMORY_KEYS_DESCR";
            $keyLevels[] = $memoryKeyLevel;
        }

        $extModules = $phone->getExtModules();
        if ($phone->getExtKeys() != 0) {
            for ($i = 0; $i <= $extModules - 1; $i++) {
                $ExtKeyLevel = array();
                $ExtKeyLevel["level"] = "ext" . $i;
                $ExtKeyLevel["name"] = "SNOM_KEYLEVEL_EXT" . $i . "_NAME";
                $ExtKeyLevel["descr"] = "SNOM_KEYLEVEL_EXT" . $i . "_DESCR";
                $keyLevels[] = $ExtKeyLevel;
            }
        }

        $extUsbModules = $phone->getUsbExtModules();
        if ($phone->getUsbExtKeys() != 0) {
            for ($i = 0; $i <= $extUsbModules - 1; $i++) {
                $UsbExtKeyLevel = array();
                $UsbExtKeyLevel["level"] = "usb" . $i;
                $UsbExtKeyLevel["name"] = "SNOM_KEYLEVEL_USB_EXT" . $i . "_NAME";
                $UsbExtKeyLevel["descr"] = "SNOM_KEYLEVEL_USB_EXT" . $i . "_DESCR";
                $keyLevels[] = $UsbExtKeyLevel;
            }
        }

        return $keyLevels;
    }

    public static function getSupportedPhoneModels()
    {
        $models = array();

        $tmp = new Snom(Snom::getManufactorName(), "snom300", "snom300");
        $tmp->setPhoneModelNameHumanReadable("snom 300");
        $tmp->setKeys(6);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom320", "snom320");
        $tmp->setPhoneModelNameHumanReadable("snom 320");
        $tmp->setKeys(12);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom360", "snom360");
        $tmp->setPhoneModelNameHumanReadable("snom 360");
        $tmp->setKeys(12);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom370", "snom370");
        $tmp->setPhoneModelNameHumanReadable("snom 370");
        $tmp->setKeys(12);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom710", "snom710");
        $tmp->setPhoneModelNameHumanReadable("snom 710");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom720", "snom720");
        $tmp->setPhoneModelNameHumanReadable("snom 720");
        $tmp->setKeys(18);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom760", "snom760");
        $tmp->setPhoneModelNameHumanReadable("snom 760");
        $tmp->setKeys(12);
        $tmp->setLineKeys(4);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom820", "snom820");
        $tmp->setPhoneModelNameHumanReadable("snom 820");
        $tmp->setKeys(0);
        $tmp->setLineKeys(4);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom821", "snom821");
        $tmp->setPhoneModelNameHumanReadable("snom 821");
        $tmp->setKeys(0);
        $tmp->setLineKeys(4);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snom870", "snom870");
        $tmp->setPhoneModelNameHumanReadable("snom 870");
        $tmp->setKeys(15);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD120", "snomD120");
        $tmp->setPhoneModelNameHumanReadable("snom D120");
        $tmp->setKeys(0);
        $tmp->setLineKeys(2);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD305", "snomD305");
        $tmp->setPhoneModelNameHumanReadable("snom D305");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD315", "snomD315");
        $tmp->setPhoneModelNameHumanReadable("snom D315");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $tmp->setUsbExtKeys(18);
        $tmp->setUsbExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD345", "snomD345");
        $tmp->setPhoneModelNameHumanReadable("snom D345");
        $tmp->setKeys(48);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $tmp->setUsbExtKeys(18);
        $tmp->setUsbExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD375", "snomD375");
        $tmp->setPhoneModelNameHumanReadable("snom D375");
        $tmp->setKeys(12);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $tmp->setUsbExtKeys(18);
        $tmp->setUsbExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD385", "snomD385");
        $tmp->setPhoneModelNameHumanReadable("snom D385");
        $tmp->setKeys(48);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(42);
        $tmp->setExtModules(1);
        $tmp->setUsbExtKeys(18);
        $tmp->setUsbExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD710", "snomD710");
        $tmp->setPhoneModelNameHumanReadable("snom D710");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD712", "snomD712");
        $tmp->setPhoneModelNameHumanReadable("snom D712");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD715", "snomD715");
        $tmp->setPhoneModelNameHumanReadable("snom D715");
        $tmp->setKeys(5);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD725", "snomD725");
        $tmp->setPhoneModelNameHumanReadable("snom D725");
        $tmp->setKeys(18);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD735", "snomD735");
        $tmp->setPhoneModelNameHumanReadable("snom D735");
        $tmp->setKeys(8);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD745", "snomD745");
        $tmp->setPhoneModelNameHumanReadable("snom D745");
        $tmp->setKeys(32);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD765", "snomD765");
        $tmp->setPhoneModelNameHumanReadable("snom D765");
        $tmp->setKeys(12);
        $tmp->setLineKeys(4);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomD785", "snomD785");
        $tmp->setPhoneModelNameHumanReadable("snom D785");
        $tmp->setKeys(24);
        $tmp->setLineKeys(0);
        $tmp->setExtKeys(18);
        $tmp->setExtModules(1);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Snom(Snom::getManufactorName(), "snomPA1", "snomPA1");
        $tmp->setPhoneModelNameHumanReadable("snom PA1");
        $tmp->setKeys(0);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        return $models;
    }
}
