<?php

namespace Hilco\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

trait HasTranslations {
    protected $queuedTranslations = [];
    /**
     * @return MorphToMany
     */
    public function translations() {
        return $this->morphMany(WebTranslation::class, 'translatable');
    }

    public static function bootHasTranslations() {
        static::created(function (Model $translatableModel) {
            foreach ($translatableModel->queuedTranslations as $translationInfo) {
                $translatableModel->setTranslation($translationInfo['language'], $translationInfo['key'], $translationInfo['value']);
            }
            $translatableModel->queuedTranslations = [];
        });
    }

    public function getTranslation($key, $language = false, $default = false) {
        if ($language === false) $language = b2b()->activeLanguage();
        if ($default === false) $default = \Illuminate\Support\Arr::get($this->attributes, $key, '');
        $translation = $this->translations->where('language', $language)->where('key', $key)->first();
        return \Illuminate\Support\Arr::get($translation, "value", $default);
    }

    public function getNameAttribute() {
        return $this->getTranslatedName();
    }

    public function getTranslatedName($language = false, $default = false) {
        return $this->getTranslation('name', $language, $default);
    }
    public function getTranslatedDetail($language = false, $default = false){
        return $this->getTranslation('detail', $language, $default);
    }

    public function setTranslation($language, $key, $value) {
        if (!$this->exists) {
            $this->queuedTranslations[] = [
                'language' => $language,
                'key' => $key,
                'value' => $value,
            ];
            return;
        }

        $this->translations()->updateOrCreate(['language' => $language, 'key' => $key],['value' => $value]);
    }

    public function setTranslatedName($language, $name) {
        $this->setTranslation($language, 'name', $name);
    }
    public function setTranslatedDetail($language, $detail){
        $this->setTranslation($language, 'detail', $detail);
    }

    public function setEnNameAttribute($name) {
        $this->setTranslatedName('en', $name);
    }

    public function setEnDetailAttribute($name){
        $this->setTranslatedDetail('en', $detail);
    }

    public function setDeNameAttribute($name) {
        $this->setTranslatedName('de', $name);
    }
    public function setDeDetailAttribute($detail){
        $this->setTranslatedDetail('de', $detail);
    }

    public function setFrNameAttribute($name) {
        $this->setTranslatedName('fr', $name);
    }
    public function setFrDetailAttribute($detail){
        $this->setTranslatedDetail('fr', $detail);
    }

    public function getEnNameAttribute() {
        return $this->getTranslatedName('en', '');
    }

    public function getEnDetailAttribute(){
        return $this->getTranslatedDetail('en', '');
    }

    public function getDeNameAttribute() {
        return $this->getTranslatedName('de', '');
    }
    public function getDeDetailAttribute(){
        return $this->getTranslatedDetail('de', '');
    }

    public function getFrNameAttribute() {
        return $this->getTranslatedName('fr', '');
    }

    public function getFrDetailAttribute(){
        return $this->getTranslatedDetail('fr', '');
    }

    public function getCombinedNameAttribute() {
        return $this->de_name . ' (' . $this->en_name . ')';
    }

    public function getTranslationArray($key) {
        return $this->translations->where('key', $key)->pluck('value', 'language')->toArray();
    }

    public function getNameTranslationsAttribute() {
        return $this->getTranslationArray('name');
    }
}