<?php

namespace TPS\API\Http\Controllers;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use TPS\API\Http\Resources\Checkout\CartResource;
use Webkul\CartRule\Repositories\CartRuleCouponRepository;
use Webkul\Checkout\Facades\Cart;
use Webkul\Checkout\Repositories\CartItemRepository;
use Webkul\Customer\Repositories\WishlistRepository;
use Webkul\RestApi\Http\Controllers\V1\Shop\Customer\CartController;

class Carts extends CartController
{
    /**
     * Get the customer cart.
     *
     * @return \Illuminate\Http\Response
     */
    public function get()
    {
        $cart = Cart::getCart();

        return response([
            'data' => $cart ? new CartResource($cart) : null,
        ]);
    }
    /**
     * Add item to the cart.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Webkul\Customer\Repositories\WishlistRepository $wishlistRepository
     * @param  int  $productId
     * @return \Illuminate\Http\Response
     */
    public function add(Request $request, WishlistRepository $wishlistRepository, int $productId)
    {
        $customer = $request->user();

        try {
            Cart::deactivateCurrentCartIfBuyNowIsActive();
            Event::dispatch('checkout.cart.item.add.before', $productId);

            $result = Cart::addProduct($productId, $request->all());

            if (is_array($result) && isset($result['warning'])) {
                return response([
                    'success' => false,
                    'message' => $result['warning'],
                ], 405);
            }

            $wishlistRepository->deleteWhere(['product_id' => $productId, 'customer_id' => $customer->id]);

            Event::dispatch('checkout.cart.item.add.after', $result);

            Cart::collectTotals();

            $cart = Cart::getCart();

            return response([
                'success' => true,
                'data'    => $cart ? new CartResource($cart) : null,
                'message' => __('rest-api::app.checkout.cart.item.success'),
            ]);
        } catch (Exception $e) {
            return response([
                'success' => false,
                'message' => $e->getMessage(),
            ], 405);
        }
    }

    /**
     * Update the cart.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Webkul\Checkout\Repositories\CartItemRepository  $cartItemRepository
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, CartItemRepository $cartItemRepository)
    {
        $this->validate($request, [
            'qty' => 'required|array',
        ]);

        foreach ($request->qty as $qty) {
            if ($qty <= 0) {
                return response([
                    'success' => false,
                    'message' => __('shop::app.checkout.cart.quantity.illegal'),
                ], 405);
            }
        }

        try {
            foreach ($request->qty as $itemId => $qty) {
                $item = $cartItemRepository->findOneByField('id', $itemId);

                Event::dispatch('checkout.cart.item.update.before', $itemId);

                Cart::updateItems(['qty' => $request->qty]);

                Event::dispatch('checkout.cart.item.update.after', $item);
            }
        }
        catch(Exception $ex){
            return response([
                'success' => false,
                'message' => $ex->getMessage(),
            ]);
        }


        Cart::collectTotals();

        $cart = Cart::getCart();

        return response([
            'success' => true,
            'data'    => $cart ? new CartResource($cart) : null,
            'message' => __('shop::app.checkout.cart.quantity.success'),
        ]);
    }
    /**
     * Remove item from the cart.
     *
     * @param  int  $cartItemId
     * @return \Illuminate\Http\Response
     */
    public function removeItem($cartItemId)
    {
        Event::dispatch('checkout.cart.item.delete.before', $cartItemId);

        Cart::removeItem($cartItemId);

        Event::dispatch('checkout.cart.item.delete.after', $cartItemId);

        Cart::collectTotals();

        $cart = Cart::getCart();

        return response([
            'success' => true,
            'data'    => $cart ? new CartResource($cart) : null,
            'message' => __('shop::app.checkout.cart.item.success'),
        ]);
    }
    /**
     * Empty the cart.
     *
     * @return \Illuminate\Http\Response
     */
    function empty() {
        Event::dispatch('checkout.cart.delete.before');

        Cart::deActivateCart();

        Event::dispatch('checkout.cart.delete.after');

        $cart = Cart::getCart();

        return response([
            'success' => true,
            'data'    => $cart ? new CartResource($cart) : null,
            'message' => __('shop::app.checkout.cart.item.success-remove'),
        ]);
    }
    /**
     * Move cart item to wishlist.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function moveToWishlist($cartItemId)
    {
        Event::dispatch('checkout.cart.item.move-to-wishlist.before', $cartItemId);

        Cart::moveToWishlist($cartItemId);

        Event::dispatch('checkout.cart.item.move-to-wishlist.after', $cartItemId);

        Cart::collectTotals();

        $cart = Cart::getCart();

        return response([
            'success' => true,
            'data'    => $cart ? new CartResource($cart) : null,
            'message' => __('shop::app.checkout.cart.move-wishlist.success'),
        ]);
    }

    /**
     * Apply coupon to the cart.
     *
     * @return \Illuminate\Http\Response
     */
    public function applyKupon(CartRuleCouponRepository $cartRuleCouponRepository)
    {
        $couponCode = request()->get('code');

        try {
            if (strlen($couponCode)) {
                $coupon = $cartRuleCouponRepository->findOneByField('code', $couponCode);

                if ($coupon->cart_rule->status) {
                    if (Cart::getCart()->coupon_code == $couponCode) {
                        return response()->json([
                            'success' => false,
                            'message' => trans('shop::app.checkout.total.coupon-already-applied'),
                        ]);
                    }

                    Cart::setCouponCode($couponCode)->collectTotals();

                    $cart = Cart::getCart();

                    if ($cart->coupon_code == $couponCode) {
                        return response()->json([
                            'success' => true,
                            'data'    => new CartResource($cart),
                            'message' => trans('shop::app.checkout.total.success-coupon'),
                        ]);
                    }
                }
            }

            return response()->json([
                'success' => false,
                'message' => trans('shop::app.checkout.total.invalid-coupon'),
            ]);
        } catch (\Exception $e) {
            report($e);

            return response()->json([
                'success' => false,
                'message' => trans('shop::app.checkout.total.coupon-apply-issue'),
            ]);
        }
    }

    /**
     * Remove the coupon code.
     *
     * @return \Illuminate\Http\Response
     */
    public function removeCoupon()
    {
        Cart::removeCouponCode()->collectTotals();

        return response([
            'success' => true,
            'data' => new CartResource(Cart::getCart()),
            'message' => __('shop::app.checkout.cart.coupon.remove'),
        ]);
    }
}