<?php

namespace Benhauer\Salesmanago\Model;

use \Magento\Framework\App\Config\ScopeConfigInterface;
use \Benhauer\Salesmanago\Exception\UserAccessException;

use \SALESmanago\Entity\Settings;
use \SALESmanago\Model\SettingsInterface;
use \SALESmanago\Model\UserInterface as SmUserInterface;
use \SALESmanago\Services\ConnectSalesManagoService;

use \Benhauer\Salesmanago\Model\AbstractModel as SalesmanagoAbstractModel;

class Hooks extends SalesmanagoAbstractModel implements SettingsInterface, SmUserInterface
{
    private $processigOrdersStatus = [
        'canceled' => ConnectSalesManagoService::EVENT_TYPE_CANCELLATION
    ];

    public function getUserSettings($userProperties)
    {
        $data = $this->getDataCoreConfig();

        if (isset($data[Settings::CLIENT_ID])
            || !empty($data[Settings::CLIENT_ID])
        ) {
            return $data;
        }
        return false;
    }

    /**
     * @return array
     * @throws UserAccessException
     */
    public function monitorVisitorsData()
    {
        $userData = $this->getUserSettings(null);

        return [
            Settings::CLIENT_ID => $userData[Settings::CLIENT_ID],
            Settings::ENDPOINT  => $userData[Settings::ENDPOINT],
            'webPush'           => $userData['webPush']
        ];
    }

    public function getSynchronizeRule()
    {
        return $this->getSynchronize();
    }

    /**
     * @return mixed - configuration of double optin (array) or false if double optin does not exis or not set up
    */
    public function getApiDoubleOptIn()
    {
        $apiDoubleOptInData = $this->getDoubleOptInData();

        return $apiDoubleOptInData
            ? array(
                AbstractModel::DOUBLE_OPT_IN => $apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN],
                AbstractModel::DOUBLE_OPT_IN_EMAIL_TEMPLATE_ID => isset($apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_TEMPLATE_ID])
                    ? $apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_TEMPLATE_ID]
                    : '',
                AbstractModel::DOUBLE_OPT_IN_EMAIL_ACCOUNT_ID => isset($apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_ACCOUNT_ID])
                    ? $apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_ACCOUNT_ID]
                    : '',
                AbstractModel::DOUBLE_OPT_IN_EMAIL_SUBJECT_ID => isset($apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_SUBJECT_ID])
                    ? $apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN_EMAIL_SUBJECT_ID]
                    : '',
            )
            : $apiDoubleOptInData[AbstractModel::DOUBLE_OPT_IN];
    }

    /**
     * Return array of order types/states which was chosen in Sm plugin
     * @return array
     */
    public function getSubmittedOrderTypes()
    {
        $data = $this->getDataCoreConfig();

        if (!isset($data[self::O_TYPES])) {
            return array();
        }

        return $data[self::O_TYPES];
    }

    /**
     * Get order statuses chosen by admin for sm plugin configuration & check if $param exist in there
     * @param string one of order status
     * @return boolean
     */
    public function checkIfOrderStatusChoosed($param)
    {
        $oTypes = $this->getSubmittedOrderTypes();

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

        return array_key_exists($param, array_flip($oTypes));
    }

    public function checkOrderStatusToProcess($orderStatus)
    {
        return (array_key_exists($orderStatus, $this->processigOrdersStatus))
            ? $this->processigOrdersStatus[$orderStatus]
            : false;
    }

    public function getUserToken($userProperties)
    {
        return true;
    }

    public function setCustomProperties($userProperties)
    {
        return true;
    }

    public function refreshUserToken($userProperties)
    {
        return true;
    }

    public function getDataForAccountType($userProperties)
    {
        return true;
    }
}
