<?php

namespace App\Http\Resources\V1;

use Illuminate\Http\Resources\Json\JsonResource;
use Spatie\CalendarLinks\Link;
use Throwable;
use Illuminate\Support\Facades\Log;

class ExhibitionResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {

        $location = new LocationResource($this->whenLoaded('location'));
        $video    = new VideoResource($this->whenLoaded('video'));
        $gif      = new ImageResource($this->whenLoaded('gifImage'));

        $out = [
            'id'               => $this->id,
            'name'             => $this->title,
            'description'      => $this->description === null ? '' : $this->description,
            'pressReleaselink' => $this->press_release_link,
            'image_label'      => $this->image_label,
            'image'            => new ImageResource($this->whenLoaded('image')),
            'gif_image'        => ($video->resource === null ?? $gif->resource) ? $gif : null,
            'video'            => $video->resource ? $video : null,
            'theme'            => $this->theme,
            'bgcolor'          => $this->bgcolor,
            'curator'          => $this->curator,
            'dateStatus'       => $this->date_status,
            'dateFrom'         => $this->date_from->toIso8601String(),
            'dateTo'           => $this->date_to->toIso8601String(),
            'highlight'        => $this->highlight,
            'sponsored'        => $this->sponsored,
            'location'         => $location,
            'artists'          => ArtistResource::collection($this->whenLoaded('artists')),
            'collections'      => CollectionResource::collection($this->whenLoaded('collections')),
            'tags'             => TagResource::collection($this->whenLoaded('tags')),
            'roomSheet'        => [
                'title'       => $this->room_sheet_title,
                'subtitle'    => $this->room_sheet_subtitle,
                'author'      => $this->room_sheet_author,
                'description' => $this->room_sheet_description,
                'footnotes'   => $this->room_sheet_footnotes,
                'image'       => new ImageResource($this->whenLoaded('roomSheetImage')),
                'imageLabel'  => $this->room_sheet_image_label,
            ],
            //'addToCalendar'   => $this->resource->relationLoaded('location') ? $this->generateAddToCalendarInfo() : [],
            'gallery'         => MediaGalleryItemResource::collection($this->whenLoaded('mediaGalleryItems')),
            'share_url'       => $this->share_url,

        ];

        return $out;
    }



    /**
     * Generates an array with the tools for adding an exhibition to calendar
     *
     * @return array
     */
    private function generateAddToCalendarInfo(): array
    {


        $from            = $this->date_from; //->shiftTimezone('Europe/Lisbon');
        $to              = $this->date_to; //->shiftTimezone('Europe/Lisbon');
        $locationString  = !empty($this->resource->location->address) ? strip_tags(nl2br($this->resource->location->address)) : "";
        // There can be exceptions thrown in the links generator
        // this will prevent the hole response to be an error and will just return an empty structure
        try {
            $link            = Link::create($this->title, $from, $to)
                ->description($this->description)
                ->address($locationString);


            $base64IcsString = str_replace('data:text/calendar;charset=utf8;base64,', '', $link->ics());

            return [
                'google.com'  => $link->google(),
                'outlook.com' => $link->webOutlook(),
                'office.com'  => $link->webOffice(),
                'ics'         => base64_decode($base64IcsString),
            ];
        } catch (Throwable $e) {
            Log::error($e);
            return [];
        }
    }
}
