<?php

namespace Autogedal\Helper\Model;

use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\FilterProductCustomAttribute;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Attribute\Backend\Media\EntryConverterPool;
use Magento\Framework\Api\AttributeValueFactory;
use Amasty\BannersLite\Model\ProductBannerProvider;

class ProductOverride extends Product
{
    /**
     * @var ProductBannerProvider
     */
    private $amastyProductBanners;
    /**
     * @var \Amasty\BannersLite\Model\ConfigProvider
     */
    private $bannersConfigProvider;

    public function __construct(
        ProductBannerProvider $amastyProductBanners,
        \Amasty\BannersLite\Model\ConfigProvider $bannersConfigProvider,
        \Magento\Framework\Model\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
        AttributeValueFactory $customAttributeFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Api\ProductAttributeRepositoryInterface $metadataService,
        Product\Url $url,
        Product\Link $productLink,
        \Magento\Catalog\Model\Product\Configuration\Item\OptionFactory $itemOptionFactory,
        \Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory $stockItemFactory,
        \Magento\Catalog\Model\Product\OptionFactory $catalogProductOptionFactory,
        \Magento\Catalog\Model\Product\Visibility $catalogProductVisibility,
        \Magento\Catalog\Model\Product\Attribute\Source\Status $catalogProductStatus,
        \Magento\Catalog\Model\Product\Media\Config $catalogProductMediaConfig,
        Product\Type $catalogProductType,
        \Magento\Framework\Module\Manager $moduleManager,
        \Magento\Catalog\Helper\Product $catalogProduct,
        \Magento\Catalog\Model\ResourceModel\Product $resource,
        \Magento\Catalog\Model\ResourceModel\Product\Collection $resourceCollection,
        \Magento\Framework\Data\CollectionFactory $collectionFactory,
        \Magento\Framework\Filesystem $filesystem,
        \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry,
        \Magento\Catalog\Model\Indexer\Product\Flat\Processor $productFlatIndexerProcessor,
        \Magento\Catalog\Model\Indexer\Product\Price\Processor $productPriceIndexerProcessor,
        \Magento\Catalog\Model\Indexer\Product\Eav\Processor $productEavIndexerProcessor,
        CategoryRepositoryInterface $categoryRepository,
        Product\Image\CacheFactory $imageCacheFactory,
        \Magento\Catalog\Model\ProductLink\CollectionProvider $entityCollectionProvider,
        \Magento\Catalog\Model\Product\LinkTypeProvider $linkTypeProvider,
        \Magento\Catalog\Api\Data\ProductLinkInterfaceFactory $productLinkFactory,
        \Magento\Catalog\Api\Data\ProductLinkExtensionFactory $productLinkExtensionFactory,
        EntryConverterPool $mediaGalleryEntryConverterPool,
        \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
        \Magento\Framework\Api\ExtensionAttribute\JoinProcessorInterface $joinProcessor,
        array $data = [],
        \Magento\Eav\Model\Config $config = null,
        FilterProductCustomAttribute $filterCustomAttribute = null
    ) {
        parent::__construct(
            $context,
            $registry,
            $extensionFactory,
            $customAttributeFactory,
            $storeManager,
            $metadataService,
            $url,
            $productLink,
            $itemOptionFactory,
            $stockItemFactory,
            $catalogProductOptionFactory,
            $catalogProductVisibility,
            $catalogProductStatus,
            $catalogProductMediaConfig,
            $catalogProductType,
            $moduleManager,
            $catalogProduct,
            $resource,
            $resourceCollection,
            $collectionFactory,
            $filesystem,
            $indexerRegistry,
            $productFlatIndexerProcessor,
            $productPriceIndexerProcessor,
            $productEavIndexerProcessor,
            $categoryRepository,
            $imageCacheFactory,
            $entityCollectionProvider,
            $linkTypeProvider,
            $productLinkFactory,
            $productLinkExtensionFactory,
            $mediaGalleryEntryConverterPool,
            $dataObjectHelper,
            $joinProcessor,
            $data = [],
            $config,
            $filterCustomAttribute
        );
        $this->amastyProductBanners = $amastyProductBanners;
        $this->bannersConfigProvider = $bannersConfigProvider;
    }

    public function hasGift()
    {
        return $this->_getData('kit_electric_inclus');
       // $bannerAlts = $this->getBannerAlts();
      //  return count($bannerAlts) > 0;
    }

    public function getBannerAlts()
    {
        $bannerAlts = [];
        foreach ($this->getBanners() as $banner) {
            if ($banner['banner_alt'] && $banner['banner_type'] == '2') {
                $bannerAlts[] = $banner['banner_alt'];
            }
        }
        return $bannerAlts;
    }

    public function getBanners()
    {
        if ($this->bannersConfigProvider->isBannersEnabled()) {
            return $this->amastyProductBanners->getBanners($this->getId());
        }
        return [];
    }

    public function hasSpecialPrice($getValue = false)
    {
        $SpecialPrice = $this->getPriceInfo()->getPrice('special_price')->getValue();

        if ($SpecialPrice || $this->getTypeId() == 'bundle') {
            if ($this->getTypeId() == 'bundle') {
                $regularPrice = $this->getPriceInfo()->getPrice('regular_price')->getMaximalPrice()->getValue();
                $finalPriceAmt = $this->getPriceInfo()->getPrice('final_price')->getMaximalPrice()->getValue();
            } else {
                $regularPrice = $this->getPriceInfo()->getPrice('regular_price')->getValue();
                $finalPriceAmt = $this->getPriceInfo()->getPrice('final_price')->getValue();
            }

            if ($finalPriceAmt > 0 && $regularPrice > 0) {
                $_savePercent = 100 - round(($finalPriceAmt / $regularPrice) * 100);
                if ($_savePercent > 0) {
                    switch ($getValue) {
                        case 1:
                            return $_savePercent;
                            break;
                        case 0:
                            return true;
                            break;
                    }
                }
            }
        }

        return false;
    }
}
