<?php

namespace Leadlion\ProductUsp\Block;

class UspBlock extends \Magento\Framework\View\Element\Template
{
    /**
    * @var \Magento\Catalog\Helper\Category
    */
    protected $categoryRepository;
    
    public function __construct(
    \Magento\Catalog\Model\CategoryRepository $categoryRepository,
    \Leadlion\Autogedal\Helper\Config $helper,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Block\Product\View $product,
    \Magento\Catalog\Block\Category\View $category        
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->helper = $helper;
        $this->_storeManager = $storeManager;
        $this->_product = $product;
        $this->_category = $category;
        parent::__construct($context);
    }
    
    public function getProductUspHeader($categories)
    {
        $uspBlockName = null;
        if(is_array($categories)):
            foreach($categories as $category):
                if($this->getCategory($category)->getProductUspBlockHeader() && $this->getCategory($category)->getIsActive()):
                    $uspBlockName = $this->getCategory($category)->getProductUspBlockHeader();
                endif;
            endforeach;
        else:
            $uspBlockName = $this->getCategory($categories)->getProductUspBlockHeader();
        endif;
        
        if(!$uspBlockName):
           $uspBlockName = $this->helper->getConfig('leadlion/category_usp/product_no_usp', $this->_storeManager->getStore()->getId()); 
        endif;
        
        return $uspBlockName;
    }
    public function getFooterUsp()
    {
        $uspBlockName = null;
        $product = $this->_product->getProduct();
        $category = $this->_category->getCurrentCategory();
        if($product):
            $categories = $product->getCategoryIds();
            if($categories):
                foreach($categories as $category):
                    if($this->getCategory($category)->getProductUspBlockFooter() && $this->getCategory($category)->getIsActive()):
                        $uspBlockName = $this->getCategory($category)->getProductUspBlockFooter();
                    endif;
                endforeach;
            endif;    
        elseif($category):
            $uspBlockName = $category->getProductUspBlockFooter();
        else:
            $uspBlockName = $this->helper->getConfig('leadlion/category_usp/pages_usp', $this->_storeManager->getStore()->getId());   
        endif;   
        
//      check if usp is not set in category / product
        
        if(!$uspBlockName):
            $uspBlockName = $this->helper->getConfig('leadlion/category_usp/pages_usp', $this->_storeManager->getStore()->getId()); 
        endif;
        
        return $uspBlockName;
    }
    
    public function getCategory($category)
    {
        $categoryData = $this->categoryRepository->get($category);
        
        return $categoryData;
    }

    public function getUPSBlockCategory() {
        $uspBlockName = null;
        $category = $this->_category->getCurrentCategory();

       if ($category) {
        $uspBlockName = $category->getProductUspBlockHeader();
       }
                
        return $uspBlockName;
    }
    
}
