<?php

require_once __DIR__ . '/../modules/routingGateways/models/Gateway.php';
require_once __DIR__ . '/../modules/routingInbound/models/RoutingInbound.php';
require_once __DIR__ . '/../modules/routingOutbound/models/RoutingOutbound.php';
require_once __DIR__ . '/../modules/networkDNS/models/DNSRecord.php';
require_once __DIR__ . '/../modules/networkDNS/models/DNSServer.php';
require_once __DIR__ . '/../modules/networkDNS/models/DNSZone.php';

class ModelToJsonConverter
{

    private static $attributeBlacklist = array(
        'id',
        'enableDNSServer',
        'secret'
    );
    private static $modelList = array(
        'Gateway',
        'RoutingInbound',
        'RoutingOutbound',
        'NetworkInterface',
        'DNSRecord',
        'DNSServer',
        'DNSZone',
        'CDR',
        'Sysconfig'
    );
    private static $modelName = '';

    public static function getJsonEncodedData()
    {
        $dataArray = array();
        foreach (self::$modelList as $modelName) {
            self::$modelName = $modelName;
            if (self::$modelName === 'RoutingInbound' || self::$modelName === 'RoutingOutbound') {
                $dataArray = self::getFormatedDataArray($dataArray, array(), 'sort');
            } else if (self::$modelName === 'CDR') {
                $dataArray = self::getFormatedDataArray($dataArray, array(), 'start_timestamp DESC', 10);
            } else if ($modelName === 'DNSServer' || $modelName === 'DNSRecord' || $modelName === 'DNSZone') {
                $dataArray = self::getFormatedDataArray($dataArray, array(), 'position');
            } else {
                $dataArray = self::getFormatedDataArray($dataArray);
            }
        }
        $dataArray['DHCP'] = NetworkSettings::getConvertedSettings();
        $dataArray['License'] = self::getLicenseInfoForSupport();
        $dataArray['SystemUpdate'] = SystemUpdate::getInstalledVersionData();
        $dataArray['SystemOverview'] = self::getSystemOverviewData();
        return json_encode($dataArray);
    }

    private static function getFormatedDataArray($dataArray, $searchAttributes = array(), $sort = 'id', $limit = 1000, $offset = 0)
    {
        $modelName = self::$modelName;
        $models = $modelName::model()->find($searchAttributes, $sort, $limit, $offset);
        foreach ($models as $modelKey => $model) {
            foreach ($model->attributes as $key => $value) {
                if (in_array($key, self::$attributeBlacklist)) {
                    continue;
                }
                $dataArray = self::getformatedAttributes($model, $dataArray, $modelKey, $key, $value);
            }
        }
        return $dataArray;
    }

    private static function getformatedAttributes($model, $dataArray, $modelKey, $key, $value)
    {
        $modelName = self::$modelName;
        if ($modelName === 'Gateway') {
            $dataArray[$modelName][$model->id][$key] = $value;
        } else if ($modelName === 'RoutingInbound' || $modelName === 'RoutingOutbound') {
            $dataArray['Gateway'][$model->gw_id][$modelName][$model->sort][$key] = $value;
        } else if ($modelName === 'DNSServer') {
            $dataArray['networkDNS'][$modelName]['enableDNSServer'] = $model->enableDNSServer;
            $dataArray['networkDNS'][$modelName][$model->position][$key] = $value;
        } else if ($modelName === 'DNSZone') {
            $dataArray['networkDNS'][$modelName][$model->id][$key] = $value;
        } else if ($modelName === 'DNSRecord') {
            $dataArray['networkDNS']['DNSZone'][$model->dnsZone][$modelName][$model->position][$key] = $value;
        } else {
            $dataArray[$modelName][$modelKey][$key] = $value;
        }
        return $dataArray;
    }

    private static function getLicenseInfoForSupport()
    {
        $licenseInfo = PhonalisaLicenseManager::getLicenseInfo();
        $sortedInfo = Licence::sortLicenseInfo($licenseInfo);
        unset($sortedInfo['valid']);
        return $sortedInfo;
    }

    private static function getSystemOverviewData()
    {
        $model = new SystemOverview();
        $data = $model->getCurrentSystemOverview();
        if ($model->hasCPUThermalInfo()) {
            $data['Temp']["cpu"] = $model->getCPUTemperature();
        }
        if ($model->hasSystemLoadInfo(SystemLoadInfo::ONE_MINUTE)) {
            $data['Load']["loadOneMin"] = $model->getSystemLoad(SystemLoadInfo::ONE_MINUTE);
        }
        if ($model->hasSystemLoadInfo(SystemLoadInfo::FIVE_MINUTES)) {
            $data['Load']["loadFiveMin"] = $model->getSystemLoad(SystemLoadInfo::FIVE_MINUTES);
        }
        if ($model->hasSystemLoadInfo(SystemLoadInfo::TEN_MINUTES)) {
            $data['Load']["loadTenMin"] = $model->getSystemLoad(SystemLoadInfo::TEN_MINUTES);
        }
        if ($model->hasRamInfo()) {
            $data["RAM"] = $model->getFormattedMemValue();
        }
        if ($model->hasHddInfo(MemoryInfo::MOUNT_POINT_ROOT)) {
            $data['HDD']["root"] = $model->getFormattedMemValue(MemoryInfo::MOUNT_POINT_ROOT);
        }
        if ($model->hasHddInfo(MemoryInfo::MOUNT_POINT_DB)) {
            $data['HDD']["db"] = $model->getFormattedMemValue(MemoryInfo::MOUNT_POINT_DB);
        }
        if ($model->hasHddInfo(MemoryInfo::MOUNT_POINT_SD_CARD)) {
            $data['HDD']["sdcard"] = $model->getFormattedMemValue(MemoryInfo::MOUNT_POINT_SD_CARD);
        }
        return $data;
    }
}
