<?php

namespace App\Http\Controllers\Admin;

/** Ajax Request */

use App\Http\Requests\Admin\RoomRequest;
use App\Models\Room;
use App\Services\Interfaces\RoomRepositoryInterface;
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 RoomController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/rooms';
    /** View folder */
    private $viewDirectory= 'admin.rooms.';
    //
    private RoomRepositoryInterface $roomRepository;

    public function __construct(RoomRepositoryInterface $roomRepository)
    {
        $this->roomRepository = $roomRepository;
    }

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

    public function fetchRooms(Request $request)
    {
        $rooms= $this->roomRepository->get($request);
        return $rooms;
    }

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

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

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

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

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

    /**
     * Show the form for editing the specified Room.
     *
     * @param  Room  $room
     * @return View
     */
    public function edit(Room $room) :View
    {
        // dd($room->id);
        $action= route('rooms.update', $room->id);
        $data= $this->roomRepository->getItem($room);
        $head= __('rooms.edit');
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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