<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Exports\MemberExport;
use App\Http\Requests\Admin\BannerRequest;
use App\Models\Banner;
use App\Models\Category;
use App\Services\Interfaces\BannerRepositoryInterface;
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 BannerController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/banners';
    /** View folder */
    private $viewDirectory= 'admin.banners.';
    //
    private BannerRepositoryInterface $bannerRepository;

    public function __construct(BannerRepositoryInterface $bannerRepository)
    {
        $this->bannerRepository = $bannerRepository;
    }

    /**
     * Display a listing of the banners.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $title= __('banners.head');

        return view($this->viewDirectory.'index', get_defined_vars());
    }

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

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

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

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

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

    /**
     * Show the form for editing the specified Banner.
     *
     * @param  Banner  $banner
     * @return View
     */
    public function edit(Banner $banner) :View
    {
        // dd($banner->id);
        $action= route('banners.update', $banner->id);
        $banner= $this->bannerRepository->getItem($banner);

        $head= __('banners.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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