<?php

namespace Lc\ShopBundle\Services\Price;


use Lc\ShopBundle\Context\OrderProductInterface;
use Lc\ShopBundle\Context\OrderShopInterface;
use Lc\ShopBundle\Context\ProductPropertyInterface;

trait PriceUtilsTrait
{
        public function applyTax($price, $taxRateValue)
        {
                return $this->round($this->applyPercent($price, $taxRateValue));
        }

        public function applyReductionPercent($price, $percentage)
        {
                return $this->applyPercent($price, -$percentage);
        }

        public function applyReductionAmount($price, $amount)
        {
                return $price - $amount;
        }

        public function applyPercent($price, $percentage)
        {
                return $price * ($percentage / 100 + 1);
        }

        public function applyPercentNegative($price, $percentage)
        {
                return $price / ($percentage / 100 + 1);
        }

        public function round($price)
        {
                return round((($price * 100)) / 100, 2);
        }
        public function amountReductionByPercentValue($price, $percent){
                return round((($price * $percent)) / 100, 2);
        }

        public function applyReductionCatalog($entity, $price, $priceWithTax, $quantity = 1, $reductionCatalog = null, $withTax = true): ?float
        {

                if ($reductionCatalog) {
                        $reductionCatalogValue = $reductionCatalog->getValue();
                        $reductionCatalogUnit = $reductionCatalog->getUnit();
                        $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
                } else {
                        if ($entity instanceof ProductPropertyInterface) {
                                $reductionCatalog = $entity->getReductionCatalogInherited();

                                if ($reductionCatalog) {
                                        $reductionCatalogValue = $reductionCatalog->getValue();
                                        $reductionCatalogUnit = $reductionCatalog->getUnit();
                                        $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate();
                                }
                        }

                        if ($entity instanceof OrderProductInterface) {
                                $orderProductReductionCatalog = $entity->getOrderProductReductionCatalog();
                                if ($orderProductReductionCatalog) {
                                        $reductionCatalogValue = $orderProductReductionCatalog->getValue();
                                        $reductionCatalogUnit = $orderProductReductionCatalog->getUnit();
                                        $reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate();
                                }
                        }
                }

                if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
                        if ($reductionCatalogUnit == 'percent') {
                                $priceWithTax = $this->applyReductionPercent(
                                        $priceWithTax,
                                        $reductionCatalogValue
                                );
                        } elseif ($reductionCatalogUnit == 'amount') {
                                if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
                                        $priceWithTax = $this->applyTax(
                                                $this->applyReductionAmount(
                                                        $price,
                                                        $reductionCatalogValue * $quantity
                                                ),
                                                $entity->getTaxRateInherited()->getValue()
                                        );
                                } elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') {
                                        $priceWithTax = $this->applyReductionAmount(
                                                $priceWithTax,
                                                $reductionCatalogValue * $quantity
                                        );
                                }
                        }
                }

                if ($withTax) {
                        $priceReturn = $priceWithTax;
                } else {
                        $priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue());
                }

                return $this->round($priceReturn);
        }




}