<?php

namespace App\Http\Resources;

use App\Http\Controllers\Traits\TaxonomyHelper;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Controllers\Api\MapController;
use App\Models\Classification;
use App\Models\Epigraph;

class Properties extends JsonResource
{
    use TaxonomyHelper;
    /**
     * 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) {
            $routes = $this->getRouteLanguages();

            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", "imovel", $this->lang_uri);
            $metaData = [
                (object)[
                    "name" => "og:title",
                    "content" =>  $this->title
                ],
                (object)[
                    "name" => "og:description",
                    "content" => $this->popup_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->popup_description
                  ],
                  (object) [
                    "name" => "twitter:image",
                    "content" => $metaImage
                  ],
                  (object) [
                    "name" => "twitter:card",
                    "content" => $metaImage
                  ]
            ];
            if (!in_array($this->property_type_id, Epigraph::NOT_MUSEUM_IDS) && empty($this->description)) {
                $desc = $this->popup_description;
            } else {
                $desc = $this->description;
            }

            $parentThemes = $this->_buildHierarchy($this->themes);
            $themesResponse = [];
            foreach ($parentThemes as $key => $parent) {
                if (!is_null($parent->icon_id)) {
                    $theme = new Themes($parent);
                    $themesResponse[] = $theme;
                }
            }

            $parentTypologies = $this->_buildHierarchy($this->typologies);
            $typologiesResponse = [];
            foreach ($parentTypologies as $key => $parent) {
                $typology = new Typologies($parent);
                $typologiesResponse[] = $typology;
            }
            $out = [
                'id'                    => (int)$this->id,
                'externalReference'     => $this->external_reference,
                'title'                 => $this->frontend_title,
                'themes'                => $themesResponse,
                'typologies'            => $typologiesResponse,
                'slug'                  => $this->slug,
                'images'                => ['items' => new PropertyImagesCollection($this->images, "resizeH400")],
                '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),
                'isMuseum'              => is_null($this->propertyType) ? false : $this->propertyType->isMuseum(),
                'isArcheologicalSite'   => is_null($this->propertyType) ? false : $this->propertyType->isArcheologicalSite(),
                'isMusealizedSite'      => is_null($this->propertyType) ? false : $this->propertyType->isMusealizedSite(),
                'isDiscoveryPlace'      => is_null($this->propertyType) ? false : $this->propertyType->isDiscoveryPlace(),
                'isAccessable'          => $this->accesses->count() > 0,
                'epigraphs'             => new PropertyEpigraphsCollection($this->epigraphs),
                'chronologies'          => new ChronologiesCollection($this->chronologies, true),
                'address'               => $this->address ?? "",
                'readingSuggestion'     => $this->reading_suggestion,
                'excavation'            => $this->excavation,
                'description'           => $desc,
                'audiovisuals'          => new AudioVisualsCollection($this->audioVisuals),
                'plunderLocations'      => $this->plunder_locations,
                'location'              => [
                    'lng' => $this->geolocation->getLng(),
                    'lat' => $this->geolocation->getLat(),
                ],
                'popupDescripition'     => $this->popup_description,
                'meta'                  => $metaData,
                'slugs'                 => $routes,
                'firstImage'            => $metaImage,
                'local'                 => $this->location ? $this->location->title : "",
                'hasQrCode'             => $this->qrcode ? true : false,
            ];
        } else {
            if ($this->images->count() > 0) {
                $image = new PropertyImages($this->images->first(), '520x340');
            } else {
                $image = (object)[];
                $image->image = (object)[
                    "url" => null,
                    "alt" => null,
                ];
            }
            $out = [
                'id'                    => (int)$this->id,
                'slug'                  => $this->lang_slug,
                'title'                 => $this->frontend_title,
                'images'                => [$image],
                '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),
                'hasQrCode'             => $this->qrcode ? true : false,
                'isMuseum'              => is_null($this->propertyType) ? false : $this->propertyType->isMuseum(),
                'isArcheologicalSite'   => is_null($this->propertyType) ? false : $this->propertyType->isArcheologicalSite(),
                'isMusealizedSite'      => is_null($this->propertyType) ? false : $this->propertyType->isMusealizedSite(),
                'isDiscoveryPlace'      => is_null($this->propertyType) ? false : $this->propertyType->isDiscoveryPlace(),
                'chronologies'          => new ChronologiesCollection($this->chronologies, false),
                'isAccessable'          => $this->accesses->count() > 0,
                'hasEpigraphs'          => $this->epigraphs->count() > 0,
                'address'               => $this->address ?? "",
                'popupDescripition'     => $this->popup_description,
                'location'              => [
                    'lng' => $this->geolocation->getLng(),
                    'lat' => $this->geolocation->getLat(),
                ],
            ];
        }
        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;
    }
}
