<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Blocks extends JsonResource
{
    /**
     * Accesses constructor.
     * @param $resource
     */
    public function __construct($resource)
    {
        $this->resource = $resource;
    }

    public function toArray($request)
    {
        if ($this->backgroundImage) {
            $backgroundImage = getImage($this->backgroundImage, '1920x1080');
        } else {
            $backgroundImage = (object)[
                'url' => null,
                'alt' => null,
            ];
        }

        $out = [
            'id'                        => (int)$this->id,
            'title'                     => strip_tags($this->block_title ?? $this->title, '<i><sup>'),
            'slug'                      => $this->lang_slug,
            'type'                      => $this->type,
            'description'               => $this->description,
            'background'                => $backgroundImage,
            'year'                      => $this->year,
            'chronologicEra'            => trans("frontend.century_type_{$this->chronologic_era}"),
            'blockSize'                 => $this->block_size,
            'images'                    => new ImageLegendsCollection($this->imageLegend, $this->type),
            'videos'                    => new VideosCollection($this->videos),
            'storytelling_type'         => $this->storytelling_type
        ];
        return $out;
    }
}
