<?php
namespace ThurData\Servers\KerioEmail\App\Libs\Migrations\Drivers\Version1To2;
use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;

/**
 * Class Settings
 * User: ThurData
 * Date: 2019-11-12
 * Time: 19:47
 */

class Settings
{

    /**
     * @var string
     */
    protected $fromTable = 'mg_prodConfig';
    /**
     * @var string
     */
    protected $toTable   = '';

    /**
     * @return string
     */
    public function getFromTable()
    {
        return $this->fromTable;
    }

    /**
     * @param $key
     * @param $value
     * @return array
     */
    public function unsupported($key, $value)
    {
        if(strpos($key, 'cos_') === 0)
        {
            $name = str_replace('cos_', 'cosQuota_', $key);
            return [$name, $value];
        }

    }

    /**
     * @return array
     */
    public function getMapped()
    {
        return [
            'acc_limit' => 'acc_limit',
            'acc_size' => 'acc_size',
            'alias_limit' => 'alias_limit',
            'cos_name' => 'cos_name',
            'dist_list_limit' => 'dist_list_limit',
            'domainMaxSize' => 'domainMaxSize',
            'domain_alias_limit' => 'domain_alias_limit',
            'filterAccountsByCOS' => 'filterAccountsByCOS',
            'login_link' => 'login_link',
            'useCos', //todo ?
            'kerioDumpsterEnabled' => 'kerioDumpsterEnabled',
            'kerioDumpsterPurgeEnabled' => 'kerioDumpsterPurgeEnabled',
            'kerioFeatureAdvancedSearchEnabled' => 'kerioFeatureAdvancedSearchEnabled',
            'kerioFeatureBriefcasesEnabled' => 'kerioFeatureBriefcasesEnabled',
            'kerioFeatureCalendarEnabled' => 'kerioFeatureCalendarEnabled',
            'kerioFeatureCalendarReminderDeviceEmailEnabled' => 'kerioFeatureCalendarReminderDeviceEmailEnabled',
            'kerioFeatureChangePasswordEnabled',
            'kerioFeatureContactsEnabled',
            'kerioFeatureConversationsEnabled',
            'kerioFeatureDistributionListFolderEnabled',
            'kerioFeatureEwsEnabled',
            'kerioFeatureExportFolderEnabled',
            'kerioFeatureFiltersEnabled',
            'kerioFeatureFlaggingEnabled',
            'kerioFeatureGalAutoCompleteEnabled',
            'kerioFeatureGalEnabled',
            'kerioFeatureGroupCalendarEnabled',
            'kerioFeatureHtmlComposeEnabled',
            'kerioFeatureIdentitiesEnabled',
            'kerioFeatureImapDataSourceEnabled',
            'kerioFeatureImportFolderEnabled',
            'kerioFeatureInitialSearchPreferenceEnabled',
            'kerioFeatureMailEnabled',
            'kerioFeatureMailPriorityEnabled',
            'kerioFeatureMailSendLaterEnabled',
            'kerioFeatureManageZimlets',
            'kerioFeatureMAPIConnectorEnabled',
            'kerioFeatureNewMailNotificationEnabled',
            'kerioFeatureOptionsEnabled',
            'kerioFeatureOutOfOfficeReplyEnabled',
            'kerioFeaturePeopleSearchEnabled',
            'kerioFeaturePop3DataSourceEnabled',
            'kerioFeatureReadReceiptsEnabled',
            'kerioFeatureSavedSearchesEnabled',
            'kerioFeatureSharingEnabled',
            'kerioFeatureSkinChangeEnabled',
            'kerioFeatureSMIMEEnabled',
            'kerioFeatureTaggingEnabled',
            'kerioFeatureTasksEnabled',
            'kerioFeatureTouchClientEnabled',
            'kerioFeatureWebClientOfflineAccessEnabled',
            'kerioImapEnabled',
            'kerioPop3Enabled',
        ];
    }

    /**
     * @param $params
     * @return mixed
     */
    public function updateValues($params, $prodId)
    {
        foreach($params as $key => $value)
        {
            if(strpos($key, 'cos_') === 0 && $key !== 'cos_name')
            {
                $name = str_replace('cos_', '', $key);
                $cos[$name] = $value;
                unset($params[$key]);
            }
        }

        if($cos)
        {
            $params['cos'] = json_encode($cos);
        }

        /**
         * update cosName
         *
         */

        $defaultTypes = [
            ClassOfServices::CUSTOM_KERIO,
            ClassOfServices::KERIO_CONFIG_OPTIONS,
            ClassOfServices::CLASS_OF_SERVICE_QUOTA,
        ];

        /**
         *
         *
         */
        if(!in_array($params['cos_name'], $defaultTypes))
        {
            $manager    = new KerioManager();
            $repository = $manager->getApiByProduct($prodId)->soap->repository();
            $cosList        = $repository->cos->all();

            /**
             * class of services from API
             */
            foreach ($cosList as $cos)
            {
                /**
                 * @var $cos \ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService
                 */
                $cosList[$cos->getName()] = $cos->getId();
            }

            $params['cos_name'] = $cosList[$params['cos_name']];
        }

        return $params;
    }

    /**
     *
     * return new fields as name => default value
     *
     * @return array
     */
    public function getNewFields()
    {
        return [
            'clientAreaFeaturesLeft'    => 'on',
            'ca_emailAccountPage'       => 'on',
            'ca_ressourcePage'          => 'on',
            'ca_distributionListPage'   => 'on',
            'ca_goToWebmailPage'        => 'on',
            'clientAreaFeaturesRight'   => 'on',
            'ca_emailAliasesPage'       => 'on',
            'ca_domainAliasesPage'      => 'on',
            'ca_settingPage'           => 'on',
        ];

    }

}
