<?php

namespace Benhauer\Salesmanago\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;

class Config extends AbstractHelper
{
    const
        ASSETS_FILE_JS_MAIN = 'magento.js',
        ASSETS_FILE_JS_VENDOR = 'chunk-vendors.86091822.js',
        ASSETS_FILE_CSS_MAIN = 'main.9e692682.css',
        ASSETS_FILE_CSS_OVERWRITE = 'app-magento.css',
        /* app-global-css is declarated in view/adminhtml/layout/default */

        BASE_URL = 'baseURL',
        PLATFORM = 'platform',
        CLIENT_ID = 'clientID',
        SHOP_ORIGIN = 'shopOrigin',

        LANG = 'lang',
        VERSION = 'version',
        PARAMS = 'params',
        IS_AJAX = 'isAjax',
        AUTH_TOKEN ='auth_token',

        M_TYPE = 'mType',
        M_ID = 'mId',
        M_CODE = 'mCode',

        ENABLE = 'enable', /*EN_*/

        EN_SYNC_RULE = 'synchronizeRule',
        EN_API_DOUBLE_OPT_IN = 'apiDoubleOptIn',
        EN_CALLBACKS = 'callbacks',
        EN_ORDER_TYPES = 'orderTypes',

        EN_PLATFORM_REDIRECT = 'platformRedirect',/*EN_PL_RED_*/
        EN_PL_RED_ACTIVE = 'active',
        EN_PL_RED_NAME = 'name',
        EN_PL_RED_URL = 'url';

    /**
     * Url to controllers for admin views
    */
    private $_baseUrl = '/';

    /**
     * Platform name (with version)
    */
    private static $_platform = 'Magento 2';

    /**
     * Platform version
     */
    private $_platformVersion = '1.0.0';

    /**
     * Sm clientId
    */
    private $_clientId = '';

    /**
     * Shop Url
    */
    private $_shopOrigin = '';

    /**
     * PL or EN
    */
    private $_lang = 'EN';

    /**
     * plugin version
    */
    private static $_version = '3.1.0';

    /**
     * Flag for parameters enabled option
     * @type boolean
    */
    private $_params = true;

    /**
     * Magento view request check flag
     * @type boolean
    */
    private $_paramIsAjax = true;

    private $_paramAuthToken = '';

    /**
     * One of Magento scope types: Website, Store, StoreView
     * @type string
    */
    private $_paramScopeType = null;

    /**
     * Id of Magento scope type
     * @type integer
    */
    private $_paramScopeId = null;

    /**
     * Magento scope code
     * @type string
    */
    private $_paramScopeCode = null;

    /**
     * Custom params which could be add through setCustomParameters
    */
    private $_customParams = array();

    /**
     * Enabled functions:
    */
    public static $enSynchronizeRule = true;
    public static $enApiDoubleOptIn = true;
    public static $enCallbacks = true;
    public static $enOrderTypes = false;

    /**
     * This will add new menu item for sm views, which will redirect user back to admin
     * For magento is true;
     * @type boolean
    */
    public static $enPlatformRedirectActive = true;

    private $_enPlatformRedirectName;
    private $_enPlatformRedirectUrl;

    /**
     * Enabled functions for admin/plugin/platform
    */
    private $_enable = true;

    /**
     * @param string $baseUrl - base url for views
     * @param string $platform - platform name (Magento)
     * @param string $clientId - SM client id
     * @param string $shopOrigin - base shop url
     * @param string $lang - PL - for poland || EN - for others
     * @param string $version - plugin version
     * @param string $viewAuthToken - token
     * @param string $magentoScopeType - Website, Store, StoreView
     * @param mixed $magentoScopeId - Magento scope id
     * @param string $magentoScopeCode - Magento scope short code
     * @return $this
     */
    public function set(
        $baseUrl = '',
        $platform = '',
        $clientId = '',
        $shopOrigin = '',
        $lang = '',
        $version = '',
        $viewAuthToken = '',
        $magentoScopeType = '',
        $magentoScopeId = '',
        $magentoScopeCode = ''
    ) {
        $this->_baseUrl = (!empty($baseUrl)) ? $baseUrl : $this->_baseUrl;
        self::$_platform = (!empty($platform)) ? $platform : self::$_platform;
        $this->_clientId = (!empty($clientId)) ? $clientId : $this->_clientId;
        $this->_shopOrigin = !empty($shopOrigin) ? $shopOrigin : $this->_shopOrigin;
        $this->_lang = (!empty($lang)) ? $lang : $this->_lang;
        self::$_version = (!empty($version)) ? $version : self::$_version;

        $this->_paramAuthToken = (!empty($viewAuthToken)) ? $viewAuthToken : $this->_paramAuthToken;

        $this->_paramScopeType = (!empty($magentoScopeType)) ? $magentoScopeType : $this->_paramScopeType;
        $this->_paramScopeId = (!empty($magentoScopeId)) ? $magentoScopeId : $this->_paramScopeId;
        $this->_paramScopeCode = (!empty($magentoScopeCode)) ? $magentoScopeCode : $this->_paramScopeCode;

        return $this;
    }

