<?php
/**
 * @category    Leadlion
 * @package     Leadlion_AutoRelated
 * @author      Leadlion Team <info@Leadlion.com>
 * @copyright   Copyright (c) 2019 Leadlion (https://www.Leadlion.com)
 * @license     https://opensource.org/licenses/gpl-3.0.html  GNU General Public License (GPL 3.0)
 */

namespace Leadlion\AutoRelated\Helper;

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

class Data extends AbstractHelper
{
    const SETTINGS_ENABLED = 'autorelated/setup/enabled';
    const SETTINGS_LIMIT = 'autorelated/setup/limit';
    const SETTINGS_CACHE_LIFETIME = 'autorelated/setup/cache_lifetime';
    const SETTINGS_DISPLAY_TYPE_RELATED = 'autorelated/setup/display_type_related';
    const SETTINGS_DISPLAY_TYPE_UPSELL = 'autorelated/setup/display_type_upsell';
    const ALLOWED_CATEGORIES = 'autorelated/setup/allowed_categories';

    const ADVANCED_SETTINGS_DISPLAY_TYPE_RELATED = 'autorelated/advanced_autorel_group/related';
    const ADVANCED_SETTINGS_DISPLAY_TYPE_UPSELL = 'autorelated/advanced_autorel_group/upsell';
    const ADVANCED_SETTINGS_DISPLAY_TYPE_CROSS = 'autorelated/advanced_autorel_group/crosssell';

    const ADVANCED_SETTINGS_PROD_ORDER_UPSELL = 'autorelated/setup/sort_order_upsell';

    /** @var \Magento\Store\Model\StoreManagerInterface */
    protected $_storeManager;
    protected $serialize;

    /**
     * @param Context $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Serialize\Serializer\Json $serialize
    ) {
        parent::__construct($context);
        $this->_storeManager = $storeManager;
        $this->serialize = $serialize;
    }

    public function getConfigData($field, $store = null)
    {
        $store = $this->_storeManager->getStore($store);
        $websiteId = $store->getWebsiteId();

        $result = $this->scopeConfig->getValue(
            $field,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
        return $result;
    }

    public function get_enabled($storeId = null)
    {
        return $this->getConfigData(self::SETTINGS_ENABLED, $storeId);
    }

    public function get_limit($storeId = null)
    {
        return $this->getConfigData(self::SETTINGS_LIMIT, $storeId);
    }

    public function get_cache_lifetime($storeId = null)
    {
        return $this->getConfigData(self::SETTINGS_CACHE_LIFETIME, $storeId);
    }

    public function getCrossDisplayType($storeId = null)
    {
        $advancedConfig = $this->getConfigData(self::ADVANCED_SETTINGS_DISPLAY_TYPE_CROSS, $storeId);
        if ($advancedConfig) {
            return ['advanced_cross' => $this->serialize->unserialize($advancedConfig)];
        }
        return 'disabled';
    }

    public function getAllowedCategories($storeId = null)
    {
        return $this->getConfigData(self::ALLOWED_CATEGORIES, $storeId);
    }
    
    public function getRelatedDisplayType($storeId = null)
    {
        $advancedConfig = $this->getConfigData(self::ADVANCED_SETTINGS_DISPLAY_TYPE_RELATED, $storeId);
        if ($advancedConfig) {
            return ['advanced_related' => $this->serialize->unserialize($advancedConfig)];
        }
        return $this->getConfigData(self::SETTINGS_DISPLAY_TYPE_RELATED, $storeId);
    }

    public function getUpsellDisplayType($storeId = null)
    {
        $advancedConfig = $this->getConfigData(self::ADVANCED_SETTINGS_DISPLAY_TYPE_UPSELL, $storeId);
        if ($advancedConfig) {
            return ['advanced_upsell' => $this->serialize->unserialize($advancedConfig)];
        }
        return $this->getConfigData(self::SETTINGS_DISPLAY_TYPE_UPSELL, $storeId);
    }

    public function getUpsellSortOrder($storeId = null)
    {
        return $this->serialize->unserialize($this->getConfigData(self::ADVANCED_SETTINGS_PROD_ORDER_UPSELL, $storeId));
    }
}
