<?php

namespace Oney\ThreeByFour\Block\Checkout;

use Magento\Checkout\Block\Cart\Totals;
use Magento\Checkout\Model\Session;
use Oney\ThreeByFour\Api\Marketing\SimulationInterface;
use Oney\ThreeByFour\Helper\Config;
use Magento\Sales\Model\ConfigInterface;
use Magento\Sales\Model\Config as ConfigSales;

class Cart extends Totals
{
    /**
     * @var SimulationInterface
     */
    protected $_simulationOney;
    /**
     * @var Session
     */
    protected $_checkoutSession;
    /**
     * @var Config
     */
    protected $configHelper;


    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Checkout\Model\Session $checkoutSession,
        SimulationInterface $simulation,
        ConfigInterface $salesConfig,
        ConfigSales $salesConfigOld,
        Config $configHelper,
        array $layoutProcessors = [],
        array $data = [])
    {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $productMetadata = $objectManager->get('Magento\Framework\App\ProductMetadataInterface');

        if (version_compare($productMetadata->getVersion(), '2.3.3') < 0) {
            parent::__construct($context, $customerSession, $checkoutSession, $salesConfigOld, $layoutProcessors, $data);
        } else {
            parent::__construct($context, $customerSession, $checkoutSession, $salesConfig, $layoutProcessors, $data);
        }
        $this->_simulationOney = $simulation;
        $this->configHelper = $configHelper;
        $this->checkAndSetTemplate('checkout/cart.phtml');
    }

    public function getSimulations(){
        return $this->_simulationOney
            ->build($this->_checkoutSession->getQuote()->getGrandTotal())
            ->getSimulations();
    }

    public function renderInstalments() {
        $array = [];
        $text = "";
        foreach ($this->getSimulations() as $simulation) {
            if(isset($simulation['instalments']) && !in_array(count($simulation['instalments']) + 1, $array, true)){
                $array[] = count($simulation['instalments']) + 1;
            }
        }
        foreach ($array as $item) {
            if(end($array) === $item && count($array) !== 1){
                $text .= " ".__('Or') . " " . $item;
            }
            else{
                $text .= " ". $item;
            }
        }
        return $text;
    }

    public function isCreditIntermediary() {
        return $this->configHelper->isCreditIntermediary();
    }

    public function isLegalEnabled() {
        return $this->configHelper->isLegalEnabled();
    }

    protected function checkAndSetTemplate($template)
    {
        if($this->configHelper->getCountrySpecificationsConfigValue('country')) {
            if ($this->getTemplateFile(
                $this->configHelper->getCountrySpecificationsConfigValue('country')
                .'/'.$template)
            ) {
                $template = $this->configHelper->getCountrySpecificationsConfigValue('country') .'/'.$template;
            }
            $this->setTemplate($template);
        }
    }

    /**
     * addCountryCodeTranslation
     * Handle languages transations for a specific country
     * @return string
     */
    public function addCountryCodeTranslation()
    {
        return $this->configHelper->addCountryCodeTranslation();
    }

    public function getMerchantGuid()
    {
        return $this->configHelper->getMerchantGuid();
    }

    /**
     * @return string|null
     */
    public function getCountryCode()
    {
        return $this->configHelper->getConfigValue('general/country/default');
    }

    /**
     * @return string
     */
    public function getLanguageCode()
    {
        return $this->configHelper->getLanguageCode();
    }

    /**
     * @return array
     */
    public function getFranceWidgetOptions(): array
    {
        return $this->configHelper->getFranceWidgetOptions();
    }

    public function isFree() {
        $isFree = true;
        foreach ($this->getSimulations() as $simulation) {
            $isFree = $isFree && $simulation['total_cost'] == 0;
        }
        return $isFree;
    }
}
