<?php

namespace Modules\Shipping\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\City\Services\CityService;
use Modules\Shipping\Models\Shipping;
use Modules\Shipping\Requests\Admin\ShippingRequest;
use Modules\Shipping\Services\ShippingService;

class ShippingController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/shippings';
    /** View folder */
    private $viewDirectory= 'admin.shippings.';
    //
    private ShippingService $shippingService;
    private CityService $cityService;

    public function __construct(ShippingService $shippingService,CityService $cityService)
    {
        $this->shippingService = $shippingService;
        $this->cityService = $cityService;
    }

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

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

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

    /**
     * Show the form for creating a new Shipping.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('shippings.store');
        $head= __('shippings.new');
        $request = $request->merge(['shipping_id'=>'main']);
        $shippings = $this->shippingService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Shipping.
     *
     * @param  Shipping  $shipping
     * @return View
     */
    public function edit(Request $request,Shipping $shipping) :View
    {
        // dd($shipping->id);
        $action= route('shippings.update', $shipping->id);
        $head= __('shippings.edit');
        $cities = $this->shippingService->cities($shipping);
        $mainCities = $this->cityService->get($request);
        $shipping = $this->shippingService->getItem($shipping);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }
    public function cities(Request $request)
    {
        $key = $request->key;
        $cities = $this->cityService->get($request);
        return view( $this->viewDirectory.'cities', get_defined_vars() );
    }
    /**
     * Update the specified Shipping in storage.
     * @param ShippingRequest $request
     * @param Shipping $shipping
     * @return RedirectResponse|Redirector
     */
    public function update(ShippingRequest $request, Shipping $shipping) :JsonResponse
    {
        $shipping= $this->shippingService->save($request, $shipping);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> route('shippings.index'),
        ];
        return ResponseService::response($response);
    }

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

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

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

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