<?php
/**
 * Created by PhpStorm.
 * User: ali
 * Date: 9/23/2020
 * Time: 1:17 PM
 */

namespace Mamad\Cart\Repository;


use Mamad\Cart\Models\Cart;
use Mamad\Product\Models\Product;
use Mamad\Product\Repository\ProductRepository;

class CartRepository
{
    public static function add_cart_item_to_session( $product,$variation=null,$quantity=1,$group_product=null)
    {

        if(ProductRepository::in_stock($product,1,$variation)){

            $cartItem=session()->get("cart");

            if(isset($cartItem[$product])){
                if($quantity!=1){
                    $cartItem[$product]["quantity"]+=$quantity;
                } else{
                    $cartItem[$product]["quantity"]++;

                }
            }
            elseif(!isset($cartItem[$product])){
                $cartItem[$product]=[
                    "id" => $product,
                    "quantity" => $quantity,
                    "variation"=>$variation,
                    "group_product"=>$group_product
                ];
            }
            else{
                $cartItem=[
                    $product=>[
                        "id" => $product,
                        "quantity" => $quantity,
                        "variation"=>$variation,
                        "group_product"=>$group_product
                    ]
                ];
            }

            session()->forget("cart");
            session()->put("cart",$cartItem);
        } else{
            toastMessage("محصول مورد نظر در انبار موجود نیست","در انبار موجود نیست","info");
        }
    }

    public static function add_cart_item_to_database( $product,$variation=null,$quantity=1,$group_product=null,$user=null)
    {

        if($user){
            if(ProductRepository::in_stock($product,1,$variation)) {
                    $user_cart = [];
                    if ($user->cart) {
                        $user_cart = $user->cart->toArray();

                    }

                    if (!isset($user_cart["cart_item"][$product])) {
                        $user_cart["cart_item"][$product] = [
                            "id" => $product,
                            "quantity" => $quantity,
                            "variation"=>$variation,
                            "group_product"=>$group_product
                        ];
                    }
                    elseif (isset($user_cart["cart_item"][$product]) ) {

                        if($quantity!=1){
                            $user_cart["cart_item"][$product]["quantity"]+=$quantity;
                        } else{
                            $user_cart["cart_item"][$product]["quantity"]++;

                        }
                    }
                    Cart::updateOrCreate([
                        "user_id" => $user->id

                    ], ["cart_item" => $user_cart["cart_item"]]);
                } else{
                toastMessage("محصول مورد نظر در انبار موجود نیست","در انبار موجود نیست","info");

            }
        }
    }

    public static function add_cart_item_from_session_to_database()
    {
        if (auth()->check()) {
            $cartItem = session()->get("cart");

            $userCartItem = auth()->user()->cart ? auth()->user()->cart->toArray() : null;

            if (!empty($cartItem)) {

                foreach ($cartItem as $id => $value) {
                    if (isset($userCartItem["cart_item"][$id])) {
                        $userCartItem["cart_item"][$id]["quantity"] += $cartItem[$id]["quantity"];
                    } else {
                        $userCartItem["cart_item"][$id] = [
                            "id" => $id,
                            "quantity" => $cartItem[$id]["quantity"],
                            "variation" => $cartItem[$id]["variation"],
                        ];
                    }
                }
                if (auth()->user()->cart) {
                    auth()->user()->cart->delete();
                }
                Cart::create([
                    "user_id" => $user->id,
                    "cart_item" => $userCartItem["cart_item"]
                ]);
                session()->forget("cart");
            }
        }
    }


    public static function get_cart($user)
    {
        if($user){
            if($user->cart){
            return $user->cart->cart_item;
            }
        } else{
            return session()->get("cart");
        }
    }


    public static function delete_cart_item($id,$user)
    {
        if($user){
            if($user->cart) {
                $cart = $user->cart->cart_item;
                unset($cart[$id]);
                $user->cart->update([
                    "cart_item" => $cart,

                ]);
            }
        }  else{

            $cart=session()->get("cart");
            if($cart){
                unset($cart[$id]);

                session()->forget("cart");
                session()->put("cart",$cart);

            }
        }

    }


    public static function increase_cart($id,$variation=null,$user=null)
    {

        if(ProductRepository::in_stock($id,1,$variation)) {

            if ($user) {
                if ($user->cart) {
                    $cart = $user->cart->cart_item;
                    $cart[$id]["quantity"]++;

                    $user->cart->update([
                        "cart_item" => $cart,

                    ]);
                }
            } else {
                $cart = session()->get("cart");
                if ($cart) {
                    $cart[$id]["quantity"]++;

                    session()->forget("cart");
                    session()->put("cart", $cart);

                }
                return  true;
            }
        } else{
            toastMessage("محصول مورد نظر در انبار موجود نیست","در انبار موجود نیست","info");
            return false;
        }
    }

    public static function decrease_cart($id,$user)
    {
        if($user){
            if($user->cart) {
                $cart = $user->cart->cart_item;
                if ($cart[$id]["quantity"] == 1) {
                    unset($cart[$id]);
                } else {
                    $cart[$id]["quantity"]--;
                }
                $user->cart->update([
                    "cart_item" => $cart,

                ]);
            }
        } else{

        $cart=session()->get("cart");
            if($cart){
                if($cart[$id]["quantity"]==1){
                    unset($cart[$id]);
                } else{
                    $cart[$id]["quantity"]--;
                }

                session()->forget("cart");
                session()->put("cart",$cart);

            }
        }
    }

}
