<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\Classification;

class Epigraphs extends JsonResource
{
    /**
     * Areas constructor.
     * @param $resource
     */

    protected $isDetail = false;

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

    public function toArray($request)
    {
        if (!$this->isDetail) {
            $out = [
                'id'    => (int)$this->id,
                'slug'  => $this->lang_slug,
                'title' => $this->title,
            ];
        } else {
            $locationSlug = null;
            foreach ($this->properties as $property) {
                if ($property->pivot->found_local_id === 1) {
                    $locationSlug = $this->getWebsiteLanguagesSlugs($property->websites[0]->languages);
                    break;
                }
            }
            $locationFound = (object)[
                "title" => $this->location_found,
                "slug"  => $locationSlug
            ];
            $routes = $this->getRouteLanguages();
            $theme = $this->themes->first();
            if ($this->images->count() > 0) {
                $firstImg = $this->images->first();
                if ($firstImg) {
                    $thisImage = getImage($firstImg->image, '1200x634');
                    $metaImage = property_exists($thisImage, 'url') ? $thisImage->url : "";
                } else {
                    $metaImage = "";
                }
            } else {
                $metaImage = "";
            }
            $langUri = str_replace("imoveis", "epigrafe", $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
                  ]
            ];
            $out = [
                'id'                     => (int)$this->id,
                'externalReference'      => $this->external_reference,
                'slug'                   => $this->lang_slug,
                'title'                  => $this->epigraph_title,
                'theme'                  => $theme ? $theme->title : "",
                'isVisitable'            => $this->classifications->hasItem('external_id', Classification::TYPE_VISITABLE),
                'isNotVisitable'         => $this->classifications->hasItem('external_id', Classification::TYPE_NOT_VISITABLE),
                'isVisitableNotVisible'  => $this->classifications->hasItem('external_id', Classification::TYPE_VISITABLE_NOT_VISIBLE),
                'description'           => $this->description,
                'readingSuggestion'      => $this->reading_suggestion,
                'plunderLocation'        => $this->plunder_location,
                'locationFound'          => $locationFound,
                'presentLocation'        => $this->present_location,
                'inscriptionType'        => $this->inscriptionType ? $this->inscriptionType->title : "",
                'inscriptionText'        => $this->inscription_text,
                'inscriptionTranslation' => $this->inscription_translation,
                'images'                 => ['items' => new PropertyImagesCollection($this->images, "resizeH400")],
                'idiom'                  => $this->idiom ? $this->idiom->title : null,
                'technique'              => $this->technique ? $this->technique->title : null,
                'chronologies'           => new ChronologiesCollection($this->chronologies, true),
                'audiovisuals'           => new AudioVisualsCollection($this->audioVisuals),
                'local'                  => $this->location ? $this->location->title : "",
                'slugs'                  => $routes,
                'meta'                   => $metaData,
                'firstImage'             => $metaImage,
            ];
        }
        return $out;
    }

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

    private function getWebsiteLanguagesSlugs($langs)
    {
        $out = [];
        foreach ($langs as $lang) {
            $out[$lang->wo_language_id] = $lang->lang_slug;
        }
        return $out;
    }
}
