<?php

require_once dirname(__FILE__) . '/../tiptel-2xx/Tiptel.php';

class Tiptel30xx extends Tiptel
{

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

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

    public static function getFunctionPlaceholders()
    {
        return array(
            'no' => 'PHONE_TIPTEL_FUNCTION_NOT_OVERRIDE',
            '0' => 'PHONE_TIPTEL_FUNCTION_EMPTY',
            '1' => 'PHONE_TIPTEL_FUNCTION_LINE', // Line (for line key only)
            '2' => 'PHONE_TIPTEL_FUNCTION_SPEEDDIAL', // SpeedDial
            '3' => 'PHONE_TIPTEL_FUNCTION_BUSY_LAMP_FIELD', // BLF
            '22' => 'PHONE_TIPTEL_FUNCTION_REDIAL', // Redial
            '25' => 'PHONE_TIPTEL_FUNCTION_RECORD', // Record
            '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
        );
    }

    public static function getFunctions($translated = true, $phone = '', $level = '')
    {
        $placeholders = self::getFunctionPlaceholders();

        if ($translated) {
            $functions = array();
            foreach ($placeholders as $key => $value) {
                $functions[$key] = __($value);
            }
            return $functions;
        } else {
            return $placeholders;
        }
    }

    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 = 1; $i <= $phone->getKeys(); $i++) {
                $name = 'Memory ' . $i;
                $level = 'memory';
                $keyIdx = $i;
                $ret[$name] = $this->createNewPhoneKey($name, $level, $keyIdx);
            }
        }

        if ($level == "" || $level == "line") {
            for ($i = 1; $i <= $phone->getLineKeys(); $i++) {
                $name = 'Line ' . $i;
                $level = 'line';
                $keyIdx = $i;
                $ret[$name] = $this->createNewPhoneKey($name, $level, $keyIdx);
            }
        }

        $extKeys = $phone->getExtKeys();
        $extModules = $phone->getExtModules();
        for ($i = 0; $i <= $extModules - 1; $i++) {
            if ($level == "" || $level == 'ext' . $i) {
                for ($j = 1; $j <= $extKeys; $j++) {
                    $name = "Ext " . ($i + 1) . '.' . $j;
                    $level = 'ext' . $i;
                    $keyIdx = "ext" . $i . '.' . $j;
                    $ret[$name] = $this->createNewPhoneKey($name, $level, $keyIdx);
                }
            }
        }
        return $ret;
    }

    public function createNewPhoneKey($name, $level, $keyIdx)
    {
        $button = new PhoneKey();
        $button->name = $name;
        $button->phone = $this->mPhoneModelName;
        $button->function = '';
        $button->level = $level;
        $button->key_idx = $keyIdx;
        $button->availableFunctions = $this->getFunctions(true, $button->phone, $button->level);
        return $button;
    }

    public function getAvailableKeyLevels()
    {
        return parent::getAvailableKeyLevels();
    }

    public static function getSupportedPhoneModels()
    {
        $tmp = new Tiptel30xx(Tiptel30xx::getManufactorName(), "tiptel-3010", "tiptel3010");
        $tmp->setPhoneModelNameHumanReadable("tiptel 3010");
        $tmp->setKeys(5);
        $tmp->setLineKeys(2);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Tiptel30xx(Tiptel30xx::getManufactorName(), "tiptel-3020", "tiptel3020");
        $tmp->setPhoneModelNameHumanReadable("tiptel 3020");
        $tmp->setKeys(10);
        $tmp->setLineKeys(4);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Tiptel30xx(Tiptel30xx::getManufactorName(), "tiptel-3030", "tiptel3030");
        $tmp->setPhoneModelNameHumanReadable("tiptel 3030");
        $tmp->setKeys(10);
        $tmp->setLineKeys(4);
        $tmp->setExtKeys(40);
        $tmp->setExtModules(3);
        $models[$tmp->getPhoneModelName()] = $tmp;

        return $models;
    }

    public function validateFirmwareFile($filename, $fileTmpPath, &$errorText)
    {
        $firmwareKey = substr($filename, 2, strlen($filename) - 2); // remove fw
        $firmwareKey = substr($firmwareKey, 0, strlen($firmwareKey) - 4); // remove .rom

        $errorText = __("FIRMWARE_VALIDATION_ERROR_WRONG_FILE_IS_NOT_VALID");
        $phoneName = $this->getPhoneModelName();

        $result = false;
        if (
                ($firmwareKey == '803' && $phoneName == 'tiptel-3010') ||
                ($firmwareKey == '806' && $phoneName == 'tiptel-3020') ||
                ($firmwareKey == '862' && $phoneName == 'tiptel-3030') ||
                ($firmwareKey == '924' && $phoneName == 'tiptel-uc924') ||
                ($firmwareKey == '926' && $phoneName == 'tiptel-uc926')
        ) {
            $result = $this->getFirmwareVersionFromBinFile($fileTmpPath);
        }
        return $result;
    }

    protected function getFirmwareVersionFromBinFile($fileTmpPath)
    {
        $firmwareVersion = '';
        if (!empty($fileTmpPath)) {
            $data = file_get_contents($fileTmpPath);
            $firmwareBinInfo = unpack('c10bin', $data);
            if (isset($firmwareBinInfo['bin7']) && isset($firmwareBinInfo['bin8']) && isset($firmwareBinInfo['bin9']) && isset($firmwareBinInfo['bin10'])) {
                $firmware = $firmwareBinInfo['bin7'] . '.' . $firmwareBinInfo['bin8'] . '.' . $firmwareBinInfo['bin9'] . '.' . $firmwareBinInfo['bin10'];
                $firmwareVersion = Tiptel30xx::tiptel_normalize_version($firmware);
            }
        }
        return $firmwareVersion;
    }

    public static function tiptel_normalize_version($fwvers)
    {
        $tmp = explode('.', $fwvers);
        $v0 = str_pad((int) @$tmp[0], 2, '0', STR_PAD_LEFT);
        $v1 = str_pad((int) @$tmp[1], 2, '0', STR_PAD_LEFT);
        $v2 = str_pad((int) @$tmp[2], 2, '0', STR_PAD_LEFT);
        $v3 = str_pad((int) @$tmp[3], 2, '0', STR_PAD_LEFT);
        return "$v0.$v1.$v2.$v3";
    }
}
