<?php namespace Eae\Transformation\Transformer;

use League\Fractal;
use Lang;
use Block;

class BlockTransformer extends Fractal\TransformerAbstract
{

    protected $availableIncludes = [
        'squareGrid', 'liveshowList', 'gallery'
    ];

    public function __construct()
    {

    }

    public function transform(Block $block)
    {
//        echo '<pre>';
//        print_r($block->toArray());
//        print_r($block->highResolutionImage->toArray());

        // High Resolution Image
        $highResolutionImage = $block->highResolutionImage;

        // Square Image
        $squareImage = $block->squareImage;
        $squareImageAux = $block->squareImageAux;

        // CTA
        $cta = [];

        $ctaBlock = link_transform($block->cta, $block->cta_icon);
        $ctaBlockSecond = link_transform($block->cta_second, $block->cta_icon_second);
        if ($ctaBlock) $cta[] = $ctaBlock;
        if ($ctaBlockSecond) $cta[] = $ctaBlockSecond;


        $out = [
            'id'                  => (int)$block->id,
            'title'               => $block->block_title,
            'subtitle'            => $block->subtitle,
            'text'                => $block->text,
            'clip_text'           => $block->clip_text,
            'clip_text_second'    => $block->clip_text_second,
            'type'                => $block->type,
            'size'                => $block->size,
            'overlap'             => $block->overlap,
            'highResolutionImage' => isset($highResolutionImage->filename) ? getMediaUrl($highResolutionImage->filename) : '',
            'squareImage'         => isset($squareImage->filename) ? getMediaUrl($squareImage->filename) : '',
            'squareImageAux'      => isset($squareImageAux->filename) ? getMediaUrl($squareImageAux->filename) : '',
            'cta'                 => $cta
        ];

//        print_r($out);
//        dd();
        return $out;
    }

    public function includeSquareGrid(Block $block)
    {
        $squareGrid = $block->squareGrid;

        return $this->collection($squareGrid, new SquareGridTransformer);

    }

    public function includeLiveshowList(Block $block)
    {
        $liveshowList = $block->liveshowList;

        return $this->collection($liveshowList, new LiveshowItemTransformer);

    }

    public function includeGallery(Block $block)
    {
        $gallery = $block->gallery;

        return $this->collection($gallery, new GalleryTransformer);

    }
}



