<?php

namespace Modules\Product\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\Attribute\Enums\AttributeStatusEnum;
use Modules\Attribute\Services\AttributeService;
use Modules\Category\Services\CategoryService;
use Modules\Option\Enums\OptionStatusEnum;
use Modules\Option\Services\OptionService;
use Modules\Product\Enums\ProductStatusEnum;
use Modules\Product\Models\Product;
use Modules\Product\Requests\Admin\ProductRequest;
use Modules\Product\Services\ProductService;

class ProductController extends Controller
{

    /** Redirect to this path after each operation success*/
    private $redirectSuccessPath= '/admin/products';
    /** View folder */
    private $viewDirectory= 'admin.products.';
    //
    private ProductService $productService;
    private CategoryService $categoryService;
    private AttributeService $attributeService;
    private OptionService $optionService;

    public function __construct(ProductService $productService , CategoryService $categoryService,AttributeService $attributeService ,OptionService $optionService)
    {
        $this->productService = $productService;
        $this->categoryService = $categoryService;
        $this->attributeService = $attributeService;
        $this->optionService = $optionService;
    }

    /**
     * Display a listing of the products.
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request) :View
    {
        $request = $request->merge(['product_id'=>'main']);
        $products = $this->productService->get($request);
        $title= __('products.head');
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $request = $request->merge(['category_id'=>'sub']);
        $sub_categories = $this->categoryService->get($request);
        return view($this->viewDirectory.'index', get_defined_vars());
    }

    public function size(Request $request)
    {
        $key = $request->key;
        $request->merge(['type'=>AttributeStatusEnum::SIZE->value]);
        $sizes = $this->attributeService->get($request);
        return view($this->viewDirectory.'size', get_defined_vars());
    }
    public function color(Request $request)
    {
        $key = $request->key;
        $keyItem = $request->keyItem;
        $request->merge(['type'=>AttributeStatusEnum::COLOR->value]);
        $colors = $this->attributeService->get($request);
        return view($this->viewDirectory.'color', get_defined_vars());
    }
    public function item(Request $request)
    {
        $key = $request->key;
        return view($this->viewDirectory.'product_item', get_defined_vars());
    }
    public function itemKey(Request $request)
    {
        $key = $request->key;
        $keyItem = $request->keyItem;
        return view($this->viewDirectory.'item', get_defined_vars());
    }

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

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

    /**
     * Show the form for creating a new Product.
     *
     * @return View
     */
    public function create(Request $request) :View
    {
        $action= route('products.store');
        $head= __('products.new');
        $request = $request->merge(['category_id'=>'main']);
        $categories = $this->categoryService->get($request);
        $statuses = ProductStatusEnum::values();
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

    /**
     * Show the form for editing the specified Product.
     *
     * @param  Product  $product
     * @return View
     */
    public function edit(Request $request,Product $product) :View
    {
        // dd($product->id);
        $action= route('products.update', $product->id);
        $product= $this->productService->getItem($product);
        $head= __('products.edit');
        $statuses = ProductStatusEnum::values();
        $items = $this->productService->items($product);
        $additions = $this->productService->additions($product);
        $logs = $this->productService->logs($product);
        $request->merge(['type'=>OptionStatusEnum::REASON_REFUSE_PRODUCT->value]);
        $reasons = $this->optionService->get($request);
        return view( $this->viewDirectory.'form', get_defined_vars() );
    }

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

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

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

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

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