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

namespace Mamad\Product\Service;


use Mamad\Product\Repository\ProductRepository;
use Mamad\Product\Repository\ProductVariationRepository;
use Mamad\ProductAttr\Repository\ProductAttrRepository;

class ProductService
{
    public static $products=[];
    public static $product_quantity=[];
    public static $product_variation=[];
    public static function get_product_attribute_parent_and_value_id_and_name($attributes)
    {
        $attributeItem=[];
        foreach ($attributes as $attribute){
            $attributeItem[$attribute->id]["parent"]=$attribute->attribute->only("id","name");
            foreach($attribute->values as $value){
                $attributeItem[$attribute->id]["values"][]=ProductAttrRepository::find($value)->only("id","name");
            }

        }
        return $attributeItem;
    }


    public static function get_cart_products($cart)
    {
        if($cart){
            $product_id=array_keys($cart);
            $product_quantity=[];
            $product_variation=[];

            foreach ($cart as $cart_item_product_id=>$cart_item) {
                $product_quantity[$cart_item_product_id]=$cart_item["quantity"];
                $product_variation[$cart_item_product_id]=$cart_item["variation"];
            }
            $products=ProductRepository::get_multiple_product_prices($product_id);
            self::$products=$products;
            self::$product_variation=$product_variation;
            self::$product_quantity=$product_quantity;
            return $products;
        }

    }

    public static function get_cart_products_total_price($format=true,$product=null,$with_coupon=true,$variation=null)
    {
        if(!empty(session()->get("coupon_amount")) && $with_coupon){
            $amount=session()->get("coupon_amount");
            if($format){
            return format_price_with_currencySymbol($amount);

            }
            return $amount;
        } else{
            $products=self::$products;
            if($product){
                $products=$product;
            }

            $product_quantity=self::$product_quantity;
            $product_variation=self::$product_variation;

            if($products){
                $total_price=0;
                    foreach ($products as $product) {

                        if((!$variation && !$product_variation[$product->id]) || $product->type != $product->type_variable) {

                            $products_meta = $product->post_meta;
                            if (isset($products_meta[1])) {
                                $product_price = $products_meta[1]->meta_value;

                            } else {
                                $product_price = $products_meta[0]->meta_value;

                            }
                            $total_price+=$product_price * $product_quantity[$products_meta[0]->post_metaable_id];

                        }

                        else{

                            if(!$variation) $variation=$product_variation[$product->id];
                            $variation=ProductVariationRepository::find_variation($variation);
                            if(isset($variation->offer_price) && !empty($variation->offer_price)){
                                $product_price=$variation->offer_price;

                            } else{
                                $product_price=$variation->price;
                            }

                            dd($product);



                            $total_price+=$product_price * $product_quantity[$variation->product_id];
                        }

                    }

                if($format){
                    return format_price_with_currencySymbol($total_price);

                } else{
                    return  $total_price;
                }
                }

            }
            return null;
    }


    public static function get_cart_products_offer_price($variation=null){
        $products=self::$products;
        $product_quantity=self::$product_quantity;
        $product_variation=self::$product_variation;
        if($products){
            $total_offer_price=0;
            foreach ($products as $product) {
                if((!$variation && !$product_variation[$product->id]) || $product->type != $product->type_variable) {
                    $products_meta = $product->post_meta;
                    if (isset($products_meta[1])) {
                        $offer_price = $products_meta[1]->meta_value;
                        $price = $products_meta[0]->meta_value;
                        $new_price = ($price - $offer_price) * $product_quantity[$products_meta[0]->post_metaable_id];
                        $total_offer_price += $new_price;

                    }
                } else{
                    if(!$variation) $variation=$product_variation[$product->id];
                    $variation=ProductVariationRepository::find_variation($variation);
                    if(isset($variation->offer_price) && !empty($variation->offer_price)){
                        $offer_price = $variation->offer_price;
                        $price = $variation->price;
                        $new_price = ($price - $offer_price) * $product_quantity[$variation->product_id];
                        $total_offer_price += $new_price;
                    }
                }


            }
                return format_price_with_currencySymbol($total_offer_price);
        }

    }

    public static function get_cart_products_total_number()
    {
        $product_quantity=self::$product_quantity;

        if(!empty($product_quantity)){
        return array_sum($product_quantity);

        } return 0;

    }

    public static function if_product_in_stock_decrease_stock_number($products,$cart=null){
        foreach ($products as $cart_product){
            $quantity=$cart[$cart_product->id]["quantity"];

            if(ProductRepository::in_stock($cart_product,$quantity,$cart[$cart_product->id]["variation"])){
                ProductRepository::decrease_stock_number($cart_product,$quantity,$cart[$cart_product->id]["variation"]);
            } else{
                $message="در انبار موجود نیست".$quantity."به تعداد: ".$cart_product->title."محصول : ";
                toastMessage($message,"در انبار موجود نیست" ,"info");
                return redirect()->route("cart.index");
            }
        }
    }
}
