<?php

require_once dirname(__FILE__) . '/../htek/Htek.php';

class Tiptel33xx extends Htek
{

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

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

    public static function getSupportedPhoneModels()
    {
        $models = array();
        $models = Tiptel33xx::writePhoneDataToTiptel32xxFactory($models, '3310', 2);
        $models = Tiptel33xx::writePhoneDataToTiptel32xxFactory($models, '3320', 28, 0, 3, 40);
        $models = Tiptel33xx::writePhoneDataToTiptel32xxFactory($models, '3330', 28, 0, 3, 40);
        $models = Tiptel33xx::writePhoneDataToTiptel32xxFactory($models, '3340', 36, 0, 3, 40);
        return $models;
    }

    protected static function writePhoneDataToTiptel32xxFactory($models, $name, $lineKeys = 0, $memoryKeys = 0, $extModules = 0, $extModulesKeys = 0)
    {
        $lowerName = strtolower($name);
        $upperName = strtoupper($name);
        $tmp = new Tiptel33xx(Tiptel33xx::getManufactorName(), "tiptel-" . $lowerName, "tiptel" . $lowerName);
        $tmp->setPhoneModelNameHumanReadable("tiptel " . $upperName);
        $tmp->setLineKeys($lineKeys);
        $tmp->setKeys($memoryKeys);
        $tmp->setExtModules($extModules);
        $tmp->setExtKeys($extModulesKeys);
        $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 == '3310' && $phoneName == 'tiptel-3310') ||
                ($firmwareKey == '3320' && $phoneName == 'tiptel-3320') ||
                ($firmwareKey == '3330' && $phoneName == 'tiptel-3330') ||
                ($firmwareKey == '3340' && $phoneName == 'tiptel-3340')
        ) {
            $result = $this->getFirmwareVersionFromBinFile($fileTmpPath);
        }
        return $result;
    }

    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);
        $v4 = str_pad((int) @$tmp[4], 2, '0', STR_PAD_LEFT);
        return "$v0.$v1.$v2.$v3.$v4";
    }
}
