<?php

namespace ModulesGarden\Servers\ZimbraEmail\App\Http\Actions;

use ModulesGarden\Servers\ZimbraEmail\App\Enums\Response;
use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Product\ProductManager;
use ModulesGarden\Servers\ZimbraEmail\App\Traits\ExtensionsCheckerTrait;
use ModulesGarden\Servers\ZimbraEmail\Core\App\Controllers\Instances\AddonController;
use ModulesGarden\Servers\ZimbraEmail\App\Models\ProductConfiguration;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 09.09.19
 * Time: 15:15
 * Class ChangePackage
 */
class ChangePackage extends AddonController
{
    use ExtensionsCheckerTrait;

    /**
     * @param null $params
     * @return string|void
     */
    public function execute($params = null)
    {
        try{

            /**
             * check if extensions are installed
             */
            $this->checkExtensionOrThrowError();

            /**
             * run zimbra service
             */
            $result =  $this->zimbraRunService($params);
            return $result;
        }catch (\Exception $ex)
        {
            /**
             * return some crit error
             */
            return $ex->getMessage();
        }

    }

    /**
     * @param null $params
     * @return string
     */
    protected function zimbraRunService($params = null)
    {

        $productManager = new ProductManager();
        $productManager->loadById($params['pid']);

        $service = (new ZimbraManager())
            ->getApiByServer($params['serverid'])
            ->soap
            ->service()
            ->changePackages($productManager->get('cos_name'))
            ->setProductManager($productManager)
            ->setFormData($params)
        ;

        $result = $service->run();

//        $configuration = ProductConfiguration::where('product_id', $params['pid'])->get();
//        foreach($configuration as $cModel)
//        {
//            $config[$cModel->setting] = $cModel->value;
//        }
//
//        $new_account_size = $config['acc_size'];
//
//        //todo mailboxes from API
//
//        $repository = (new ZimbraManager())
//            ->getApiByServer($params['serverid'])
//            ->soap
//            ->repository();
//
//        $mailboxesApi = $repository->accounts->getByDomainName($params['domain']);
//
//        if ($config['coss_name'] == "customMGzimbra")
//        {
//            $attr = $config;
//            foreach ($attr as $key => &$item)
//            {
//                if (preg_match('/zimbra.+/', $key))
//                {
//                    $item = $item == "on" ? "TRUE" : "FALSE";
//                }
//                else
//                {
//                    unset($attr[$key]);
//                }
//            }
//
//            $attr['zimbraMailQuota'] = $new_account_size * 1048576;
//        }elseif ($config['coss_name'] == 'cosQuota')
//        {
//            //todo
//        }else{
//
//        }
//
//        foreach($mailboxesApi as $key => $value)
//        {
//            //todo
//        }

        return Response::SUCCESS;
    }
}