<?php

namespace Lc\SovBundle\Definition;

abstract class AbstractSettingDefinition
{
    protected $settings = [];

    public function addSettingText(array $params): self
    {
        $params['type'] = 'text';
        $params['field'] = 'text';
        return $this->addSetting($params);
    }

    public function addSettingTextarea(array $params): self
    {
        $params['type'] = 'textarea';
        $params['field'] = 'text';
        return $this->addSetting($params);
    }

    public function addSettingTextareaAdvanced(array $params): self
    {
        $params['type'] = 'textarea_advanced';
        $params['field'] = 'text';
        return $this->addSetting($params);
    }

    public function addSettingDate(array $params): self
    {
        $params['type'] = 'date';
        $params['field'] = 'date';
        return $this->addSetting($params);
    }

    public function addSettingTime(array $params): self
    {
        $params['type'] = 'time';
        $params['field'] = 'date';
        return $this->addSetting($params);
    }

    public function addSettingFile(array $params): self
    {
        $params['type'] = 'file';
        $params['field'] = 'file';
        return $this->addSetting($params);
    }

    public function addSettingImage(array $params): self
    {
        $params['type'] = 'image';
        $params['field'] = 'file';
        return $this->addSetting($params);
    }

    public function addSettingSelect(array $params): self
    {
        $params['type'] = 'select';
        $params['field'] = 'text';
        return $this->addSetting($params);
    }

    public function addSettingRadio(array $params): self
    {
        $params['type'] = 'radio';
        $params['field'] = 'text';
        return $this->addSetting($params);
    }

    public function addSetting($params)
    {
        $name = $params['name'];
        $category = $params['category'];

        if (!isset($this->settings[$category])) {
            $this->settings[$category] = [];
        }

        $this->settings[$category][$name] = $params;

        return $this;
    }

    public function getSettings(): array
    {
        return $this->settings;
    }

    public function getSettingsByCategory($category)
    {
        $settings = $this->getSettings();

        if (isset($settings[$category])) {
            return $settings[$category];
        }

        return [];
    }

    public function getSettingByName($name): ?array
    {
        $settings = $this->getSettings();

        foreach ($settings as $category => $settingsCategory) {
            foreach ($settingsCategory as $nameSetting => $setting) {
                if ($nameSetting == $name) {
                    return $setting;
                }
            }
        }

        return null;
    }

    public function getSettingType($name): ?string
    {
        $setting = $this->getSettingByName($name);

        if ($setting) {
            return $setting['type'];
        }

        return null;
    }
}