<?php

namespace App\Repositories\Shop;

use App\Models\Shop\Category;
use App\Repositories\Core\Categories as CategoryTrees;
use App\Repositories\Core\Tag;
use App\Traits\Model\Basic;

class Categories
{
    use Basic;

    public static function getAncestorsByArticle($id)
    {
        $category = Articles::get($id)->categories()->first();
        $ancestors = self::getAncestorsByCategory($category->id);
        $ancestors[] = $category->toArray();

        return $ancestors;
    }

    public static function getAncestorsByCategory($id)
    {
        $category = self::get($id);
        $ancestors = $category->getAncestors()->toArray();
        unset($ancestors[0]);

        return $ancestors;
    }

    public static function getByHomepage()
    {
        return Category::hasAvailableOffers()->visible()->homepage()->orderBy('name', 'asc')->get();
    }

    public static function getFull($id)
    {
        $category = self::get($id, ['Parent']);
        $data = $category->toArray();
        $data['name'] = $category->name;
        $data['description'] = $category->description;
        $data['tags'] = self::getTagsByCategory($category);

        return $data;
    }

    public static function getTagsByCategory($category)
    {
        return $category->tags->pluck('id')->toArray();
    }

    public static function getTagsNameByCategory($category)
    {
        return $category->tags->pluck('name', 'id')->toArray();
    }

    public static function getTreeVisibles()
    {
        return CategoryTrees::getTreeVisibles();
    }

    public static function getTree()
    {
        return CategoryTrees::getTree();
    }

    public static function storeFull($data)
    {
        $images = $data['images'] ?? false;
        unset($data['images']);

        $tags = $data['tags'] ?? false;
        unset($data['tags']);

        $category = self::store($data);
        self::storeImages($category, $images);
        self::storeTags($category, $tags);

        return $category;
    }

    public static function storeTags($category, $tags)
    {
        return Tag::storeTags($category, $tags);
    }

    public static function storeImages($category, $files)
    {
        if ($files) {
            foreach ($files as $file) {
                self::storeImage($category, $file);
            }
        }
    }

    public static function storeImage($category, $file)
    {
        return $category->addMedia($file)->withResponsiveImages()->toMediaCollection('images');
    }

    public static function getImages($id)
    {
        $category = self::get($id);
        if (! $category) {
            return false;
        }
        $category->getMedia();
        foreach ($category->media as $key => $media) {
            $category->media[$key]['url'] = $media->getUrl();
        }

        return $category->media;
    }

    public static function deleteImage($id, $index)
    {
        $category = self::get($id);
        $category->getMedia();
        $ret = $category->media[$index]->delete();

        return '1';
    }

    public static function moveTree($node_id, $target_id, $type)
    {
        return CategoryTrees::moveTree($node_id, $target_id, $type);
    }

    public static function toggleVisible($id, $visible)
    {
        return self::update(['visible' => $visible], $id);
    }

    public static function toggleHomepage($id, $homepage)
    {
        return self::update(['homepage' => $homepage], $id);
    }

    public static function getRoot()
    {
        return app('rinvex.categories.category')->find(1);
    }

    public static function getModel()
    {
        return Category::query();
    }
}
