<?php

require_once dirname(__FILE__) . '/../tiptel-30xx/Tiptel30xx.php';

class Htek extends Tiptel30xx
{

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

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

    public static function getSupportedPhoneModels()
    {
        $models = array();
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC803', 2, 5);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC806', 4, 10);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC862', 4, 10, 3, 40);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC902', 2);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC903', 20);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC912E', 12);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC912G', 12);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC912P', 12);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC923', 20);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC924', 28, 0, 3, 40);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC924E', 28, 0, 3, 40);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC926', 36, 0, 3, 40);
        $models = Htek::writePhoneDataToTiptel32xxFactory($models, 'UC926E', 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 Htek(Htek::getManufactorName(), "htek-" . $lowerName, "htek" . $lowerName);
        $tmp->setPhoneModelNameHumanReadable("Htek " . $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 == '803' && $phoneName == 'htek-uc803') ||
                ($firmwareKey == '806' && $phoneName == 'htek-uc806') ||
                ($firmwareKey == '862' && $phoneName == 'htek-uc862') ||
                ($firmwareKey == '902' && $phoneName == 'htek-uc902') ||
                ($firmwareKey == '903' && $phoneName == 'htek-uc903') ||
                ($firmwareKey == '923' && $phoneName == 'htek-uc923') ||
                ($firmwareKey == '924' && $phoneName == 'htek-uc924') ||
                ($firmwareKey == '926' && $phoneName == 'htek-uc926') ||
                ($firmwareKey == '912' && $phoneName == 'htek-uc912p') ||
                (strtoupper($firmwareKey) == '912E' && $phoneName == 'htek-uc912e') ||
                (strtoupper($firmwareKey) == '912G' && $phoneName == 'htek-uc912g') ||
                (strtoupper($firmwareKey) == '912P' && $phoneName == 'htek-uc912p') ||
                (strtoupper($firmwareKey) == '924E' && $phoneName == 'htek-uc924e') ||
                (strtoupper($firmwareKey) == '926E' && $phoneName == 'htek-uc926e')
        ) {
            $result = $this->getFirmwareVersionFromBinFile($fileTmpPath);
        }
        return $result;
    }
}
