<?php

namespace Modules\Country\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\Country\Models\Country;
use Modules\Country\Requests\Admin\CountryRequest;
use Modules\Country\Services\CountryService;

class CountryController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/countries';
    /** View folder */
    private $viewDirectory= 'admin.countries.';
    //
    private CountryService $countryService;

    public function __construct(CountryService $countryService)
    {
        $this->countryService = $countryService;
    }

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

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

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

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

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

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

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

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

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

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

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

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