<?php

namespace Leadlion\Autogedal\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    const OOS_IGNORED_PRODUCTS_PATH = 'leadlion/products/ids';
    const ATTRIBUTES_SETS_SKU = 'leadlion/sku_fz_custom_conf/ids';

    protected $scopeConfig;
    protected $_objectManager = null;
    protected $_attributes        = [];
    protected $_eavSetup        = null;

    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Framework\ObjectManagerInterface $objectmanager
    ) {
        $this->scopeConfig = $scopeConfig;
        $this->_objectManager = $objectmanager;
    }

    public function getAttributesSetsIdsForShowingSku()
    {
        return $this->scopeConfig->getValue(
            self::ATTRIBUTES_SETS_SKU,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            null
        );
    }

    public function getIgnoredProductsIds()
    {
        return $this->scopeConfig->getValue(
            self::OOS_IGNORED_PRODUCTS_PATH,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            null
        );
    }

    public function getAttribute($_code, $_force = false)
    {
        if (!isset($this->_attributes[$_code]) || $_force) {
            $_attribute = $this->_objectManager->get("Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory")->create();
            $this->_attributes[$_code] = $_attribute->loadByCode(\Magento\Catalog\Api\Data\ProductAttributeInterface::ENTITY_TYPE_CODE, $_code);
        }

        return $this->_attributes[$_code];
    }

    public function getEavSetup()
    {
        if (!$this->_eavSetup) {
            $this->_eavSetup = $this->_objectManager->get('Magento\Eav\Setup\EavSetupFactory')->create([
                'setup' => $this->_objectManager->get('Magento\Framework\Setup\ModuleDataSetupInterface')
            ]);
        }
        return $this->_eavSetup;
    }

    public function getAttributeOptionId($_code, $_value) {
        $attribute = $this->getAttribute($_code);   
        if(!$_value || !$attribute || !$attribute->getId()) {
            return ''; 
        }

        $optionId = '';
        $options = $attribute->getSource()->getAllOptions(false);

        if($options && count($options) > 0) {
            foreach($options as $option){
                if(trim(strtolower($option['label'])) ==  strtolower($_value)){
                    $optionId = $option['value'];
                    break;
                }
            }
        }    

        if(!$optionId) {
            $option = [];
            $option['attribute_id'] = $attribute->getAttributeId();
            $option['value']['option'][0] = $_value; 
            $option['value']['option'][1] = $_value; 
            $option['order']['option'] = ''; 
            $this->getEavSetup()->addAttributeOption($option);

            $attribute = $this->getAttribute($_code, true);
            $options = $attribute->getSource()->getAllOptions(false);

            if($options && count($options) > 0) {
                foreach($options as $option){
                    if(trim(strtolower($option['label'])) ==  strtolower($_value)){
                        $optionId = $option['value'];
                        break;
                    }
                }
            } 
        } 
        return $optionId;
    }
}