    /**
     * @return string self::$_version plugin version
     */
    public static function getPluginVersion()
    {
        return self::$_version;
    }

    /**
     * @return string self::$_platform plugin version
     */
    public static function getPluginPlatform()
    {
        return  self::$_platform;
    }

    /**
     * @return array - admin views configuration
    */
    public function getBhrSso()
    {
         $bhrSso = array(
            self::BASE_URL => $this->_baseUrl,
            self::PLATFORM => self::$_platform,
            self::CLIENT_ID => $this->_clientId,
            self::SHOP_ORIGIN => $this->_shopOrigin,
            self::LANG => ($this->_lang != 'PL' && $this->_lang != 'EN') ? 'EN' : $this->_lang,
            self::VERSION => self::$_version,
        );

        $bhrSso = array_merge($bhrSso, $this->_getParameters());
        $bhrSso = array_merge($bhrSso, $this->_getEnabledFunctions());

        return $bhrSso;
    }

    public function getConfigForViews()
    {
        return json_encode($this->getBhrSso());
    }

    /**
     * Set auth token
     * @param $param - authToken;
     * @return $this
     * */
    public function setAuthToken($param)
    {
        $this->_paramAuthToken = $param;
        return $this;
    }

    /**
     * Sets custom parameters to parameters
     * @param array $parameters associative array;
     * @return $this
     */
    public function setCustomParameters($parameters = array())
    {
        $this->_customParams = array_merge($this->_customParams, $parameters);
        return $this;
    }

    /**
     * @param string $name - menu item name
     * @param string $url - platform admin url
     * @return $this
    */
    public function setPlatformRedirect($name = 'to platform', $url = '/') {
        $this->_enPlatformRedirectName = $name;
        $this->_enPlatformRedirectUrl = $url;
        return $this;
    }

    /**
     * Build array of default params which will be send in each request from view to platform
     * @return array of params
    */
    private function _getDefaultParams()
    {
        $params = ($this->_params)
            ? array(
                self::PARAMS =>
                    array(
                        self::IS_AJAX => $this->_paramIsAjax,
                        self::AUTH_TOKEN => $this->_paramAuthToken,

                        self::M_TYPE => $this->_paramScopeType,
                        self::M_ID => $this->_paramScopeId,
                        self::M_CODE => $this->_paramScopeCode
                    )
            )
            : array();

        return $params;
    }

    /**
     * Build array of all parameters which will be send in each request from view to platform
     * @return array of params
     */
    private function _getParameters()
    {
        $params = $this->_getDefaultParams();

        if (!isset($params[self::PARAMS])) {
            return array(self::PARAMS => $this->_customParams);
        }

        if (empty($this->_customParams)) {
            return $params;
        }

        $params[self::PARAMS] = array_merge($params[self::PARAMS], $this->_customParams);
        return $params;

    }

    /**
     * Build array of default enabled functions for platform
     * @return array of params
     */
    private function _getEnabledFunctions()
    {
        $enabled = ($this->_enable)
            ? array(self::ENABLE => array(
                self::EN_SYNC_RULE => self::$enSynchronizeRule,
                self::EN_API_DOUBLE_OPT_IN => self::$enApiDoubleOptIn,
                self::EN_CALLBACKS => self::$enCallbacks,
                self::EN_ORDER_TYPES => self::$enOrderTypes,

                self::EN_PLATFORM_REDIRECT => array(
                    self::EN_PL_RED_ACTIVE => self::$enPlatformRedirectActive,
                    self::EN_PL_RED_NAME => $this->_enPlatformRedirectName,
                    self::EN_PL_RED_URL => $this->_enPlatformRedirectUrl
                )
            ))
            : array();
        return $enabled;
    }
}
