<?php

namespace Modules\Banner\Controllers\Admin;

/** Ajax Request */

use App\Services\Response\ResponseService;
use Illuminate\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Modules\Banner\Enums\BannerActiveStatusEnum;
use Modules\Banner\Enums\BannerCountPhotoEnum;
use Modules\Banner\Enums\BannerStatusEnum;
use Modules\Banner\Requests\Admin\BannerRequest;
use Modules\Banner\Services\BannerService;
use Modules\Type\Services\TypeService;

class BannerController extends Controller
{

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

    public function __construct(
        private BannerService $bannerService,
        private TypeService $typeService,
    )
    {}

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

    /**
     * return data of the banners.
     *
     * @param Request $request
     * @return View
     */
    public function grid(Request $request) :View
    {
        $request->merge(['paginate'=>true,'typeStatuses'=>BannerStatusEnum::values()]);
        $banners= $this->bannerService->get($request->all());

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

    /**
     * Show the form for editing the specified Banner.
     *
     * @param  int $id
     * @return View
     */
    public function edit(int $id) :View
    {
        $banner = $this->bannerService->getItem($id);
        if (!$banner)
        {
            abort(404);
        }
        $action= route('banners.update', $banner->id);
        $head= __('banners.edit');
        if ($banner->status == BannerStatusEnum::CATEGORY->value)
        {
            $head .= ' : '.$banner->category->currentDescription->title;
        }
        $status_category = BannerStatusEnum::CATEGORY->value;
        $status_footer = BannerStatusEnum::FOOTER->value;
        $count_photos = BannerCountPhotoEnum::values();
        if ($banner->status == BannerStatusEnum::MODELS->value)
        {
            $count_photos = BannerCountPhotoEnum::models();
        }
        $active_statuses = BannerActiveStatusEnum::values();
        $photos = $this->bannerService->photos($id);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

    /**
     * Update the specified Banner in storage.
     * @param BannerRequest $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(BannerRequest $request, int $id) :JsonResponse
    {
        $banner = $this->bannerService->checkItem($id);
        if (!$banner)
        {
            abort(404);
        }
        $this->bannerService->update($request->validated(), $id);
        session()->flash('message', __('dashboard.saveDone'));
        $response = [
            'status'=>200,
            'message'=>__('web.success'),
            'data'=> $banner->status == BannerStatusEnum::CATEGORY->value ? '' : route('banners.index'),
        ];
        return ResponseService::response($response);
    }


}
