<?php
namespace App\Domain\Feeds\Service;

use App\Domain\Feeds\Models\Feed;
use App\Domain\Feeds\Enums\CollectionMediaEnum;

class FeedMediaTransform
{
    public $feed;
    public $type;
    public $typeUrl = 'web';
    public $collection_name;

    public function __construct(Feed $feed)
    {
       $this->feed = $feed;
       $this->type = $feed->type;
    }

    public function getFullPath()
    {
         $this->typeUrl = 'disk';
         return $this;
    }

    public function paid()
    {
        return $this->addCollectionMediaName(CollectionMediaEnum::PAID());
    }

    public function default()
    {
        return $this->addCollectionMediaName(CollectionMediaEnum::COMMON());
    }

    public function addCollectionMediaName($collection_name)
    {
        $this->collection_name = $collection_name;
        return $this;
    }

    public function spot()
    {
        if($this->type === 'images'){
            return $this->images();
        }

        if($this->type === 'videos'){
            return $this->videos();
        }

        if($this->type === 'musics'){
            return $this->musics();
        }

    }

    public function videos()
    {
        $collection = [];
        $medias = $this->feed->getMedia($this->collection_name);
        $medias->each(function ($item) use (&$collection) {
            $collection[] = [
                'url' => $this->typeUrl === 'web' ? $item->getFullUrl() : $item->getPath(),
                'id' => $item->id,
            ];
        });
        return [
            'preview' => $this->getPreview(),
            'medias' => $collection,
        ];
    }
    public function images()
    {

        $collection = [];
        $medias = $this->feed->getMedia($this->collection_name);
        $medias->each(function ($item) use (&$collection) {
            $collection[] = [
                'url' => $this->typeUrl === 'web' ? $item->getFullUrl() : $item->getPath(),
                'id' => $item->id,
            ];
        });
        return [
            'preview' => $this->getPreview(),
            'medias' => $collection,
        ];
    }

    public function musics()
    {
        $collection = [];
        $medias = $this->feed->getMedia($this->collection_name);

        $medias->each(function ($item) use (&$collection) {
            $collection[] = [
                'url' => $this->typeUrl === 'web' ? $item->getFullUrl() : $item->getPath(),
                'name' => $item->name,
                'playing' => false,
                'liked' => $item->likes_count ?? false,
                'time' => $item->getCustomProperty('time'),
                'id' => $item->id,
            ];
        });
        return [
            'preview' => $this->getPreview(),
            'medias' => $collection,
        ];
    }

    public function getPreview($type = 0)
    {
        if($this->type === 'images'){
            $img_preview = $this->feed->getMedia($this->collection_name)->first();
            if($img_preview){
                if($type){
                    return $img_preview;
                }
                return $img_preview->getFullUrl();
            }
        }

        $preview = $this->feed->getMedia(CollectionMediaEnum::PREVIEW());
        if($type){
            return $preview->count() ? $preview->first() : null;
        }

        return $preview->count() ? $preview->first()->getFullUrl() : null;
    }

    public function getPreviewObject()
    {
        $preview = $this->getPreview(1);
        if($preview){
            return [ 'id'=> $preview->id, 'url' => $preview->getFullUrl() ];
        }
        return null;
    }
}
