<?php

namespace Benhauer\Salesmanago\Model\Config;

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

class PurchasesConfig extends SalesmanagoAbstractModel
{
    /**
     * P_T - Processing Type;
     * C_L - Config Label;
     */
    const
        P_T_DEFAULT = 0,
        P_T_ON_CHOOSEN_EVENT = 1,
        P_T_ON_EVENT = 2,
        P_T_CRON = 3,

        C_L_TYPE = 'purchaseProcessingType',
        C_L_ON_CHOOSEN_EVENT = 'purchaseProcessingTypeSystemEvents',
        C_L_ON_CRON = 'purchaseProcessingTypeCron';

    public static $defaultPurchaseEvent = 'checkout_onepage_controller_success_action';
    public static $lastEventToSendPurchase = 'checkout_submit_all_after';

    /**
     * @var array processing types labels;
     */
    public static $processingTypes = [
        ['value' => self::P_T_DEFAULT, 'label' => 'Default'],
        ['value' => self::P_T_ON_CHOOSEN_EVENT, 'label' => 'System event'],
        ['value' => self::P_T_ON_EVENT, 'label' => 'After all purchase system events'],
        ['value' => self::P_T_CRON, 'label' => 'With CRON']
    ];

    /**
     * @var string
     */
    public static $configPath = SalesmanagoAbstractModel::C_PLATFORM_SETTING_PATH.'purchases/';

    /**
     * @var array list of events for purchase;
     */
    public static $processingSystemEvents = [
       0 => 'checkout_onepage_controller_success_action',
       1 => 'multishipping_checkout_controller_success_action',
       2 => 'checkout_submit_all_after',
       3 => 'checkout_controller_onepage_saveOrder',
       4 => 'restore_quote',
       5 => 'sales_convert_order_to_quote',
       6 => 'sales_order_invoice_register',
       7 => '_set_sales_order',
       8 => 'checkout_type_onepage_save_order_after',
       9 => 'checkout_type_multishipping_create_orders_single',
       10 => 'paypal_express_place_order_success'
    ];

    protected $config;
    protected $type = self::P_T_DEFAULT;

    /**
     * Default CRON cycle in hrs;
    */
    protected $defaultCroneCycle = 3;

    /**
     * @return bool|string
     */
    public function getConfig()
    {
        $type = $this->getConfigItem(self::$configPath.self::C_L_TYPE);

        switch ($type) {
            case self::P_T_ON_CHOOSEN_EVENT:
                $this->type = self::P_T_ON_CHOOSEN_EVENT;
                $this->config = $this->getConfigItem(self::$configPath.self::C_L_ON_CHOOSEN_EVENT);
                return $this->config;
                break;
            case self::P_T_ON_EVENT:
                $this->type = self::P_T_ON_EVENT;
                $this->config = self::$lastEventToSendPurchase;
                return $this->config;
                break;
            case self::P_T_DEFAULT:
                $this->type = self::P_T_DEFAULT;
                $this->config = self::$defaultPurchaseEvent;
                return $this->config;
                break;
            case self::P_T_CRON:
                $this->type = self::P_T_CRON;
                $this->config = $this->getConfigItem(self::$configPath.self::C_L_ON_CRON);
                return empty($this->config) ? $this->defaultCroneCycle : $this->config;
                break;
            default:
                return false;
                break;
        }
    }

    /**
     * @return int configuration type
     */
    public function getType()
    {
        $this->getConfig();
        return $this->type;
    }

    /**
     * @param string $hookName
     * @return boolean
     */
    public function isHookActive($hookName)
    {
        $config = $this->getConfig();

        if ($this->type == self::P_T_CRON) {
            return false;
        }

        if (isset(self::$processingSystemEvents[intval($this->config)])
            && (self::$processingSystemEvents[intval($this->config)] == $hookName)
        ) {
            return true;
        }

        return false;
    }

    /**
     * @return boolean
     */
    public function isCronActive()
    {
        $this->getConfig();

        if ($this->type == self::P_T_CRON) {
            return true;
        }

        return false;
    }
}