<?php

namespace App\Repositories\Shop;

use App\Repositories\Botanic\Species;
use App\Repositories\Botanic\Varieties;
use App\Traits\Repository\Imageable;

class ArticleImages
{
    use Imageable;

    public static function getFullImagesByArticleId($id)
    {
        $article = Articles::get($id);

        return $article ? self::getFullImagesByArticle($article) : false;
    }

    public static function countFullImagesByArticleId($id)
    {
        $article = Articles::get($id);

        return $article ? self::countFullImagesByArticle($article) : 0;
    }

    public static function countFullImagesByArticle($article)
    {
        return count(self::getFullImagesByArticle($article));
    }

    public static function getFullImagesByArticle($article)
    {
        if (! $article) {
            return collect([]);
        }

        $images = count($article->images ?? []) ? $article->images : collect([]);
        switch ($article->product_type) {
            case 'App\Models\Botanic\Variety':
                $variety = $article->product ?? false;
                $specie = $variety->specie ?? false;
                if ($variety) {
                    $images = count($variety->images ?? []) ? $images->merge($variety->images) : $images;
                }
                if ($specie) {
                    $images = count($specie->images ?? []) ? $images->merge($specie->images) : $images;
                }
                break;
            case 'App\Models\Botanic\Specie':
                $specie = $article->product ?? false;
                $images = count($specie->images ?? []) ? $specie->images : $images;
                break;
            case 'App\Models\Shop\Merchandise':
                $merchandise = $article->product ?? false;
                $images = count($merchandise->images ?? []) ? $merchandise->images : $images;
                break;
            default:
        }

        return $images;
    }

    public static function getFullImageById($id)
    {
        return self::getFullImageByArticle(self::get($id));
    }

    public static function getFullImageByArticle($article)
    {
        $image = $article->image;
        if ($image) {
            return $image;
        }
        switch ($article->product_type) {
            case 'App\Models\Botanic\Variety':
                $image = $article->product->image ?? ($article->product->specie->image ?? false);
                break;
            case 'App\Models\Botanic\Specie':
                $image = $article->product->image ?? false;
                break;
            case 'App\Models\Shop\Merchandise':
                $image = $article->product->image ?? false;
                break;
            default:
        }

        return $image;
    }

    public static function getInheritedImagesByProduct($product_id, $product_type)
    {
        switch ($product_type) {
            case 'App\Models\Botanic\Variety':
                $images = Varieties::getImages($product_id);
                break;
            case 'App\Models\Botanic\Specie':
                $images = Species::getImages($product_id);
                break;
            case 'App\Models\Shop\Merchandise':
                $images = Merchandises::getImages($product_id);
                break;
            default:
        }

        return $images ?? false ? ['images' => $images] : false;
    }
}
