<?php

class GenericBlock extends ContentTypeModel implements CardableModel
{
    use LanguageSupportTrait;

    protected $hidden = array('languages', "websites");
    protected $with = array("websites", "languages");

    public function image()
    {
        return $this->belongsTo("MediaItem");
    }

    public function svg()
    {
        return $this->belongsTo("MediaItem");
    }

    public function block() {
        return $this->belongsToMany(Block::class, 'block_cards')->withPivot('position');
    }

    public function getCardFields()
    {
        if ($this->type != "reference") {
            $fieldsObject = new stdClass();
            $fieldsObject->is_card = true;
            $fieldsObject->subject = $this->subject;
            $fieldsObject->title = $this->title;
            $fieldsObject->content = $this->content;
            $fieldsObject->excerpt = $this->excerpt;
            $fieldsObject->image = $this->image;
            $fieldsObject->image_alt = $this->title;
            $fieldsObject->cta =  $this->cta;
            $fieldsObject->cta2 = $this->cta2;
            $fieldsObject->cta_label = $this->cta_label;
            $fieldsObject->cta2_label = $this->cta2_label;
            return $fieldsObject;
        }
        if (!$this->related('reference_objects')) {
            return null;
        }
        $reference = $this->related('reference_objects')->first();
        if (!$reference) {
            return null;
        }
        $wo = $reference->websites()->first();
        if ($reference && $reference->status == 'published' && $wo->lang_status == 'published') {
            return $reference->getCardFields();
        }
        return null;
    }
}
