<?php

namespace Lc\SovBundle\Solver\Setting;

use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Model\Setting\SettingInterface;
use Lc\SovBundle\Model\Setting\SettingModel;

class SettingSolver
{
    public function getSetting(EntityInterface $entity, string $name): ?SettingModel
    {
        if ($entity->getSettings()) {
            foreach ($entity->getSettings() as $setting) {
                if ($setting->getName() == $name) {
                    return $setting;
                }
            }
        }

        return null;
    }

    public function getSettingValue(EntityInterface $entity, string $name)
    {
        $setting = $this->getSetting($entity, $name);

        if($setting) {
            return $this->getValue($setting);
        }

        return null;
    }

    public function getValue(SettingInterface $setting)
    {
        if ($setting->getText()) {
            return $setting->getText();
        } elseif ($setting->getDate()) {
            return $setting->getDate();
        } elseif ($setting->getFile()) {
            return $setting->getFile();
        }

        return null;
    }

    public function getDefaultValue(EntityInterface $entity, $settingDefault)
    {
        $valueDefault = null;

        if(is_array($settingDefault)) {
            if(isset($settingDefault[$entity->getDevAlias()])) {
                $valueDefault = $settingDefault[$entity->getDevAlias()];
            }
        }
        else {
            $valueDefault = $settingDefault;
        }

        return $valueDefault;
    }
}
