<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Entries extends JsonResource
{
    public function __construct($resource)
    {
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        // Default data that we need to return on every block
        $out = [
            'id'       => (int)$this->id,
            'title'    => $this->frontend_title ? $this->frontend_title : $this->title,
            'pageType' => $this->page_type,
        ];

        // Specific block data per type
        $out = array_merge($out, $this->_getEntryData());

        return $out;
    }

    private function _getEntryData()
    {
        $out = [];
        switch ($this->page_type) {
            case 'aboutUs':
                $out = $this->_getAboutUsData();
                break;
            case 'technical-record':
                $out = $this->_getTechnicalRecordData();
                break;
            case 'storyTelling':
                $out = $this->_getBlocksData();
                break;
            case 'multimedia':
                $out = $this->_getMultimediaData();
                break;
            case 'termos-e-condicoes':
                $out = $this->_getContent();
                break;
            case 'privacy-policy':
                $out = $this->_getContent();
                break;
            case 'cookies':
                $out = $this->_getContent();
                break;
            case 'gastronomic-week':
                $out = $this->_getGastronomicWeekData();
                break;
        }

        return $out;
    }

    private function _getAboutUsData()
    {
        if ($this->image) {
            $image = getImage($this->image, 'resizeH780');
        } else {
            $image = (object)[];
            $image->url = null;
            $image->alt = null;
        }
        $out = [
            'title'          => $this->frontend_title,
            'image'          => $image,
            'content'        => $this->content,
            'specifications' => $this->specifications,
            'partners'       => new PartnersCollection($this->partners)
        ];

        return $out;
    }

    private function _getGastronomicWeekData()
    {
        if ($this->image) {
            $image = getImage($this->image, 'resizeH780');
        } else {
            $image = (object)[];
            $image->url = null;
            $image->alt = null;
        }
        if ($this->bottomImage) {
            $bottomImage = getImage($this->bottomImage, 'resizeH780');
        } else {
            $bottomImage = (object)[];
            $bottomImage->url = null;
            $bottomImage->alt = null;
        }
        $out = [
            'title'          => $this->frontend_title,
            'image'          => $image,
            'bottomImage'    => $bottomImage,
            'content'        => $this->content
        ];

        return $out;
    }

    private function _getTechnicalRecordData()
    {
        if ($this->image) {
            $image = getImage($this->image, 'resizeH780');
        } else {
            $image = (object)[];
            $image->url = null;
            $image->alt = null;
        }
        $out = [
            'title'   => $this->frontend_title,
            'image'   => $image,
            'content' => $this->content
        ];

        return $out;
    }

    private function _getBlocksData()
    {
        // TODO: Optimization --> $this->blocks->loadMissing([''])
        $out = [
            'description' => $this->description ? $this->description : "",
            'blocks'      => new BlocksCollection($this->blocks),
        ];

        return $out;
    }

    private function _getMultimediaData()
    {
        $out = [
            'videos' => new MediaVideosCollection($this->mediaVideos),
        ];

        return $out;
    }

    private function _getContent()
    {
        $out = [
            "content" => $this->content
        ];

        return $out;
    }
}
