<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Jenssegers\Date\Date;

class Events extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */

    protected $isDetail = false;

    public function __construct($resource, $isDetail = false)
    {
        $this->resource = $resource;
        $this->isDetail = $isDetail;
    }

    public function toArray($request)
    {
        $activeLang = getActiveLanguage();
        $thumbnail = $this->image ? getImage($this->image, '334x188') : null;
        $metaImage = $thumbnail && property_exists($thumbnail, 'url') ? $thumbnail->url : "";
        $langUri = $this->lang_uri;
        $metaData = [
            (object)[
                "name" => "og:title",
                "content" =>  $this->title
            ],
            (object)[
                "name" => "og:description",
                "content" => $this->description
            ],
            (object)[
                "name" => "og:image",
                "content" => $metaImage
            ],
            (object) [
                "name" => "og:url",
                "content" => config('app.frontend_url') . $langUri
              ],
              (object) [
                "name" => "twitter:title",
                "content" => $this->title
              ],
              (object) [
                "name" => "twitter:description",
                "content" => $this->description
              ],
              (object) [
                "name" => "twitter:image",
                "content" => $metaImage
              ],
              (object) [
                "name" => "twitter:card",
                "content" => $metaImage
              ]
        ];
        $date = $this->_getFormatedDate($this->date);
        $url = $activeLang === 'en' ? $this->lang_uri : '/' . $activeLang . $this->lang_uri;
        $backUrl = substr($url, 0, strrpos($url, '/'));
        $out = [
            'id'          => (int)$this->id,
            'title'       => $this->title,
            'slug'        => $this->lang_slug,
            'language_id' => $this->language_id,
            'excerpt'     => $this->excerpt,
            'thumbnail'   => $thumbnail,
            'date'        => $date,
            'url'         => $url,
            'backUrl'     => $backUrl,
            'event_date'  => $date->extended,
            'facebook'    => $this->facebook,
            'twitter'     => $this->twitter,
            'whatsapp'    => $this->whatsapp,
        ];

        if ($this->isDetail) {
            // SEO IMAGE
            $routes = $this->getRouteLanguages();
            $metaImage = $this->metaImage ? getImage($this->metaImage, '1200x630') : null;
            // Main Image
            $image = $this->image ? getImage($this->image, '690x390') : null;
            if ($this->location) {
                $lng = $this->location->getLng();
                $lat = $this->location->getLat();
                if (!$lng || !$lat) {
                    $location = null;
                } else {
                    $location = [
                        'lng'  => $this->location->getLng(),
                        'lat'  => $this->location->getLat(),
                        'zoom' => $this->location_zoom,
                    ];
                }
            } else {
                $location = null;
            }

            $out = array_merge($out, [
                'image'       => $image,
                'description' => $this->description,
                'meta'        => (object)[
                    'title'       => $this->meta_title,
                    'description' => $this->meta_description,
                    'image'       => $metaImage
                ],
                'contacts'    => $this->contacts,
                'images'      => ['items' => new ImageLegendsCollection($this->images)],
                'location'    => $location,
                'slugs'       => $routes,
                'meta'        => $metaData,
            ]);
        }

        return $out;
    }

    private function _getFormatedDate($date = null)
    {
        $response = (object)[
            'day'           => null,
            'day_full'      => null,
            'day_month'     => null,
            'month_abbr'    => null,
            'day_full_time' => null,
            'year'          => null,
            'extended'      => null
        ];

        if (!$date) {
            return $response;
        }

        // Format dates
        $activeLanguage = getActiveLanguage();
        $carbonDate = new Date($date);
        $carbonDate->setLocale($activeLanguage);
        $day = $carbonDate->format('d');
        $dayFull = $carbonDate->format('l');
        $dayMonth = $carbonDate->format('d F');
        $monthAbbr = ucwords($carbonDate->format('M'));
        $dayFullTime = ucwords($carbonDate->format('l, H:m'));
        $year = $carbonDate->format('Y');
        $extended = $carbonDate->format('Y-m-d');

        // Update Response Object
        $response->day = $day;
        $response->day_full = $dayFull;
        $response->year = $year;
        $response->day_month = $dayMonth;
        $response->month_abbr = $monthAbbr;
        $response->day_full_time = $dayFullTime;
        $response->extended = $extended;

        return $response;
    }

    private function getRouteLanguages()
    {
        $langs = [];
        $wo = $this->websites[0];
        foreach ($wo->languages as $lang) {
            $langs[$lang->wo_language_id] = $lang->lang_slug;
        }
        return $langs;
    }
}
