<?php

namespace App\Libs\Backoffice\Fields;

use App\Libs\Backoffice\Contracts\SearchableField;
use Illuminate\Database\Eloquent\Model;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class TextAreaField extends BaseField implements SearchableField
{
    protected $heights = [
        'small' => 2,
        'medium' => 5,
        'big' => 10,
    ];

    protected $height = 5;
    protected $maxLength;

    public function getMaxLength()
    {
        return $this->maxLength;
    }

    public function setMaxLength($maxLength = null)
    {
        $this->maxLength = $maxLength;
    }

    public function __construct($config)
    {
        unset($config["inList"]);
        parent::__construct($config);

        if (!empty($config["height"])) {
            if (array_key_exists($config['height'], $this->heights)) {
                $this->height = $this->heights["{$config["height"]}"];
            }
        }

        if (!empty($config["maxLength"])) {
            $this->maxLength = $config['maxLength'];
        }
    }

    public function getData(Model $model, $enabled = true, $hidden = false)
    {
        if ($this->isMultiLanguage()) {
            $locales = LaravelLocalization::getSupportedLocales();

            foreach ($locales as $locale => $localeData) {
                if ($model->exists) {
                    $languageModel = $model->languages->findByLocale($locale);

                    if (!empty($languageModel)) {
                        $value[$locale] = $languageModel->$fieldName;
                    } else {
                        $value[$locale] = '';
                    }
                } else {
                    $value[$locale] = '';
                }
            }
        } else {
            $fieldName = $this->getName();
            $value = null;

            if ($model->exists) {
                $value = $model->$fieldName;
            }
        }

        return [
            'name' => $this->getName(),
            'type' => ucfirst($this->getType()),
            'isMultilanguage' => $this->isMultiLanguage(),
            'label' => $this->getLabel(),
            'info' => $this->getInfo(),
            'value' => $value,
            'height' => $this->height,
            'maxLength' => $this->maxLength,
            'hide' => $hidden,
            'enabled' => $enabled,
            'conditions' => $this->conditions,
        ];
    }

    public function getSearchStructure(Model $model, $view = false)
    {
        $searchData = array();
        $fieldName = $this->getName();
        if ($this->isMultiLanguage()) {
            foreach ($model->languages as $languageModel) {
                if (!empty($languageModel->$fieldName)) {
                    if ($view) {
                        $value = cut_string($languageModel->$fieldName, 500);
                    } else {
                        $value = $languageModel->$fieldName;
                    }
                    $searchData[$languageModel->language_id] = $value;
                } else {
                    $searchData[$languageModel->language_id] = '';
                }
            }
        } else {

            $languages = LaravelLocalization::getSupportedLocales();
            foreach ($languages as $locale => $localeData) {
                if (!empty($model->$fieldName)) {
                    if ($view) {
                        $value = cut_string($model->$fieldName, 500);
                    } else {
                        $value = $model->$fieldName;
                    }
                    $searchData[$locale] = $value;
                } else {
                    $searchData[$locale] = '';
                }
            }

        }
        return $searchData;
    }
}


