<?php

namespace Oney\ThreeByFour\Block;

use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template;
use Oney\ThreeByFour\Helper\Config as HelperConfig;

/**
 * Abstract Oney Block
 * 
 * Base block class providing common functionality for Oney payment method blocks.
 * Handles legal text display, configuration checks, and template management.
 * 
 * @package Oney\ThreeByFour\Block
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class AbstractOney extends \Magento\Framework\View\Element\Template
{
    /**
     * Configuration helper instance
     * 
     * @var HelperConfig
     * @version 1.0.0
     */
    protected $helperConfig;

    /**
     * Constructor
     * 
     * @param Template\Context $context Template context
     * @param HelperConfig $helperConfig Configuration helper
     * @param array $data Additional data
     * @version 1.0.0
     */
    public function __construct(
        Template\Context $context,
        HelperConfig $helperConfig,
        array $data = []
    ) {
        parent::__construct($context, $data);
        $this->helperConfig = $helperConfig;
    }

    /**
     * Check if legal text should be displayed
     * 
     * @return bool True if legal banner should be shown
     * @version 1.0.0
     */
    public function isLegalTextShown(): bool
    {
        return $this->helperConfig->getCountrySpecificationsConfigValue("legal_banner");
    }

    /**
     * Check if current merchant is a credit intermediary
     * 
     * @return bool True if merchant is a credit intermediary
     * @version 1.0.0
     */
    public function isCreditIntermediary(): bool
    {
        return $this->helperConfig->isCreditIntermediary();
    }

    /**
     * Check if legal information is enabled
     * 
     * @return bool True if legal information is enabled
     * @version 1.0.0
     */
    public function isLegalEnabled(): bool
    {
        return $this->helperConfig->isLegalEnabled();
    }

    /**
     * Get company name from configuration
     * 
     * @return string Company name
     * @version 1.0.0
     */
    public function getCompanyName(): string
    {
        return $this->helperConfig->getCompanyName();
    }

    /**
     * Get Oney PDF document URL
     * 
     * @return string|null PDF URL if configured, null otherwise
     * @version 1.0.0
     */
    public function getOneyPdfUrl(): ?string
    {
        if ($this->helperConfig->getOneyPdf()) {
            return $this->getUrl('', ['_type' => UrlInterface::URL_TYPE_MEDIA]) . $this->helperConfig->getOneyPdf();
        }
        return null;
    }

    /**
     * Get Oney information URL
     * 
     * @return string Oney information URL
     * @version 1.0.0
     */
    public function getOneyUrl(): string
    {
        return HelperConfig::URL_ONEY_INFO;
    }

    /**
     * Get country class for specific CSS use
     * 
     * @return string|null Country code in lowercase for CSS classes
     * @version 1.0.0
     */
    public function getCssClassCountry(): ?string
    {
        return strtolower($this->helperConfig->getConfigValue('general/country/default'));
    }

    /**
     * Check if custom country template exists and set it
     * 
     * @param string $template Template name to check
     * @return void
     * @version 1.0.0
     */
    protected function checkAndSetTemplate(string $template): void
    {
        if ($this->helperConfig->getCountrySpecificationsConfigValue('country')) {
            $countryTemplate = $this->helperConfig->getCountrySpecificationsConfigValue('country') . '/' . $template;
            if ($this->getTemplateFile($countryTemplate)) {
                $template = $countryTemplate;
            }
            $this->setTemplate($template);
        }
    }

    /**
     * Add country code translation
     * 
     * Handle language translations for a specific country
     * 
     * @return string Country code translation
     * @version 1.0.0
     */
    public function addCountryCodeTranslation(): string
    {
        return $this->helperConfig->addCountryCodeTranslation();
    }

    /**
     * Check if only instalments from API should be used
     * 
     * @return bool True if only API instalments should be used
     * @version 1.0.0
     */
    public function isOnlyInstalmentsFromApi(): bool
    {
        return $this->helperConfig->getCountrySpecificationsConfigValue("add_an_instalment");
    }

}
