<?php

require_once __DIR__ . '/../modules/provisioningGeneral/lib/AbstractPhoneModel.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/tiptel-2xx/Tiptel.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/tiptel-30xx/Tiptel30xx.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/tiptel-32xx/Tiptel32xx.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/tiptel-33xx/Tiptel33xx.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/yealink-t4x/Yealink_t4x.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/siemens/siemens.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/snom/snom.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/ergophone/ergophone.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/grandstream/Grandstream.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/htek/Htek.php';
require_once __DIR__ . '/../modules/provisioningGeneral/phones/gigaset/Gigaset.php';

class PhoneModelFactory
{

    public static function getPhoneManufactors()
    {
        $manufactors = array();
        $manufactors[] = new Siemens();
        $manufactors[] = new Snom();
        $manufactors[] = new Tiptel("unknown");
        $manufactors[] = new Tiptel30xx("unknown");
        $manufactors[] = new Tiptel32xx();
        $manufactors[] = new Tiptel33xx();
        $manufactors[] = new Yealink_t4x();
        $manufactors[] = new Ergophone();
        $manufactors[] = new Grandstream();
        $manufactors[] = new Htek();
        $manufactors[] = new Gigaset();

        return $manufactors;
    }

    public static function getPhoneManufactorAndModelNames()
    {
        $manufactors = PhoneModelFactory::getPhoneManufactors();
        $phoneNames = array();
        foreach ($manufactors as $manufactor) {
            $allPhones = $manufactor->getSupportedPhoneModels();
            foreach ($allPhones as $phone) {
                $manufactorName = $manufactor->getManufactorName();
                if (startsWith(strtolower($manufactorName), 'tiptel')) {
                    $manufactorName = 'Tiptel';
                }
                if (startsWith(strtolower($phone->getPhoneModelName()), 'yealink')) {
                    $manufactorName = 'Yealink';
                }
                $phoneNames[$manufactorName][$phone->getPhoneModelName()] = $phone->getPhoneModelNameHumanReadable();
                asort($phoneNames[$manufactorName]);
            }
        }
        ksort($phoneNames);

        return $phoneNames;
    }

    public static function getManufactorOfType($type)
    {
        $manufactors = PhoneModelFactory::getPhoneManufactors();
        foreach ($manufactors as $manufactor) {
            $allPhones = $manufactor->getSupportedPhoneModels();
            foreach ($allPhones as $phone) {
                if ($phone->getPhoneModelName() == $type) {
                    return $manufactor;
                }
            }
        }
    }

    public static function getPhoneModelOfType($type)
    {
        $manufactors = PhoneModelFactory::getPhoneManufactors();
        foreach ($manufactors as $manufactor) {
            $allPhones = $manufactor->getSupportedPhoneModels();
            foreach ($allPhones as $phone) {
                if ($phone->getPhoneModelName() == $type) {
                    return $phone;
                }
            }
        }
        return NULL;
    }
}
