<?php

use App\Libs\Database\Eloquent\WebsiteObjectCollection;
use App\Libs\Facades\Localization;
use Illuminate\Database\Eloquent\SoftDeletes;

class WebsiteObject extends BaseModel
{
    use SoftDeletes;

    protected $primaryKey = 'website_object_id';
    protected $hidden = array('languages');

    protected $languageAttributes = array();

    public function languages()
    {
        return $this->hasMany("WebsiteObjectLanguage", "website_object_id");
    }

    public function objectable()
    {
        return $this->morphTo(null, "object_type", "object_id");
    }

    public function related()
    {
        return $this->belongsToMany('WebsiteObject', "related", "website_object_id", "related_id")->withPivot('type',
            'position');
    }

    public function website()
    {
        return $this->belongsTo('Website');
    }

    public function parent()
    {
        return $this->belongsTo('WebsiteObject', 'parent_id');
    }

    public function children()
    {
        return $this->hasMany('WebsiteObject', 'parent_id');
    }

    public function searches()
    {
        return $this->belongsToMany('Language', "website_object_search", 'website_object_id')->withPivot("primary", "secondary");
    }

    public function menuItems()
    {
        return $this->hasMany('MenuItem', 'website_object_id', 'website_object_id');
    }

    public function newCollection(array $models = array())
    {
        return new WebsiteObjectCollection($models);
    }

    public static function maxPosition($websiteId, $objectType)
    {
        return WebsiteObject::where("object_type", $objectType)
            ->where("website_id", $websiteId)->max("position");
    }

    public function getIdAttribute($value)
    {
        return $this->getKey();
    }

    public function getAttribute($key)
    {
        if ($key == "id") {
            $key = $this->getPrimaryKeyName();
        }
        if (parent::keyExists($key)) {
            return parent::getAttribute($key);
        }

        if ($this->exists) {

            $activeLang = $this->getActiveLanguage();

            if (!empty($activeLang) && !empty($activeLang->$key)) {
                return $activeLang->$key;
            }

            if (config("app.showDefaultLanguage")) {

                $defaultLang = $this->getDefaultLanguage();

                if (!empty($defaultLang) && !empty($defaultLang->$key)) {
                    return $defaultLang->$key;
                }
            }
        }

        return null;

    }

    public function getDefaultLanguage()
    {
        $languagesData = $this->languages;
        if (empty($languagesData)) {
            return null;
        }
        $language = $languagesData->findByLocale(Localization::getDefaultLocale());
        return $language;
    }

    public function getActiveLanguage()
    {
        $languagesData = $this->languages;

        if (empty($languagesData)) {
            return null;
        }

        $language = $languagesData->findByLocale(Localization::getActiveLocale());
        return $language;
    }

    public function toArray()
    {
        $array = parent::toArray();
        if (isset($this->relations["languages"])) {
            $defaultLanguage = $this->getActiveLanguage();
            $languageAttributes = array_keys($defaultLanguage->getAttributes());

            foreach ($languageAttributes as $languageAttribute) {
                if (!in_array($languageAttribute, $this->hidden)) {
                    $array[$languageAttribute] = $this->$languageAttribute;
                    if ($array[$languageAttribute] instanceof DateTime) {
                        $array[$languageAttribute] = $array[$languageAttribute]->format($this->getDateFormat());
                    }
                }
            }
        }
        return $array;
    }
}
