<?php

namespace App\Helpers\Backoffice;

use App\Helpers\Backoffice\LanguageHelper;
use App\Models\Backoffice\WebsiteObject;
use App\Models\Backoffice\WebsiteObjectLanguage;
use Illuminate\Database\Eloquent\Collection;
use Mcamara\LaravelLocalization\Facades\LaravelLocalization;

class DatabaseHelper
{
    public static function loadPolymorphicDependencies(
        Collection $collection,
        string $polymorphicName,
        string $modelType,
        array $relation,
        string $polyType = null,
        string $polyId = null
    )
    {
        if (empty($polyType)) {
            $polyType = $polymorphicName."_type";
        }

        if (empty($polyId)) {
            $polyId = $polymorphicName."_id";
        }
        $newCollection = new Collection();

        foreach ($collection as $model) {
            if ($model->$polymorphicName instanceof $modelType) {
                $newCollection->add($model->$polymorphicName);
            }
        }

        $newCollection->load($relation);
        return $collection;
    }

    public static function langUrl($url = null, $locale = null)
    {

        $langUrl = LaravelLocalization::getLocalizedURL($locale, $url);
        return rtrim($langUrl, '/');
    }

    public static function langUri($url = null, $locale = null)
    {

        $langUrl = self::langUrl($url, $locale);

        return str_replace(url(''), '', $langUrl);
    }


    public static function resolveLink($link)
    {
        if (!$link) {
            return [];
        }
        if (is_string($link)) {
            $link = json_decode($link, true);
        }
        if (array_key_exists('selectedObjectId', $link)) {
            $activeLanguage = LaravelLocalization::getCurrentLocale();
            $websiteObject = WebsiteObject::join('website_objects_lang', 'website_objects_lang.website_object_id', '=', 'website_objects.website_object_id')
                ->where('website_objects.website_object_id', $link['selectedObjectId'])
                ->first();
            if ($websiteObject) {
                // Articles are special, we need to find their category to build the correct URL
                if ($websiteObject->content_type == 'article') {
                    $catObject = WebsiteObject::join('website_objects_lang', 'website_objects_lang.website_object_id', '=', 'website_objects.website_object_id')
                        ->join('categories', 'categories.id', '=', 'website_objects.object_id')
                        ->join('articles', 'articles.category_id', '=', 'categories.id')
                        ->where('website_objects.content_type', 'category')
                        ->where('articles.id', $websiteObject->object_id)
                        ->first();
                    if (!$catObject) {
                        return $link;
                    }
                    $link['url'] = self::langUri($catObject->uri . '/' . $websiteObject->slug, $activeLanguage);
                } else {
                    $link['url'] = self::langUri($websiteObject->uri, $activeLanguage);
                }
            }
        }

        return $link;
    }
}
