<?php

use Leanpay\Payment\Api\Data\InstallmentInterface;
use Leanpay\Payment\Block\Installment\Pricing\Render\DefaultPriceBox;
use Leanpay\Payment\Helper\InstallmentHelper;
use Magento\Framework\Escaper;

/** @var DefaultPriceBox $block */
/** @var Escaper $escaper */

$financialProduct = $block->getFinancialProduct();
$vendorProductName = $block->getInstallmentVendorName($financialProduct);
$isPreCalculated = (bool)$vendorProductName;

if ($vendorProductName) {
    $price = $block->getLowestInstallmentPrice($vendorProductName);
} else {
    $price = $block->getLowestInstallmentPrice();
}

$viewKey = $block->getData('view_key') ?? InstallmentHelper::LEANPAY_INSTALLMENT_VIEW_OPTION_PRODUCT_PAGE;
$helper = $block->getInstallmentHelper();
$canShow = $helper->canShowInstallment($viewKey);

$amount = intval(round($block->getAmount()));
$meetsThreshold = $helper->meetsAmountThreshold($amount);

$iconPath = 'Leanpay_Payment::images/' . $helper->getColorTheme()['icons'] . '/';
?>
<?= /* @noEscape */ $block->fetchView($block->getTemplateFile('Leanpay_Payment::pricing/render/installment_theme.phtml')); ?>
<?php if ($viewKey !== InstallmentHelper::LEANPAY_INSTALLMENT_VIEW_OPTION_PRODUCT_PAGE): ?>
    <?php if ($price && $canShow && $meetsThreshold): ?>
        <div class="installment-price plp-installment"
             style="color: var(--lp-text-plp); background-color: var(--lp-bg-plp);">
            <?= $escaper->escapeHtml($helper->getCategoryPriceBlock($amount, $price)); ?>
            <img class="installment-logo"
                 src="<?= /* @noEscape */ $block->getLogo(); ?>"
                 alt="leanpay-logo"/>
        </div>
    <?php elseif ($price && $canShow && !$meetsThreshold): ?>
        <div class="installment-price plp-installment"
             style="color: var(--lp-text-plp); background-color: var(--lp-bg-plp);">
            <span><?= $escaper->escapeHtml($helper->getUnderThresholdText()); ?></span>
            <img class="installment-logo"
                 src="<?= /* @noEscape */ $block->getLogo(); ?>"
                 alt="leanpay-logo"/>
        </div>
    <?php endif; ?>
