<?php
/**
 * @author Colinet Julien
 */

namespace CpCreation\VitiCore\Bridge\Symfony\Bundle\Twig;

use CpCreation\VitiCore\Cart\Repository\CartRepository;
use CpCreation\VitiCore\Product\Model\ProductVariant;
use CpCreation\VitiCore\Product\Repository\ProductPriceCodeRepository;
use CpCreation\VitiCore\Product\Repository\ProductPriceGroupRepository;
use CpCreation\VitiCore\Shop\Model\ShippingCountry;
use CpCreation\VitiCore\Shop\Repository\ShippingCountryRepository;
use CpCreation\VitiCore\Shop\Repository\ShopConfigDisableCountryRepository;
use Symfony\Component\Intl\Countries;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class AppExtension extends AbstractExtension
{
    private $productPriceGroupRepository;
    private $productPriceCodeRepository;
    private $cartRepository;
    private $shippingCountryRepository;
    private $shopConfigDisableCountryRepository;

    public function __construct(ProductPriceGroupRepository        $productPriceGroupRepository,
                                ProductPriceCodeRepository         $productPriceCodeRepository,
                                CartRepository                     $cartRepository,
                                ShippingCountryRepository          $shippingCountryRepository,
                                ShopConfigDisableCountryRepository $shopConfigDisableCountryRepository
    )
    {
        $this->productPriceGroupRepository = $productPriceGroupRepository;
        $this->productPriceCodeRepository = $productPriceCodeRepository;
        $this->shippingCountryRepository = $shippingCountryRepository;
        $this->cartRepository = $cartRepository;
        $this->shopConfigDisableCountryRepository = $shopConfigDisableCountryRepository;
    }

    public function getFunctions()
    {
        return [
            new TwigFunction('countriesShippingByGroup', [$this, 'countriesShippingByGroup']),
            new TwigFunction('countryDisableDescription', [$this, 'countryDisableDescription']),
            new TwigFunction('soldOutWithStock', [$this, 'soldOutWithStock'])

        ];
    }

    public function getFilters()
    {
        return [
            new TwigFilter('saleSinceStockAt', [$this, 'saleSinceStockAt'])
        ];
    }

    public function countriesShippingByGroup($string, $groupId)
    {
        $group = $this->productPriceGroupRepository->find($groupId);

        $countries = $this->shippingCountryRepository->findCountriesAssignedByGroup($group->getId()->toString());
        /** @var ShippingCountry $country */
        $countriesWithZone = [];
        foreach ($countries as $country) {
            $countriesWithZone[] = Countries::getName($country->getCode());
        }

        return str_replace("%countries%", implode(", ", $countriesWithZone), $string);
    }

    public function countryDisableDescription($country)
    {
        $disableCountry = $this->shopConfigDisableCountryRepository->findOneBy(['country' => $country]);
        if (!$disableCountry) {
            return null;
        }
        return $disableCountry->getDescription();
    }

    public function saleSinceStockAt(ProductVariant $variant)
    {
        $quantitySale = $this->cartRepository->stockSaleSinceStockAt($variant)[0]['quantitySale'];
        if (!$quantitySale) {
            return 0;
        }

        return $quantitySale;
    }

    public function soldOutWithStock(ProductVariant $variant)
    {
        if ($variant->getStock() && $this->saleSinceStockAt($variant) >= $variant->getStock()) {
            return true;
        }

        return false;
    }
}
