<?php

namespace Modules\Package\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\Package\Models\Package;
use Modules\Package\Requests\Admin\PackageRequest;
use Modules\Package\Services\PackageService;

class PackageController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/packages';
    /** View folder */
    private $viewDirectory= 'admin.packages.';
    //
    private PackageService $packageService;

    public function __construct(PackageService $packageService)
    {
        $this->packageService = $packageService;
    }

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

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

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

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

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

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

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

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

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

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

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

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