<?php

namespace Modules\Page\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\Page\Models\Page;
use Modules\Page\Requests\Admin\PageRequest;
use Modules\Page\Services\PageService;

class PageController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/pages';
    /** View folder */
    private $viewDirectory= 'admin.pages.';
    //
    private PageService $pageService;

    public function __construct(PageService $pageService)
    {
        $this->pageService = $pageService;
    }

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

    /**
     * return data of the pages.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) //:View
    {
        $request->merge(['paginate'=> true]);
        if ($request->intro)
        {
            $request->merge(['ids'=>[7,8,9]]);
        }
        else
        {
            $request->merge(['not_ids'=>[7,8,9]]);
        }
        $pages= $this->pageService->get($request);

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

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

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

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

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

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

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

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

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

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