<?php

namespace App\Http\Controllers\Web;


use Illuminate\Http\Request;
use Illuminate\View\View;
use App\Http\Controllers\Controller;
use Modules\Category\Interfaces\CategoryRepositoryInterface;
use Modules\Category\Services\CategoryService;
use Modules\Page\Models\Page;
use Modules\Page\Services\PageService;
use Modules\Partner\Services\PartnerService;
use Modules\Product\Enums\ProductStatusSoldEnum;
use Modules\Product\Interfaces\ProductRepositoryInterface;
use Modules\Product\Services\ProductService;
use Modules\Review\Services\ReviewService;
use Modules\Slider\Interfaces\SliderRepositoryInterface;
use Modules\Slider\Services\SliderService;

class HomeController extends Controller
{
    private $view = 'web.pages.home.';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    private ReviewService $reviewService;
    private ProductService $productService;
    private PageService $pageService;
    private CategoryService $categoryService;
    private SliderService $sliderService;

    public function __construct(ReviewService $reviewService , ProductService $productService , PageService $pageService , CategoryService $categoryService , SliderService $sliderService)
    {
        $this->reviewService = $reviewService;
        $this->productService = $productService;
        $this->pageService = $pageService;
        $this->categoryService = $categoryService;
        $this->sliderService = $sliderService;
    }

    /**
     * Show the application dashboard.
     * @return View
     */

    public function index(Request $request) :View
    {
        $title = __('web.home');
        $reviews = $this->reviewService->get($request);
        $sliders = $this->sliderService->get($request);
        $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $request->merge(['category_id'=>null]);
        $request->merge(['discount'=>true,'paginate'=>12]);
        $offer_products = $this->productService->get($request)->chunk(2);
        $request->merge(['discount'=>null]);
        $request->merge(['status_sold'=>ProductStatusSoldEnum::SHOW->value,'paginate'=>18]);
        $product_specials = $this->productService->get($request)->chunk(3);
        $request->merge(['paginate'=>null]);
        $request->merge(['ids'=>[2,3,4]]);
        $cards = $this->pageService->get($request);
        $page = Page::findOrFail(9);
        $page = $this->pageService->getItem($page);
        $banner = Page::findOrFail(10);
        $banner = $this->pageService->getItem($banner);
        $newsletter = Page::findOrFail(12);
        $newsletter = $this->pageService->getItem($newsletter);

        $section_1 =Page::findOrFail(15);
        $section_2 =Page::findOrFail(14);
        $section_3 =Page::findOrFail(13);
        $section_1 = $this->pageService->getItem($section_1);
        $section_2 = $this->pageService->getItem($section_2);
        $section_3 = $this->pageService->getItem($section_3);


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

    public function products(Request $request)
    {
        $request->merge(['paginate'=>6]);
        if (in_array($request->typeStatus,['newest','rate']))
        {
            $request->merge(['orderBy'=>$request->typeStatus]);
        }
        elseif($request->typeStatus == 'sold')
        {
            $request->merge(['sold'=>true]);
        }
        elseif($request->typeStatus == 'discount')
        {
            $request->merge(['discount'=>true]);
        }
        $products = $this->productService->get($request);
        return view($this->view.'products', get_defined_vars());
    }

}
