<?php

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

use ModulesGarden\Servers\OpenXchange\App\Libs\RestApi;
use ModulesGarden\Servers\OpenXchange\App\Models\ProductConfiguration;
use ModulesGarden\Servers\OpenXchange\Core\App\Controllers\Instances\AddonController;
use ModulesGarden\Servers\OpenXchange\App\Helpers\Helper;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 09.09.19
 * Time: 15:14
 * Class CreateAccount
 */
class ChangePackage extends AddonController
{
    /**
     * create domain in zimbra
     *
     * @param null $params
     * @return string
     */
    public function execute($params = null)
    {
        //logModuleCall('openXchange', "change package params", 'test params', $params);

        try
        {
            $config         = ProductConfiguration::config($params['packageid']);
            $context        = new RestApi($params);
            $users          = $context->listAllUsers();
            $classOfService = Helper::getCoSForUpdate($params, $config);
            $currentCos     = Helper::getCurrentCOS($params);

            foreach ($users as $user)
            {
                if (!empty($classOfService) && count($classOfService) == 1 && !empty($currentCos) && count($currentCos) == 1)
                {
                    //situation 1: change the clients mailboxes
                    $context->changeUser($user->name, $classOfService[0]);
                }

                if (isset($params['configoptions']['allow_to_change_password']))
                {
                    $context->changePasswordAccess($user->name, $params['configoptions']['allow_to_change_password'] == 1);
                }
                elseif($config['allow_to_change_password'] == 'on')
                {
                    $context->changePasswordAccess($user->name, true);
                }
                else
                {
                    $context->changePasswordAccess($user->name, false);
                }
            }

            if ((!empty($classOfService) && count($classOfService) > 1) || (!empty($currentCos) && count($currentCos) > 1))
            {
                //situation 2: There is more than one COS allowed. Just sernd an emial to client
                $command  = 'SendEmail';
                $postData = [
                    'messagename' => 'OpenXchange Upgrade Product',
                    'id'          => $params['serviceid'],
                    'customvars'  => base64_encode(serialize(array("packageID"=>$params['packageid'], "custommerge2"=>$populatedvar2))),
                ];

                $results = localAPI($command, $postData);

                if ($results['result'] != 'success')
                {
                    logModuleCall('openXchange', "change package send email", $postData, $results);
                }
            }

            $context->changeQuota();
        }
        catch (\Exception $ex)
        {
            /**
             * return some crit error
             */
            return $ex->getMessage();
        }

        return 'success';
    }
}
