<?php
namespace App\Services;

use Yard;
use App\Models\Product;
use App\Models\ShippingCountry;


class UserCart
{
  
    public static $points = 0;
    public static $price = 0;
    public static $price_net = 0;
    public static $ek_price = 0;
    public static $income_price = 0;
    public static $max_qty= 100;

    public static $yard_margin;
    public static $yard_commission;


    
    public static function initYard($for='shop', $userShop = null, $promotionUser = null){

        $id = 1;
        if($ShippingCountry = ShippingCountry::all()->first()){
            $id = $ShippingCountry->id;
        }
      //  Yard::instance('shopping')->destroy();

        Yard::instance('shopping')->setShippingCountryWithPrice($id, $for); //'shop' or 'prom'
    }

    public static function getCurrentShippingPrice($shipping_for = 2) //shipping_for === 2 promotion , 3 shop
    {
        $shipping = Yard::instance('shopping')->shipping();
        if($shipping > 0){
            return $shipping;
        }
        $shipping = Yard::instance('shopping')->preCalcuShippingPrice();
        if($shipping > 0){
            return formatNumber($shipping);
        }
        if($ShippingCountry = ShippingCountry::all()->first()){
            if($ShippingCountry->shipping){
                if($ShippingPrices = $ShippingCountry->shipping->getShippingPricesFirstBy($shipping_for)){
                    return formatNumber($ShippingPrices->price);
                }
            }
        }
    }

    public static function clearCart($data)
    {
        Yard::instance('shopping')->destroy();
    }

    public static function switchShipping($data)
    {
        //pick_up//dhl_shipping
        Yard::instance('shopping')->setShippingOption($data['shipping_option']);
        Yard::instance('shopping')->reCalculateShippingPrice();

    }

    public static function changeStateShipping($data, $for='shop')
    {    
        $id = 1;
        if($ShippingCountry = ShippingCountry::find($data['shipping_country_id'])){
            $id = $ShippingCountry->id;
        }
        Yard::instance('shopping')->setShippingCountryWithPrice($id, $for); //'shop' or 'prom'
    }


    

    public static function updateProduct($data, $add=false, $minus=false)
    {
        if($product = Product::find($data['product_id'])){
            $image = "";
            if($product->images->count()){
                $image = $product->images->first()->slug;
            }

            //get the card item or add 1
            $cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, $product->price, $product->tax,
                [
                    'image' => $image,
                    'slug' => $product->slug,
                    'weight' => $product->weight,
                    'single_commission' => $product->single_commission,
                    'amount_commission' => $product->amount_commission,
                    'value_commission' => $product->value_commission,
                    'partner_commission' => $product->partner_commission,
                ]);
                
                Yard::setTax($cartItem->rowId, $product->tax);
            //remove one from Card or remove it
            if($minus){
               if(intval($cartItem->qty) <= 2){
                    Yard::instance('shopping')->update($cartItem->rowId, intval($cartItem->qty)-2);

                    //Yard::instance('shopping')->remove($cartItem->rowId);
               }else{
                    // > 1
                    Yard::instance('shopping')->update($cartItem->rowId, intval($cartItem->qty)-2);

               }
            }
            //update Card set product by qty or remove it
            if(!$add && !$minus){
                if(isset($data['qty'])  && $data['qty'] > 0){
                    Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);
                }else{
                    //if 0 get the item by qty:1 and remove it
                    Yard::instance('shopping')->update($cartItem->rowId, $data['qty']);

                    //Yard::instance('shopping')->remove($cartItem->rowId);
                }
            }
            //max 100
            if($cartItem->qty > self::$max_qty){
                Yard::instance('shopping')->update($cartItem->rowId, self::$max_qty);
            }
            Yard::instance('shopping')->reCalculate();
            return $cartItem->qty;
        }
    }

    public static function updateFeeProduct($data)
    {
        foreach (Yard::instance('shopping')->content() as $row) {
            //wenn kleiner wurde ein produkt entfernt aufgrund der Anzahl
            //wenn gleich löschen, da neue Versandkosten

            if($row->options->free_product_id) {
              Yard::instance('shopping')->remove($row->rowId);
            }
        }

        if(isset($data['free_product_id'])) {
              if ($product = Product::find($data['product_id'])) {
                  $image = "";
                  if ($product->images->count()) {
                      $image = $product->images->first()->slug;
                  }
                  $cartItem = Yard::instance('shopping')->add($product->id, $product->getLang('name'), 1, 0, 0,
                  [
                    'image' => $image,
                    'slug' => $product->slug,
                    //'weight' => $product->weight,
                    'weight' => 1,
                    'free_product_id' => intval($data['free_product_id']),
                    'product_id' => $product->id,
                    'single_commission' => 0,
                    'amount_commission' => 0,
                    'value_commission' => 0,
                    'partner_commission' => 0,
                  ]);
                  Yard::setTax($cartItem->rowId, 0);
                  Yard::instance('shopping')->reCalculate();

              }
          }
    }

}