<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\Blocks as BlockResource;
use App\Http\Resources\ProductCollection as ProductsResource;
use App\Http\Resources\ProductOtherVersionsCollection;
use App\Http\Resources\BluetoothVersions;
use App\Models\GenericBackoffice\WebsiteObject;


class ProductResource 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();
        $url = $activeLang === 'en' ? $this->lang_uri : '/' . $activeLang . $this->lang_uri;
        $highlightImage = getImage($this->highlightImage, '640x640');
        $out = [
            'id'             => (int)$this->id,
            'title'          => $this->block_title,
            'price'          => $this->price,
            'slug'           => '/' . $this->lang_slug,
            'subTitle'   => $this->productGroup ? $this->productGroup->title : 'None',
            'language_id'    => $this->language_id,
            'url'            => $url,
            'highlightImage' => $highlightImage
        ];
        if ($this->isDetail) {
            $otherImages = [];
            $out['highlightImage'] = getImage($this->highlightImage, '1100x825');
            foreach($this->otherImages as $image) {
                array_push($otherImages, getImage($image, '545x545'));
            }

            $buyNowLink = $this->buy_now_link;
            if ($buyNowLink) {
                if (array_key_exists('objectId', $buyNowLink)) {
                    $websiteObject = WebsiteObject::find($buyNowLink['objectId']);
                    if ($websiteObject) {
                        $buyNowLink['url'] = $websiteObject->uri;
                    }
                }
                unset($buyNowLink['objectId']);
            }
            $quickStart = getDocument($this->quickStart);
            if ($quickStart) {
                $quickStart->title = $this->quick_start_label ?? 'Quick start guide';
            }
            $otherLink = $this->other_link;
            if ($otherLink) {
                if (array_key_exists('objectId', $otherLink)) {
                    $websiteObject = WebsiteObject::find($otherLink['objectId']);
                    if ($websiteObject) {
                        $otherLink['url'] = $websiteObject->uri;
                    }
                }
                unset($otherLink['objectId']);
            }

            $out = array_merge($out, [
                'otherVersions'      => new ProductOtherVersionsCollection($this->otherVersions),
                'relatedProducts'    => new ProductsResource($this->relatedProducts, false),
                'productGroup'       => $this->productGroup ? $this->productGroup->title : 'None',
                'bluetoothVersion'   => new BluetoothVersions($this->bluetoothVersion),
                'versionDescription' => $this->version_description,
                'buyNowLink'         => $buyNowLink,
                'quickStartLink'     => $quickStart,
                'otherLink'          => $otherLink,
                'blocks'             => BlockResource::collection($this->productBlocks),
                'otherImages'        => $otherImages,
                'descriptionTitle'   => $this->description_title,
                'descriptionText'    => $this->description,
            ]);
        }
        return $out;
    }
}
