<?php

namespace Lc\SovBundle\Component;

class MetaComponent
{
    protected FileComponent $fileComponent;
    protected StringComponent $stringComponent;

    public function __construct(FileComponent $fileComponent, StringComponent $stringComponent)
    {
        $this->fileComponent = $fileComponent;
        $this->stringComponent = $stringComponent;
    }

    public function getMetaTitle($entity, $title = null)
    {
        if($entity) {
            if(method_exists($entity, 'getMetaTitle') && $entity->getMetaTitle()) {
                return $entity->getMetaTitle() ;
            }
            elseif(!is_null($title)) {
                return $title;
            }
            elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
                return $entity->getTitle() ;
            }
        }

        return '' ;
    }

    public function getMetaDescription($entity)
    {
        if($entity) {
            if(method_exists($entity, 'getMetaDescription') && $entity->getMetaDescription()) {
                return $entity->getMetaDescription() ;
            }
            elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
                return $this->formatDescription($entity->getDescription());
            }
        }

        return '' ;
    }

    public function getOpenGraphTitle($entity, $title = null)
    {
        if($entity) {
            if(method_exists($entity, 'getOpenGraphTitle') && $entity->getOpenGraphTitle()) {
                return $entity->getOpenGraphTitle() ;
            }
            elseif(!is_null($title)) {
                return $title;
            }
            elseif(method_exists($entity, 'getTitle') && $entity->getTitle()) {
                return $entity->getTitle() ;
            }
        }

        return '' ;
    }

    public function getOpenGraphDescription($entity)
    {
        if($entity) {
            if(method_exists($entity, 'getOpenGraphDescription') && $entity->getOpenGraphDescription()) {
                return $entity->getOpenGraphDescription() ;
            }
            elseif(method_exists($entity, 'getDescription') && $entity->getDescription()) {
                return $this->formatDescription($entity->getDescription());
            }
        }

        return '' ;
    }

    public function getOpenGraphImage($entity)
    {
        if($entity) {
            if(method_exists($entity, 'getOpenGraphImage') && $entity->getOpenGraphImage()) {
                return $entity->getOpenGraphImage() ;
            }
            elseif(method_exists($entity, 'getImage') && $entity->getImage()) {
                return $entity->getImage() ;
            }
        }

        return '' ;
    }

    public function getOpenGraphImageUrl($entity)
    {
        $image = $this->getOpenGraphImage($entity);
        if($image && $image->getPath() && strlen($image->getPath()) > 0) {
            return $this->fileComponent->getAssetUrl($image->getPath());
        }

        return '';
    }

    public function formatDescription($description)
    {
        $description = trim($description);
        $description = $this->stringComponent->limitText($description, 50);
        $description = str_replace("\r\n","",$description);

        return $description;
    }
}
