<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Http\Requests\Admin\ProductRequest;
use App\Models\Product;
use App\Services\Interfaces\ProductRepositoryInterface;
use App\Services\Response\ResponseService;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Redirector;
use Illuminate\Http\RedirectResponse;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;

class ProductController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/products';
    /** View folder */
    private $viewDirectory= 'admin.products.';
    //
    private ProductRepositoryInterface $productRepository;

    public function __construct(ProductRepositoryInterface $productRepository)
    {
        $this->productRepository = $productRepository;
    }

    /**
     * Display a listing of the menu.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('products.head');
        $products= $this->productRepository->get($request);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    /**
     * return data of the menu.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $products= $this->productRepository->get($request);

        return view($this->viewDirectory.'grid',compact('products'));
    }

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('menu.store');
        $head= __('menu.new');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Store a newly created Product in storage.
     * @param ProductRequest $request
     * @return RedirectResponse|Redirector
     */
    public function store(ProductRequest $request) :JsonResponse
    {
        $product= $this->productRepository->save($request, null);

        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('menu.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Show the form for editing the specified Product.
     *
     * @param  Product  $product
     * @return View
     */
    public function edit($id) :View
    {
        $action= route('menu.update', $id);
        $product = $this->productRepository->getItem($id);
        $head= __('menu.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Product in storage.
     * @param ProductRequest $request
     * @param Product $product
     * @return RedirectResponse|Redirector
     */
    public function update(ProductRequest $request, Product $product) :JsonResponse
    {
        $product= $this->productRepository->save($request, $product);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('menu.index'),
        ];
        return ResponseService::response($response);
    }

    /**
     * Remove the specified Product from storage.
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws Exception
     */
    public function destroy(Request $request, int $id) :JsonResponse
    {
        $return= $this->productRepository->destroy($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Remove several menu by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function destroyAll(Request $request) :JsonResponse
    {
        $return= $this->productRepository->destroyAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore the specified Product from storage.
     *
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function restore(Request $request, int $id) :JsonResponse
    {
        $return= $this->productRepository->restore($request, $id);
        return response()->json($return[0], $return[1] ?? 200);
    }

    /**
     * Restore several menu by IDs.
     *
     * @param Request $request
     * @return JsonResponse
     */
    public function restoreAll(Request $request) :JsonResponse
    {
        $return= $this->productRepository->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
