<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;

/**
 * Class Setting
 *
 * @property int $id
 * @property string|null $identifier
 * @property string $slug
 * @property int $referenz
 * @property string|null $action
 * @property string|null $object
 * @property string|null $content
 * @property int $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @package App\Models
 * @property string|null $full_text
 * @property string|null $text
 * @property string|null $type
 * @method static \Illuminate\Database\Eloquent\Builder|Setting findSimilarSlugs(string $attribute, array $config, string $slug)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Setting newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Setting query()
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereAction($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereFullText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereIdentifier($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereObject($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereReferenz($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereSlug($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereText($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Setting whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class Setting extends Model
{
    use Sluggable;

    protected $table = 'settings';

	protected $casts = [
		'referenz' => 'int',
		'status' => 'int',
        'object' => 'array'
	];

	protected $fillable = [
		'identifier',
		'slug',
		'referenz',
		'action',
		'object',
		'full_text',
        'text',
        'status',
        'type'
	];

    protected static $types = [
        'object' => 'Object',
        'full_text' => 'Full Text',
        'text' => 'Text',
    ];

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'name'
            ]
        ];
    }
    public static function getContentBySlug($slug){
        $content = self::whereSlug(trim($slug))->first();
        if($content){
            switch ($content->type){
                case 'object':
                    return $content->object;
                    break;
                case 'full_text':
                    return $content->full_text;
                    break;
                case 'text':
                    return $content->text;
                    break;
            }
        }
        return false;
    }

    public static function setContentBySlug($slug, $value, $type = "full_text"){

        $content = self::whereSlug(trim($slug))->first();
        if(!$content) {
            $content = self::create([
                'slug' => $slug,
                'type' => $type,
            ]);
        }
        $content->type = $type;
        switch ($content->type){
            case 'object':
                $content->object = $value;
                break;
            case 'full_text':
                $content->full_text = $value;
                break;
            case 'text':
                $content->text = $value;
                break;
        }

        $content->save();
        return $content;
    }

}
