<?php

namespace Modules\Option\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\Option\Enums\OptionStatusEnum;
use Modules\Option\Models\Option;
use Modules\Option\Requests\Admin\OptionRequest;
use Modules\Option\Services\OptionService;

class OptionController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/options';
    /** View folder */
    private $viewDirectory= 'admin.options.';
    //
    private OptionService $optionService;

    public function __construct(OptionService $optionService)
    {
        $this->optionService = $optionService;
    }

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

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

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

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

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

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

    /**
     * Show the form for editing the specified Option.
     *
     * @param  Option  $option
     * @return View
     */
    public function edit(Option $option) :View
    {
        // dd($option->id);
        $action= route('options.update', $option->id);
        $data= $this->optionService->getItem($option);
        $head= __('options.edit');
        $statuses = OptionStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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