<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\MemberExport;
use App\Http\Requests\Admin\SliderRequest;
use App\Models\Slider;
use App\Services\Interfaces\SliderRepositoryInterface;
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 SliderController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/sliders';
    /** View folder */
    private $viewDirectory= 'admin.sliders.';
    //
    private SliderRepositoryInterface $sliderRepository;

    public function __construct(SliderRepositoryInterface $sliderRepository)
    {
        $this->sliderRepository = $sliderRepository;
    }

    /**
     * Display a listing of the sliders.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('sliders.head');
        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->sliderRepository->get($request);

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

    /**
     * Show the form for creating a new Slider.
     *
     * @return View
     */
    public function create() :View
    {
        $action= route('sliders.store');
        $head= __('sliders.new');
        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->sliderRepository->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(Slider $slider) :View
    {
        // dd($slider->id);
        $action= route('sliders.update', $slider->id);
        $data= $this->sliderRepository->getItem($slider);
        $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->sliderRepository->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->sliderRepository->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->sliderRepository->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->sliderRepository->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->sliderRepository->restoreAll($request);
        return response()->json($return[0], $return[1] ?? 200);
    }
}
