<?php

namespace Modules\Slider\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\Slider\Interfaces\SliderRepositoryInterface;
use Modules\Slider\Models\Slider;
use Modules\Slider\Requests\Admin\SliderRequest;
use Modules\Slider\Enums\SliderStatusEnum;
use Modules\Slider\Services\SliderService;
use Modules\Store\Services\StoreService;

class SliderController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/sliders';
    /** View folder */
    private $viewDirectory= 'admin.sliders.';
    //
    private SliderService $sliderService;
    private StoreService $storeService;

    public function __construct(SliderService $sliderService , StoreService $storeService)
    {
        $this->sliderService = $sliderService;
        $this->storeService = $storeService;
    }

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Slider.
     *
     * @param  Slider  $slider
     * @return View
     */
    public function edit(Request $request,Slider $slider) :View
    {
        // dd($slider->id);
        $action= route('sliders.update', $slider->id);
        $slider= $this->sliderService->getItem($slider);
        $stores = $this->storeService->get($request);
        $head= __('sliders.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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