<?php

namespace App\Http\Controllers\Web;

use App\Services\Interfaces\CategoryRepositoryInterface;
use App\Services\Interfaces\ProductRepositoryInterface;
use App\Services\Interfaces\SliderRepositoryInterface;
use App\Http\Controllers\Controller;
use App\Services\Interfaces\GroupRepositoryInterface;
use Illuminate\Http\Request;

class MenuController extends Controller
{
    private $view = 'web.pages.menu.';
    private $productRepository ;
    private $sliderRepository ;
    private $groupRepository ;
    private $categoryRepository ;


    public function __construct(
        ProductRepositoryInterface $productRepository,
        SliderRepositoryInterface $sliderRepository,
        GroupRepositoryInterface $groupRepository,
        CategoryRepositoryInterface $categoryRepository
    )
    {
        $this->productRepository = $productRepository;
        $this->sliderRepository = $sliderRepository;
        $this->groupRepository = $groupRepository;
        $this->categoryRepository = $categoryRepository;
    }

    public function index(Request $request)
    {
        $sliders = $this->sliderRepository->get($request);
        $products = $this->productRepository->get($request);
        $subGroups = null;
        $category = null;
        if($request->group_id)
        {
            $group = $this->groupRepository->getItem($request->group_id);
            $request->merge(['active'=> 1, 'paginate'=> null]);
            $subGroups = $this->groupRepository->get($request);
        }

        $title = __('web.menu');

        return view($this->view.'index', get_defined_vars());
    }

    public function show(Request $request,$id)
    {
        $sliders = $this->sliderRepository->get($request);
        $product = $this->productRepository->getItem($id);
        // $group = $this->groupRepository->getItem($product->groups->first()->group_id ?? null);
        $ids = $product->groups->pluck('group_id')->toArray();
        $request->merge(['ids'=>$ids]);
        $groups = $this->groupRepository->get($request);
        $title = $product->name;
        return view($this->view.'details', get_defined_vars());
    }


    public function menu(Request $request)
    {
        // $menus = $this->categoryRepository->get($request);
        $request->merge(['active'=> 1, 'paginate'=> null, 'parents_only'=> true]);
        $menus = $this->groupRepository->get($request);

        $title = __('web.menu');

        return view($this->view.'menu', get_defined_vars());
    }
}
