<?php

namespace App\Http\Resources;

use App\Models\GenericBackoffice\WebsiteObject;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Blocks as BlockResource;

class OpenPositionResource extends JsonResource
{
    protected $isDetail = FALSE;
    protected $linkStyle = [];

    public function __construct($resource, $isDetail = FALSE, array $linkStyle = [])
    {
        $this->resource = $resource;
        $this->isDetail = $isDetail;
        $this->linkStyle = $linkStyle;
    }

    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $out = [
            'id' => (int)$this->id,
            'title' => $this->position_title,
            'url' => '/' . $this->lang_slug,
            'languages' => new WebsiteObjectLanguages($this->getActiveWebsiteEntry()->languages),
            'style' => $this->linkStyle
        ];

        if ($this->isDetail) {
            // SEO IMAGE
            $metaImage = $this->metaImage ? getImage($this->metaImage, '1200x630') : NULL;

            $out = array_merge($out,
                [
                    'id' => (int)$this->id,
                    'title' => $this->position_title,
                    'details' => [],
                    'specs' => [],
                    'link' => $this->_getBlockLinkWithStyleInfo($this->link_info),
                    'linkDescription' => $this->link_description,
                    'blocks' => BlockResource::collection($this->blocks),
                    'url' => '/' . $this->lang_slug,
                    'meta' => (object)[
                        'title' => $this->meta_title,
                        'description' => $this->meta_description,
                        'keywords' => $this->meta_keywords,
                        'image' => $metaImage
                    ]
                ]
            );

            $details = $this->openPositionPoDetails;
            foreach ($details as $detailRecord) {
                $out['details'][] = [
                    'id' => $detailRecord->id,
                    'title' => $detailRecord->detail_title,
                    'text' => $detailRecord->text
                ];
            }
            $specs = $this->openPositionPoSpecs;
            foreach ($specs as $specRecord) {
                $out['specs'][] = [
                    'id' => $specRecord->id,
                    'title' => $specRecord->spec_title,
                    'text' => $specRecord->text_wysiwyg
                ];
            }
        }
        return $out;
    }

    private function _getLink(?array $blockLink)
    {
        if ($blockLink) {
            if (array_key_exists('objectId', $blockLink)) {
                $websiteObject = WebsiteObject::find($blockLink['objectId']);
                if ($websiteObject) {
                    $blockLink['url'] = $websiteObject->uri;
                }
            }
        }
        return $blockLink;
    }

    private function _getBlockLinkWithStyleInfo($blockLink)
    {
        if (!is_array($blockLink)) {
            $blockLink = json_decode($blockLink, true);
        }
        $blockLink = $this->_getLink($blockLink);
        if ($blockLink && $blockLink['url']) {
            $blockLink['style'] = [];
            $blockLink['style']['type'] = $this->link_effect;
            $blockLink['style']['iconSize'] = $this->link_icon_size;
            $blockLink['style']['isIconFirst'] = $this->link_icon_side == 'left';
            $blockLink['style']['textSize'] = $this->link_text_size;
        } else {
            $blockLink = null;
        }
        return $blockLink;
    }
}
