<?php namespace Eae\Transformation\Transformer;

use League\Fractal;
use Lang;
use Block;
use Carbon\Carbon;
use Eae\Support\WaveGenerator;

class BlockTransformer extends Fractal\TransformerAbstract
{
    protected $availableIncludes = [
        'gallery',
        'quizzQuestions',
        'listItems'
    ];

    public function __construct($year = NULL)
    {
        $this->year = NULL;
        if ($year) {
            $this->year = $year;
        }
    }

    public function transform(Block $block)
    {
        $out = [];
        $out = $this->_getBlock($block);

        return $out;
    }

    public function includeGallery(Block $block)
    {
        if ($block->type !== 'blockMainArticle') {
            return NULL;
        }

        return $this->collection($block->blockGallery, new GalleryTransformer());
    }

    public function includeQuizzQuestions(Block $block)
    {
        if ($block->type != 'blockQuizz') {
            return NULL;
        }

        return $this->collection($block->quizzQuestions, new QuizzQuestionTransformer());
    }

    public function includeListItems(Block $block)
    {
        if ($block->type !== 'blockListItems') {
            return NULL;
        }

        return $this->collection($block->listItems, new ListItemTransformer());
    }

    private function _getBlock(Block $block)
    {
        $out = [];
        switch ($block->type) {
            case 'blockMainArticle':
                $out = $this->_getMainArticle($block);
                break;
            case 'blockAudio':
                $out = $this->_getBlockAudio($block);
                break;
            case 'blockQuizz':
                $out = $this->_getBlockQuizz($block);
                break;
            case 'blockTitleDescription':
                $out = $this->_getBlockTitleDescription($block);
                break;
            case 'blockListItems':
                $out = $this->_getBlockListItems($block);
                break;
            case 'blockTitleDescriptionSubtitleAudio':
                $out = $this->_getBlockTitleDescriptionSubtitleAudio($block);
                break;
        }

        return $out;
    }

    private function _getMainArticle(Block $block)
    {
        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'title'       => nl2br($block->block_title),
            'description' => nl2br($block->block_description),
            'audio'       => $block->block_audio,
            'peaks'       => $this->_getPeaks($block->block_audio),
            'year'        => $this->year ? Carbon::createFromFormat('Y-m-d', $this->year)->format('Y') : ''
        ];

        return $out;
    }

    private function _getBlockAudio(Block $block)
    {
        $out = [
            'id'    => (int)$block->id,
            'type'  => $block->type,
            'title' => nl2br($block->block_title),
            'audio' => $block->block_audio,
            'peaks' => $this->_getPeaks($block->block_audio),
        ];

        return $out;
    }

    private function _getBlockQuizz(Block $block)
    {
        $out = [
            'id'   => (int)$block->id,
            'type' => $block->type,
        ];

        return $out;
    }

    private function _getBlockTitleDescription(Block $block)
    {
        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'title'       => nl2br($block->block_title),
            'description' => nl2br($block->block_description)
        ];

        return $out;
    }

    private function _getBlockTitleDescriptionSubtitleAudio(Block $block)
    {
        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'title'       => nl2br($block->block_title),
            'description' => nl2br($block->block_description),
            'subtitle'    => nl2br($block->block_subtitle),
            'audio'       => $block->block_audio,
            'peaks' => $this->_getPeaks($block->block_audio),
        ];

        return $out;
    }


    private function _getBlockListItems(Block $block)
    {
        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'title'       => nl2br($block->block_title),
            'subtitle'    => nl2br($block->block_subtitle),
            'description' => nl2br($block->block_description)
        ];

        return $out;
    }

    private function _getBlockImageAudio(Block $block)
    {
        $image = $block->blockImage;

        $out = [
            'id'    => (int)$block->id,
            'type'  => $block->type,
            'image' => isset($image->filename) ? [
                'url' => getMediaUrl($image->filename, '1168x546'),
                'alt' => isset($image->alt) ? $image->alt : ''
            ] : '',
            'audio' => $block->block_audio
        ];

        return $out;
    }

    private function _getBlockTitleAudio(Block $block)
    {
        $out = [
            'id'    => (int)$block->id,
            'type'  => $block->type,
            'title' => nl2br($block->block_title),
            'audio' => $block->block_audio
        ];

        return $out;
    }

    private function _getBlockDescription(Block $block)
    {
        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'description' => nl2br($block->block_description)
        ];

        return $out;
    }

    private function _getBlockTitleDescriptionImageAudio(Block $block)
    {
        $image = $block->blockImage;

        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'title'       => nl2br($block->block_title),
            'description' => nl2br($block->block_description),
            'image'       => isset($image->filename) ? [
                'url' => getMediaUrl($image->filename, '556x296'),
                'alt' => isset($image->alt) ? $image->alt : ''
            ] : '',
            'audio'       => $block->block_audio
        ];

        return $out;
    }

    private function _getBlockDescriptionImage(Block $block)
    {
        $image = $block->blockImage;

        $out = [
            'id'          => (int)$block->id,
            'type'        => $block->type,
            'description' => nl2br($block->block_description),
            'image'       => isset($image->filename) ? [
                'url' => getMediaUrl($image->filename, '556x356'),
                'alt' => isset($image->alt) ? $image->alt : ''
            ] : ''
        ];

        return $out;
    }

    private function _getPeaks($audioFile)
    {
        $waveGenerator = new WaveGenerator($audioFile);
        $peaks = $waveGenerator->getPeaksData();

        return $peaks;
    }
}
