<?php

namespace Modules\Gallery\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\Gallery\Models\Gallery;
use Modules\Gallery\Requests\Admin\GalleryRequest;
use Modules\Gallery\Services\GalleryService;

class GalleryController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/galleries';
    /** View folder */
    private $viewDirectory= 'admin.galleries.';
    //
    private GalleryService $galleryService;

    public function __construct(GalleryService $galleryService)
    {
        $this->galleryService = $galleryService;
    }

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


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

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

}
