<?php

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

class Siemens extends AbstractPhoneModel
{

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

    public function validateFirmwareFile($filename, $fileTmpPath, &$errorText)
    {
        // Filename ist like: OS_60_SIP_V3_R1_41_0.img
        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, ".img")) {
            return false;
        }

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

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

        if ($parts[0] != "OS") {
            $errorText = __("FIRMWARE_VALIDATION_ERROR_WRONG_FILE_IS_NOT_A_OS_FIRMWARE");
            return false;
        }

        $ok = false;
        if ($this->mPhoneModelName == "os60" && $parts[1] == "60") {
            $ok = true;
        } elseif ($this->mPhoneModelName == "os80" && $parts[1] == "80") {
            $ok = true;
        } elseif ($this->mPhoneModelName == "os40" && $parts[1] == "40") {
            $ok = true;
        } elseif ($this->mPhoneModelName == "os20" && $parts[1] == "20") {
            $ok = true;
        } elseif ($this->mPhoneModelName == "os15" && $parts[1] == "15") {
            $ok = true;
        }

        if ($ok == false) {
            $errorText = __("FIRMWARE_VALIDATION_ERROR_WRONG_FILE_IS_NOT_A_FIRMWARE_FOR") . ": " . $this->mPhoneModelNameHuman;
            return false;
        }

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

        if ($parts[3][0] != "V" && $parts[4][0] != "R") {
            return false;
        }

        $p1 = $parts[3][1];
        $p2 = $parts[4][1];
        $p3 = $parts[5];
        $p4 = $parts[6];

        $p1 = str_pad($p1, 2, '0', STR_PAD_LEFT);
        $p2 = str_pad($p2, 2, '0', STR_PAD_LEFT);
        $p3 = str_pad($p3, 2, '0', STR_PAD_LEFT);
        $p4 = str_pad($p4, 2, '0', STR_PAD_LEFT);

        $version = $p1 . "." . $p2 . "." . $p3 . "." . $p4;

        //$errorText = "$filename!";
        return $version;
    }

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

    public static function getFunctionPlaceholders()
    {
        return array(
            'no' => 'PHONE_TIPTEL_FUNCTION_NOT_OVERRIDE',
            '0' => 'PHONE_SIEMENS_FUNCTION_EMPTY', //'Leer',               # clear
            '1' => 'PHONE_SIEMENS_FUNCTION_SELECTED_DIALING', //'Zielwahl',           # selected dialing
            '9' => 'PHONE_SIEMENS_FUNCTION_RINGER_OFF', //'Rufton aus',         # ringer off
            '10' => 'PHONE_SIEMENS_FUNCTION_HOLD', //'Halten',             # hold
            '11' => 'PHONE_SIEMENS_FUNCTION_ALTERNATE', //'Makeln',             # alternate
            '13' => 'PHONE_SIEMENS_FUNCTION_ATTENDED_TRANSFER', //'Übergabe',           # attended transfer
            '12' => 'PHONE_SIEMENS_FUNCTION_BLIND_TRANSFER', //'Überg. v. Melden',   # blind transfer
            '14' => 'PHONE_SIEMENS_FUNCTION_DEFLECT', //'Weiterleiten',       # deflect
            '18' => 'PHONE_SIEMENS_FUNCTION_SHIFT', //'Shift',              # shift
            '24' => 'PHONE_SIEMENS_FUNCTION_HEADSET', //'Kopfhörer',          # headset
            '50' => 'PHONE_SIEMENS_FUNCTION_CONSULTATION', //'Rückfrage',          # consultation
            '56' => 'PHONE_SIEMENS_FUNCTION_RECORD', //'Call Recording',          # consultation
            '58' => 'PHONE_SIEMENS_FUNCTION_FEATURE_TOGGLE', //'Fn.-Schalter',       # feature toggle   .._shifted_ok?
            '59' => 'PHONE_SIEMENS_FUNCTION_EXTENSION_BLF', //'Nebenstelle/BLF',    # extension
            '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 getAvailableKeys($level)
    {
        $phones = $this->getSupportedPhoneModels();
        if (!array_key_exists($this->mPhoneModelName, $phones)) {
            return array();
        }

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

        if ($level == "" || $level == "fpk") {
            for ($i = 1; $i <= $phone->getKeys(); $i++) {
                $button = new PhoneKey();
                $button->name = $i;
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'fpk';
                $button->key_idx = $i;
                $button->availableFunctions = $this->getFunctions();

                $ret[$button->name] = $button;
            }
        }

        if ($level == "" || $level == "shift") {
            for ($i = 1; $i <= $phone->getKeys(); $i++) {
                $button = new PhoneKey();
                $button->name = "100" . $i;
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'fpk';
                $button->key_idx = "100" . $i;
                $button->availableFunctions = $this->getFunctions();

                $ret[$button->name] = $button;
            }
        }

        if ($level == "" || $level == "ext1") {
            for ($i = 1; $i <= 12; $i++) {
                $button = new PhoneKey();
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'ext1';
                if ($i < 10) {
                    $button->key_idx = "30" . $i;
                    $button->name = "30" . $i;
                }
                if ($i > 9) {
                    $button->key_idx = "3" . $i;
                    $button->name = "3" . $i;
                }
                $button->availableFunctions = $this->getFunctions();

                $ret[$button->name] = $button;
            }
        }

        if ($level == "" || $level == "ext1-shift") {
            for ($i = 1; $i <= 12; $i++) {
                $button = new PhoneKey();
                $button->phone = $this->mPhoneModelName;
                $button->function = '';
                $button->level = 'ext1-shift';
                if ($i < 10) {
                    $button->name = "130" . $i;
                    $button->key_idx = "130" . $i;
                }
                if ($i > 9) {
                    $button->name = "13" . $i;
                    $button->key_idx = "13" . $i;
                }
                $button->availableFunctions = $this->getFunctions();

                $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->getKeys() != 0) {
            $lineKeyLevel = array();
            $lineKeyLevel["level"] = "fpk";
            $lineKeyLevel["name"] = "SIEMENS_KEYLEVEL_FPK_NAME";
            $lineKeyLevel["descr"] = "SIEMENS_KEYLEVEL_FPK_DESCR";
            $keyLevels[] = $lineKeyLevel;
        }

        if ($phone->getKeys() != 0) {
            $lineKeyLevel = array();
            $lineKeyLevel["level"] = "shift";
            $lineKeyLevel["name"] = "SIEMENS_KEYLEVEL_SHIFT_NAME";
            $lineKeyLevel["descr"] = "SIEMENS_KEYLEVEL_SHIFT_DESCR";
            $keyLevels[] = $lineKeyLevel;
        }

        if ($phone->getKeys() != 0) {
            $lineKeyLevel = array();
            $lineKeyLevel["level"] = "ext1";
            $lineKeyLevel["name"] = "SIEMENS_KEYLEVEL_EXT1_NAME";
            $lineKeyLevel["descr"] = "SIEMENS_KEYLEVEL_EXT1_DESCR";
            $keyLevels[] = $lineKeyLevel;
        }

        if ($phone->getKeys() != 0) {
            $lineKeyLevel = array();
            $lineKeyLevel["level"] = "ext1-shift";
            $lineKeyLevel["name"] = "SIEMENS_KEYLEVEL_SHIFT_EXT1_NAME";
            $lineKeyLevel["descr"] = "SIEMENS_KEYLEVEL_SHIFT_EXT1_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;
          }
         */

        return $keyLevels;
    }

    public function supportsLogoDeployment()
    {
        if ($this->mPhoneModelName == "os60" || $this->mPhoneModelName == "os80") {
            return true;
        }
        return false;
    }

    public function supportsScreenSaverDeployment()
    {
        if ($this->mPhoneModelName == "os60" || $this->mPhoneModelName == "os80") {
            return true;
        }
        return false;
    }

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

        $tmp = new Siemens(Siemens::getManufactorName(), "os15", "OpenStage 15");
        $tmp->setPhoneModelNameHumanReadable("Siemens OpenStage 15");
        $tmp->setKeys(8);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Siemens(Siemens::getManufactorName(), "os20", "OpenStage 20");
        $tmp->setPhoneModelNameHumanReadable("Siemens OpenStage 20");
        $tmp->setKeys(0);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Siemens(Siemens::getManufactorName(), "os40", "OpenStage 40");
        $tmp->setPhoneModelNameHumanReadable("Siemens OpenStage 40");
        $tmp->setKeys(6);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;


        $tmp = new Siemens(Siemens::getManufactorName(), "os60", "OpenStage 60");
        $tmp->setPhoneModelNameHumanReadable("Siemens OpenStage 60");
        $tmp->setKeys(8);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Siemens(Siemens::getManufactorName(), "os80", "OpenStage 80");
        $tmp->setPhoneModelNameHumanReadable("Siemens OpenStage 80");
        $tmp->setKeys(10);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        $tmp = new Siemens(Siemens::getManufactorName(), "dp55", "Desk Phone IP 55");
        $tmp->setPhoneModelNameHumanReadable("Unify Desk Phone IP 55");
        $tmp->setKeys(10);
        $tmp->setLineKeys(0);
        $models[$tmp->getPhoneModelName()] = $tmp;

        return $models;
    }

    public function getOs40UploadableFiles()
    {
        return array(
            'os40_logo' => array(
                'name' => 'os40_logo',
                'type' => 'image',
                'mimeType' => 'image/bmp',
                'maxFileSize' => 100000,
                'details' => array(
                    'width' => 144,
                    'height' => 32
                )
            ),
        );
    }

    public function getOs60UploadableFiles()
    {
        return array(
            'os60_screensaver' => array(
                'name' => 'os60_screensaver',
                'type' => 'image',
                'mimeType' => 'image/png',
                'maxFileSize' => 1000000,
                'details' => array(
                    'width' => 640,
                    'height' => 480
                )
            ),
            'os60_logo' => array(
                'name' => 'os60_logo',
                'type' => 'image',
                'mimeType' => 'image/png',
                'maxFileSize' => 1000000,
                'details' => array(
                    'width' => 240,
                    'height' => 70
                )
            ),
        );
    }

    public function getOs80UploadableFiles()
    {
        return array(
            'os40_screensaver' => array(
                'name' => 'os40_screensaver',
                'type' => 'image',
                'mimeType' => 'image/png',
                'maxFileSize' => 1000000,
                'details' => array(
                    'width' => 640,
                    'height' => 480
                )
            ),
            'os40_logo' => array(
                'name' => 'os40_logo',
                'type' => 'image',
                'mimeType' => 'image/png',
                'maxFileSize' => 1000000,
                'details' => array(
                    'width' => 480,
                    'height' => 148
                )
            ),
        );
    }

    public function getUploadableFiles()
    {
        if ($this->mPhoneModelName == 'os40') {
            return $this->getOs40UploadableFiles();
        } elseif ($this->mPhoneModelName == 'os60') {
            return $this->getOs60UploadableFiles();
        } elseif ($this->mPhoneModelName == 'os80') {
            return $this->getOs80UploadableFiles();
        }
        return array();
    }
}