<?php else: ?>
    <?php if ($price && $canShow): ?>
        <div class="installment-wrapper" data-mage-init='{"Leanpay_Payment/js/installment":""}'>
            <div class="installment-additional-wrapper">
                <?php
                $list = $helper->getInstallmentList($amount, $vendorProductName);
                $maxPeriod = $helper->getMaxInstallmentPeriod($amount, $vendorProductName);
                $isWithinThreshold = $helper->isWithinThreshold($amount);
                $currentPeriod = $isWithinThreshold
                    ? $helper->getEffectiveDefaultInstallmentCount($amount, (string) $vendorProductName)
                    : $helper->getInstallmentPeriodForPrice($amount, $price, $vendorProductName);
                ?>
                <div class="installment-pdp-block"
                     style="background-color: var(--lp-bg-primary); color: var(--lp-text-primary);">
                    <div class="installment-pdp-content">
                    <div class="installment-pdp-left">
                        <img class=""
                             src="<?= /* @noEscape */ $block->getViewFileUrl(
                                 $isWithinThreshold
                                     ? $iconPath . 'leanpat_short_logo_0.png'
                                     : $iconPath . 'leanpay_short_logo.png'
                             ); ?>"
                             alt=""
                             aria-hidden="true"/>
                    </div>
                        <div class="installment-pdp-text">
                            <div class="installment-pdp-price">
                                <?php if ($isWithinThreshold): ?>
                                    <span class="installment-price-amount">
                                        <?= $escaper->escapeHtml($helper->getProductPriceBlock($amount, $price)); ?>
                                    </span>
                                    <?php if ($currentPeriod > 0): ?>
                                        <span class="installment-price-count">
                                            <?= $escaper->escapeHtml(' x ' . $currentPeriod . ' ' . ($currentPeriod === 1 ? __('obrok') : __('obroki'))); ?>
                                        </span>
                                    <?php endif; ?>
                                <?php else: ?>
                                    <span class="installment-price-from">
                                        <?= $escaper->escapeHtml(__('od ') ); ?>
                                    </span>
                                    <span class="installment-price-amount">
                                        <?= $escaper->escapeHtml($helper->getProductPriceOnly($amount, $price)); ?>
                                    </span>
                                    <span class="installment-price-month">
                                        <?= $escaper->escapeHtml(' ' . __('/mesec')); ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                            <?php if ($maxPeriod > 0): ?>
                                <div class="installment-pdp-period">
                                    <?php if (!$isWithinThreshold): ?>
                                        <?= $escaper->escapeHtml(__('Razdeli na do %1 obrokov', $maxPeriod)); ?>
                                    <?php else: ?>
                                        <?= $escaper->escapeHtml(__('0% obresti, 0 stroškov')); ?>
                                    <?php endif; ?>
                                    <img class="installment-logo"
                            src="<?= /* @noEscape */ $block->getViewFileUrl($iconPath . 'leanpay-original.svg'); ?>"
                            alt="leanpay-logo"
                            style="color: var(--lp-text-primary);"/>
                                </div>
                            <?php endif; ?>
                        </div>
                        <button type="button" class="installment-calculate-btn" aria-label="<?= $escaper->escapeHtmlAttr(__('Izračun')); ?>">
                            <img class="installment-calculate-arrow"
                                 src="<?= /* @noEscape */ $block->getViewFileUrl($iconPath . 'dropdown.svg'); ?>"
                                 alt=""
                                 aria-hidden="true"/>
                        </button>
                    </div>
                </div>
                <div class="installment-mouse">
                    <div class="installment-info">
                        <?php if ($helper->allowDownPayment()): ?>
                            <div class="installment-deposit">
                            <span class="deposit">
                                <?= $escaper->escapeHtml(
                                    __('+ %1 € polog', $helper->getDownPaymentRule($amount))
                                ); ?>
                            </span>
                            </div>
                        <?php endif; ?>

                    </div>
                    <div class="installment-tooltip hidden">
                        <div class="installment-tooltip-header">
                            <div class="installment-tooltip-title">
                                <?= $escaper->escapeHtml(__('Izračun obrokov')); ?>
                            </div>
                            <button type="button" class="installment-tooltip-close" aria-label="<?= $escaper->escapeHtmlAttr(__('Close')); ?>">
                                <span>×</span>
                            </button>
                        </div>
                        <div class="installment-title">
                            <img class="installment-logo"
                                 src="<?= /* @noEscape */
                                 $block->getLogo(); ?>"
                                 alt="leanpay-logo"/>
                            <div class="installment-title-price">
                                <span class="installment-title-price-amount"><?= $escaper->escapeHtml($helper->getProductPriceBlock($amount, $price)); ?></span>
                                <span><?= $escaper->escapeHtml(__('/mesec')); ?></span>
                            </div>
                        </div>
                        <div class="installment-plans">
                            <div class="installment-slider-term" data-month-label="<?= $escaper->escapeHtmlAttr(__('mes.')); ?>">
                                <div class="installment-slider"></div>
                            </div>
                        </div>
                        <div class="installment-quick-information">
                            <?= $escaper->escapeHtml($helper->getQuickInformation()); ?>
                            <a href="<?= $escaper->escapeUrl($helper->getMoreInfoURL()); ?>"
                               target="_blank"
                               class="link"
                               rel="noopener noreferrer">
                                <span
                                    class="link-option"><?= $escaper->escapeHtml(__('Več informacij')); ?></span>
                            </a>
                        </div>
                        <div class="installment-links">
                            <a href="<?= $escaper->escapeUrl($helper->getCheckYourLimitURL()); ?>"
                               target="_blank"
                               rel="noopener noreferrer">
                                <span
                                    class="link-option"><?= $escaper->escapeHtml(__('Preveri svoj limit')); ?></span>
                                <img class="installment-link-arrow"
                                     src="<?= /* @noEscape */ $block->getViewFileUrl($iconPath . 'arrow-right.svg'); ?>"
                                     alt=""
                                     aria-hidden="true"/>
                            </a>

                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="installment-slider-data" style="display: none; visibility: hidden;">
            <?= $helper->getJsonConfig($amount, $vendorProductName) ?: ''; ?>
        </div>
    <?php endif; ?>
<?php endif; ?>
