<?php

namespace ModulesGarden\Servers\OpenXchange\App\UI\Admin\ProductConfiguration\Providers;

use \Illuminate\Database\Capsule\Manager as DB;
use ModulesGarden\Servers\OpenXchange\App\Enums\Size;
use ModulesGarden\Servers\OpenXchange\App\Enums\Zimbra;
use ModulesGarden\Servers\OpenXchange\Core\Traits\Lang;
use ModulesGarden\Servers\OpenXchange\App\Traits\LangHandler;
use ModulesGarden\Servers\OpenXchange\App\Enums\ProductParams;
use ModulesGarden\Servers\OpenXchange\App\Traits\ServerParams;
use ModulesGarden\Servers\OpenXchange\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\OpenXchange\App\Traits\ZimbraApiHandler;
use ModulesGarden\Servers\OpenXchange\App\Libs\Migrations\Drivers\Version1To2;
use ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Interfaces\AdminArea;
use ModulesGarden\Servers\OpenXchange\App\Models\ProductConfiguration as ProductConfigModel;
use ModulesGarden\Servers\OpenXchange\App\Libs\Zimbra\Components\Api\Soap\Repository;
use ModulesGarden\Servers\OpenXchange\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
use ModulesGarden\Servers\OpenXchange\App\Libs\Zimbra\Components\Api\Soap\Repository\ClassOfServices;
use ModulesGarden\Servers\OpenXchange\App\UI\Client\EmailAccount\Constants;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 29.08.19
 * Time: 10:27
 * Class ProductConfigurationDataProvider
 */
class ProductConfigurationDataProvider extends BaseDataProvider implements AdminArea
{
    const FORM_DATA = [
        //'email_accounts',
        'email_accounts_productivity',
        'email_accounts_security',
        'email_accounts_productivity_security',
        'forwarding_alias_limit',
        'alias_limit',
        //'module_access',
        'user_quota',
        'quota',
        'own_domains',
        'allow_to_change_password',
        'free_mailboxes',
        'selected_product',
        'product_accounts',
        'email_accounts_guard',
        'email_accounts_ai',
        'show_catch_all_button'
    ];

    protected function catchFormData()
    {
        /**
         * catch from request
         */
        $params = self::FORM_DATA;

        foreach ($params as $name)
        {
            if ($value = $this->request->get($name))
            {
                $this->formData[$name] = $value;
            } else {
                $this->formData[$name] = '';
            }
        }
    }

    public function read()
    {
        try{
            $settings = ProductConfigModel::where('product_id', $this->request->get('id'))->get();

            foreach(Constants::getTranslatedPackages() as $name => $access)
            {
                $this->availableValues['selected_product'][$access] = $name;
            }
    
            foreach ($settings as $setting)
            {
                $this->data[$setting->setting] = $setting->value;
            }
        } catch (\Exception $e)
        {
            logActivity($e->getMessage());
        }

       
    }

    public function update()
    {
        $this->catchFormData();

        $productId = $this->request->get('id');

        foreach ($this->formData as $key => $value)
        {
            if($key == 'product_accounts' && $value > 500)
            {
                $value = 500;
            }

            if($key == 'alias_limit' && $value > 50)
            {
                $value = 50;
            }

            if($key == 'quota' && $value < 10000)
            {
                $value = 10000;
            }

            if ($key == 'user_quota' && $value > $this->formData['quota'] && $this->formData['quota'] >= 10000)
            {
                $value = $this->formData['quota'];
            }

            ProductConfigModel::updateOrCreate(['product_id' => $productId, 'setting' => $key], ['value' => $value]);
        }
    }
}