<?php
namespace TbiBank\FusionPay\Block\Frontend;

// Import necessary classes
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use TbiBank\FusionPay\Helper\Data as FusionPayHelper;
use TbiBank\FusionPay\Helper\ProductHelper;

/**
 * Advert Block for FusionPay
 *
 * This block class is designed to handle the advertisement functionalities
 * for FusionPay, including enabling the advert, fetching the advert URL, and
 * retrieving the TBI logo.
 */
class ProductModal extends Template
{
    /**
     * @var FusionPayHelper Helper instance to access FusionPay's functionalities
     */
    protected $fusionPayHelper;

    /**
     * Constructor
     *
     * @param Context $context Context object with environment data
     * @param FusionPayHelper $fusionPayHelper Helper object for FusionPay functionalities
     * @param array $data Additional data array
     */
    public function __construct(
        Context $context,
        FusionPayHelper $fusionPayHelper,
        ProductHelper $productHelper,
        array $data = []
    ) {
        parent::__construct($context, $data); // Call the parent constructor
        $this->fusionPayHelper = $fusionPayHelper; // Initialize FusionPay helper
        $this->productHelper = $productHelper; // Initialize Product helper
    }

    /**
     * Check if the advertisement is enabled
     *
     * This method utilizes the helper to check whether the advertisement feature
     * is enabled in the FusionPay configuration.
     *
     * @return bool True if advert is enabled, false otherwise
     */
    public function isEnabled()
    {
        return $this->fusionPayHelper->isProductFeatureEnabled();
    }

    /**
     * Get the TBI logo URL
     *
     * This method uses the helper to obtain the URL of the TBI logo.
     *
     * @return string The URL of the TBI logo
     */
    public function getTbiLogo()
    {
        return $this->fusionPayHelper->getTbiLogo();
    }

    /**
     * Get Loading Image
     *
     * @return string
     */
    public function getLoadingImg()
    {
        return $this->fusionPayHelper->getConstValue('TBI_LOADING_SVG');
    }

    /**
     * Get all combinations of options for a configurable product.
     *
     * @param int $productId The ID of the configurable product.
     * @return array An array of all possible option combinations.
     */
    public function getProductCombinations($productId)
    {
        return $this->productHelper->getProductCombinations($productId);
    }

    /**
     * Get Allowed Origin
     *
     * @return string
     */
    public function getAllowedOrigin()
    {
        return $this->fusionPayHelper->getConstValue('TBI_PLATFORM_URL');
    }

    /**
     * Render block HTML
     * Only render if product feature is enabled
     *
     * @return string
     */
    protected function _toHtml()
    {
        if (!$this->fusionPayHelper->isProductFeatureEnabled()) {
            return '';
        }
        
        return parent::_toHtml();
    }
}
