<?php

class Block extends ContentTypeModel implements CardableModel
{
    use LanguageSupportTrait;

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

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

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

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

    public function projects()
    {
        return $this->belongsToMany("Project");
    }

    public function stories()
    {
        return $this->belongsToMany("Story");
    }

    public function blockTimelineItems()
    {
        return $this->hasMany("BlockTimelineItem");
    }

    public function blockQuickFacts()
    {
        return $this->hasMany("BlockQuickFact");
    }

    public function testimonial()
    {
        return $this->belongsTo("Testimonial");
    }

    public function testimonials()
    {
        return $this->belongsToMany("Testimonial", "block_testimonials", "block_id", "testimonial_id");
    }

    public function publications()
    {
        return $this->belongsToMany("Publication", "block_publications", "block_id", "publication_id");
    }

    public function clippingNews()
    {
        return $this->belongsToMany("News", "news_blocks", "block_id", "news_id");
    }

    public function teamMemberGroup()
    {
        return $this->belongsTo("TeamMemberGroup");
    }

    public function cards()
    {
        return $this->belongsToMany("GenericBlock", "block_cards", "block_id", "generic_block_id");
    }

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

    public function blockAgendaItems()
    {
        return $this->hasMany("BlockAgendaItem");
    }

    public function blockGalleryItems()
    {
        return $this->hasMany("BlockGalleryItem");
    }

    public function pardotForm()
    {
        return $this->belongsTo("PardotForm");
    }

    public function teamMemberGroups1()
    {
        return $this->belongsToMany("TeamMemberGroup", "block_team_group_1", "block_id", "team_member_group_id");
    }

    public function teamMemberGroups2()
    {
        return $this->belongsToMany("TeamMemberGroup", "block_team_group_2", "block_id", "team_member_group_id");
    }

    public function teamMemberGroups3()
    {
        return $this->belongsToMany("TeamMemberGroup", "block_team_group_3", "block_id", "team_member_group_id");
    }

    public function publication()
    {
        return $this->belongsTo("Publication");
    }

    public function blogCategory()
    {
        return $this->belongsTo("BlogCategory");
    }

    public function parentEntries() {
        return $this->belongsToMany(Entry::class, 'block_entry')->withPivot('position');
    }

    public function parentProjects() {
        return $this->belongsToMany(Project::class, 'project_blocks')->withPivot('position');
    }

    public function parentSolutions() {
        return $this->belongsToMany(Solution::class, 'solution_blocks')->withPivot('position');
    }

    public function parentBlogPosts() {
        return $this->belongsToMany(BlogPost::class, 'blog_post_blocks')->withPivot('position');
    }

    public function parentEventItems() {
        return $this->belongsToMany(EventItem::class, 'event_item_blocks')->withPivot('position');
    }

    public function parentNews() {
        return $this->belongsToMany(News::class, 'news_blocks')->withPivot('position');
    }

    public function parentPublications() {
        return $this->belongsToMany(Publication::class, 'publication_blocks')->withPivot('position');
    }

    public function parentSectors() {
        return $this->belongsToMany(Sector::class, 'sector_blocks')->withPivot('position');
    }

    public function parentCaseStudies() {
        return $this->belongsToMany(CaseStudy::class, 'case_study_blocks')->withPivot('position');
    }

    public function imageObjects()
    {
        return $this->hasMany(ImageObject::class);
    }

    public function getCardFields()
    {
        if ($this->type == 'featured-one-item-reference' || $this->type == 'more-info-block-reference') {
            $reference = $this->related('reference_objects')->first();
            if (!$reference) {
                return null;
            }
            $wo = $reference->websites()->first();

            if ($reference && $reference->status == 'published' && $wo->lang_status == 'published') {
                $cardFields =  $reference->getCardFields();
                $cardFields->excerpt = '<p>' . $cardFields->excerpt . '</p>';
                return $cardFields;
            }
            return null;
        }
        $fieldsObject = new stdClass();
        $fieldsObject->subject = $this->block_subtitle;
        $fieldsObject->title = $this->block_title;
        $fieldsObject->excerpt = $this->content;
        $fieldsObject->image = $this->image1;
        $fieldsObject->image_alt = $this->block_title;
        $fieldsObject->cta =  $this->cta1;
        $fieldsObject->cta2 = $this->cta2;

        return $fieldsObject;
    }

    public function getMixedMedia()
    {
        $reference = $this->related('reference_objects')->first();
        if (!empty($reference)) {
            $wo = $reference->websites()->first();

            if ($reference && $reference->status == 'published' && $wo->lang_status == 'published') {
                $cardFields =  $reference->getCardFields();
                $cardFields->excerpt = '<p>' . $cardFields->excerpt . '</p>';
                return $cardFields;
            }
        }

        $fieldsObject = new stdClass();
        $fieldsObject->subject = $this->block_subtitle;
        $fieldsObject->title = $this->block_title;
        $fieldsObject->excerpt = $this->content;
        $fieldsObject->image = $this->image1;
        $fieldsObject->image_alt = $this->block_title;
        $fieldsObject->cta =  $this->cta1;
        $fieldsObject->cta2 = $this->cta2;

        return $fieldsObject;
    }

    public function bgVideo()
    {
        return $this->belongsTo("MediaItem", "video_id");
    }

    public function images()
    {
        return $this->belongsToMany("MediaItem", "block_images", "block_id", "media_id");
    }

    public function mobileImages()
    {
        return $this->belongsToMany("MediaItem", "block_mobile_images", "block_id", "media_id");
    }

    public function speakers()
    {
        return $this->belongsToMany("Speaker");
    }

    public function linkGroups()
    {
        return $this->belongsToMany("LinkGroup");
    }

    public function hasLuumoBackground()
    {
        return $this->luumo && in_array($this->type, ['hero-marketing', 'video-content']);
    }
}
