<?php

namespace common\components;

use common\logic\Producer\Producer\Model\Producer;
use common\logic\ProducerContextTrait;
use yii\base\ErrorException;

class BusinessLogic
{
    protected ?Producer $producerContext = null;

    use BusinessLogicTrait;

    public function getModules()
    {
        return [
            $this->getSettingModule(),
            $this->getFeatureModule(),
            $this->getFeatureProducerModule(),
            $this->getUnitModule(),
            $this->getTaxRateModule(),
            $this->getUserUserGroupModule(),
            $this->getUserGroupModule(),
            $this->getPaymentModule(),
            $this->getProducerPriceRangeModule(),
            $this->getUserProducerModule(),
            $this->getUserPointSaleModule(),
            $this->getUserModule(),
            $this->getPointSaleDistributionModule(),
            $this->getProductDistributionModule(),
            $this->getProductCategoryModule(),
            $this->getProductPointSaleModule(),
            $this->getProductPriceModule(),
            $this->getProductSubscriptionModule(),
            $this->getTicketUserModule(),
            $this->getTicketMessageModule(),
            $this->getTicketModule(),
            $this->getPointSaleModule(),
            $this->getProductModule(),
            $this->getProductOrderModule(),
            $this->getQuotationModule(),
            $this->getInvoiceModule(),
            $this->getDeliveryNoteModule(),
            $this->getDocumentModule(),
            $this->getSubscriptionModule(),
            $this->getDistributionModule(),
            $this->getProducerModule(),
            $this->getOrderModule(),
        ];
    }

    public function getProducerContext(): ?Producer
    {
        return $this->producerContext;
    }

    public function setProducerContext(Producer $producer = null)
    {
        $this->producerContext = $producer;

        foreach($this->getModules() as $module) {
            foreach($module->getServices() as $serviceClass) {
                $instanceService = $serviceClass::getInstance();
                $instanceService->setProducerContext($producer);
            }
        }
    }

    /*
     * Hiérarchie des apps
     */

    public function getModuleLevelHierarchyByService($serviceClass): int
    {
        $modulesArray = $this->getModules();
        $entityFqcnService = $this->getEntityFqcnByService($serviceClass);

        foreach($modulesArray as $key => $module) {
            if($module->getEntityFqcn() == $entityFqcnService) {
                return $key;
            }
        }
    }

    public function getEntityFqcnByService(string $serviceClass): string
    {
        $modulesArray = $this->getModules();

        foreach($modulesArray as $module) {
            if(in_array($serviceClass, $module->getServices())) {
                return $module->getEntityFqcn();
            }
        }

        throw new ErrorException('Service '.$serviceClass.' introuvable dans les modules.');
    }
}