<?php

namespace Modules\Category\Controllers\Admin;

/** Ajax Request */

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 Modules\Category\Models\Category;
use Modules\Category\Requests\Admin\CategoryRequest;
use Modules\Category\Services\CategoryService;

class CategoryController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/categories';
    /** View folder */
    private $viewDirectory= 'admin.categories.';
    //
    private CategoryService $categoryService;

    public function __construct(CategoryService $categoryService)
    {
        $this->categoryService = $categoryService;
    }

    /**
     * Display a listing of the categories.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $title= __('categories.head');
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    public function model(Request $request)
    {
        $sub_categories = $this->categoryService->get($request);
        return view($this->viewDirectory.'categories', get_defined_vars());
    }

    /**
     * return data of the categories.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        $categories= $this->categoryService->get($request);

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

    /**
     * Show the form for creating a new Category.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('categories.store');
        $head= __('categories.new');
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Category.
     *
     * @param  Category  $category
     * @return View
     */
    public function edit(Request $request,Category $category) :View
    {
        // dd($category->id);
        $action= route('categories.update', $category->id);
        $category= $this->categoryService->getItem($category);
        $head= __('categories.edit');
        $request = $request->merge(['category_id'=>'main','not_id'=>$category->id]);
        $categories = $this->categoryService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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