<?php

namespace Modules\Linker\Traits;

use Illuminate\Database\Eloquent\Builder;
use Modules\Linker\Models\LinkerRelation;

trait HasLinker
{

    /**
     * @var mixed
     */
    public $linker_id;

    /**
     * @var mixed
     */
    public $linker_params;

    /**
     * @var string|null
     */
    public ?string $linker_mode;

    /**
     * 处理对应模型中关于链接数据的处理
     * 模型请 use HasLinker
     */
    public static function bootHasLinker()
    {
        self::saving(function ($model) {
            unset($model->attributes['linker_id']);
            unset($model->attributes['linker_params']);
            unset($model->attributes['linker_mode']);
        });
        self::saved(function ($model) {
            $model->saveWithUrl();
        });
        // 这里要注意，如果模型使用了缓存 (use GeneaLabs\LaravelModelCaching\Traits\Cachable)
        // 在表单实例化模型的时候  要使用 (new Model())->disableModelCaching()
        self::retrieved(function ($model) {
            if ($relation = $model->hasLinker()) {
                $model->attributes['linker_id']     = $relation->linker_id;
                $model->attributes['linker_params'] = $relation->params;
                $model->attributes['linker_mode']   = $relation->mode;
            }
        });
    }

    /**
     * Notes   : 处理当前模型多余的三个字段
     * @Date   : 2021/6/22 2:10 下午
     * @Author : < Jason.C >
     * @param $value
     */
    public function setLinkerIdAttribute($value)
    {
        $this->linker_id = $value;
    }

    public function setLinkerParamsAttribute($value)
    {
        $this->linker_params = $value;
    }

    public function setLinkerModeAttribute($value)
    {
        $this->linker_mode = $value;
    }

    /**
     * 外链入库
     * 在BOOT中触发
     */
    public function saveWithUrl(): void
    {
        if ($this->linker_id) {
            LinkerRelation::updateOrCreate([
                'model_type' => get_class($this),
                'model_id'   => $this->getKey(),
            ], [
                'linker_id' => $this->linker_id,
                'mode'      => $this->linker_mode,
                'params'    => $this->linker_params,
            ]);
        } elseif (!$this->linker_id && $this->hasLinker()) {
            $this->hasLinker()->delete();
        }
    }

    /**
     * 返回当前模型的链接配置
     * @return null|LinkerRelation
     */
    public function hasLinker(): ?LinkerRelation
    {
        return LinkerRelation::whereHasMorph(
            'model',
            [get_class($this)],
            function (Builder $query) {
                $query->where($this->getKeyName(), $this->getKey());
            }
        )->first();
    }

    /**
     * Notes   : 返回该模型的链接数组
     * @Date   : 2021/6/22 2:08 下午
     * @Author : < Jason.C >
     * @return array|null
     */
    public function getLinkerAttribute(): ?array
    {
        $relation = $this->hasLinker();
        if ($relation && $relation->linker && $relation->linker->status) {
            return $relation->linker->getResource($relation);
        }

        return null;
    }

}