<?php

namespace lumanPlus\App\Controllers;

use lumanPlus\Core\Helper;
use lumanPlus\Core\Logger;
use SimplePie\Exception;

class SettingController
{

    public function saveServiceSetting (array $params){

        if ( empty($params) ) {
            return false;
        }

        $keys = [
            'FarazSms'      => 'faraz_sms_config',
            'MelliPayamak'  => 'melli_payamak_config',
            'KavehNegar'    => 'kaveh_negar_config'
        ];

        if ( empty($params['default_service']) || !isset($keys[$params['default_service']]) ) {
            return false;
        }

        $configKey = $keys[$params['default_service']];

        try {
            Helper::setOption('default_service',$params['default_service']);
            unset($params['default_service']);
            Helper::setOption($configKey,$params);
        } catch (Exception $exception) {
            Logger::error('Error on saving service setting ',[
                'message'   => $exception->getMessage()
            ]);
            return false;
        }

        return true;

    }

    public function saveGeneralSetting (array $params)
    {
        if ( empty($params) ) {
            return false;
        }

        try {
            foreach ($params as $key => $value) {
                Helper::setOption($key,$value);
            }
        } catch (Exception $exception) {
            Logger::error('Error on saving service setting ',[
                'message'   => $exception->getMessage()
            ]);
            return false;
        }

        return true;

    }

    public function saveCustomSetting (array $params)
    {
        if ( empty($params) ) {
            return false;
        }

        try {
            foreach ($params as $key => $value) {
                Helper::setOption($key,$value);
            }
        } catch (Exception $exception) {
            Logger::error('Error on saving service setting ',[
                'message'   => $exception->getMessage()
            ]);
            return false;
        }

        return true;
    }

    public function saveRecaptchaSetting (array $params): bool
    {
        if ( empty($params) ) {
            return false;
        }

        if ( empty($params['default_recaptcha_version']) ) {
            return false;
        }

        $siteKey    = "recaptcha_" . $params['default_recaptcha_version'] . "_site_key";
        $secretKey  = "recaptcha_" . $params['default_recaptcha_version'] . "_secret_key";

        try {
            Helper::setOption('default_recaptcha_version',$params['default_recaptcha_version']);
            Helper::setOption('recaptcha_status',$params['recaptcha_status']);
            Helper::setOption($siteKey,$params[$siteKey]);
            Helper::setOption($secretKey,$params[$secretKey]);
        } catch (Exception $exception) {
            Logger::error('Error on saving service setting ',[
                'message'   => $exception->getMessage()
            ]);
            return false;
        }

        return true;
    }
}