<?php

namespace App\Models\GenericBackoffice;

use App\Libs\Database\Eloquent\BaseModel;
use App\Libs\Database\Eloquent\Collection\WebsiteObjectCollection;
use DateTime;

class WebsiteObject extends BaseModel
{
    protected $primaryKey = 'website_object_id';
    protected $hidden = array('languages');

    protected $languageAttributes = array();

    protected $casts = ['tree_ids' => 'json'];

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

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

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

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

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

    public function searches()
    {
        return $this->hasMany(WebsiteObjectSearch::class, 'website_object_id');
    }

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

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

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

    public static function hasSlug($slug, $langId)
    {
        $count = WebsiteObjectLanguage::where('lang_slug', $slug)
            ->where('wo_language_id', $langId)
            ->count();

        return $count > 0;
    }

    public function getAttribute($key)
    {
        if ($key == "id") {
            $key = $this->getKeyName();
        }
        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("backoffice.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(getDefaultLanguage());
        return $language;
    }

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

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

        $language = $languagesData->findByLocale(getActiveLanguage());
        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;
    }
}
