<?php

namespace common\logic\Setting;

use common\logic\AbstractDefinition;
use common\logic\Setting\SettingDetails\Admin\AdminSettingDefinition;
use common\logic\Setting\SettingDetails\Producer\ProducerSettingDefinition;

class SettingDefinition extends AbstractDefinition
{
    public function getEntityFqcn(): string
    {
        return Setting::class;
    }

    public function getSettingDetailByName(string $name)
    {
        $adminSettingDefinition = AdminSettingDefinition::getInstance();
        $producerSettingDefinition = ProducerSettingDefinition::getInstance();

        $settingDetail = $this->findSettingDetailInArray($name, $adminSettingDefinition->getSettingDetails());
        if(!$settingDetail) {
            $this->findSettingDetailInArray($name, $producerSettingDefinition->getSettingDetails());
        }

        if(!$settingDetail) {
            throw new \Exception('SettingDetail non trouvé');
        }

        return $settingDetail;
    }

    public function findSettingDetailInArray(string $name, array $settingDetailsArray)
    {
        foreach($settingDetailsArray as $section => $subsectionsArray) {
            foreach($subsectionsArray as $subsection => $settingDetailsArray) {
                foreach($settingDetailsArray as $settingDetail) {
                    if($name == $settingDetail->getName()) {
                        return $settingDetail;
                    }
                }
            }
        }

        return null;
    }
}