<?php
namespace App\Services;

use App\Models\HomepartyUser;
use App\Models\Product;
use App\Models\ShippingCountry;
use \Gloudemans\Shoppingcart\Cart;
use Gloudemans\Shoppingcart\CartItem;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Illuminate\Session\SessionManager;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\Collection;


class HomepartyUserCart
{
    /*private $shipping_price = 0;
    private $shipping_price_net = 0;
    private $shipping_tax_rate = 0;
    private $shipping_tax = 0;
    private $shipping_country_id = 0; //default de
    private $shipping_is_for;
    private $num_comp;
    private $ysession;
    private $yinstance;
    private $shopping_data = [];*/


    public $points;
    public $price;
    public $price_net;
    public $ek_price;
    public $income_price;

    public $weight;
    public $shipping_weight;


    public $shipping_price;
    public $shipping_tax_rate;
    public $shipping_price_net;
    public $shipping_tax;



    private $homepartyUser;

    public function __construct(HomepartyUser $homepartyUser)
    {
            $this->homepartyUser = $homepartyUser;
            //is all total
            $this->points = 0;
            $this->price = 0;
            $this->price_net = 0;
            $this->ek_price = 0;
            $this->income_price = 0;
            $this->weight = 0;
            $this->shipping_weight = 0;
            $this->shipping_price = 0;
            $this->shipping_tax_rate = 0;
            $this->shipping_price_net = 0;
            $this->shipping_tax = 0;
            $this->calculateUserCart();
    }

    public function calculateUserCart(){

        foreach ($this->homepartyUser->homeparty_user_order_items as $order_item) {
            $this->points += $order_item->getTotalPoints();
            $this->price += $order_item->getTotalPrice();
            $this->price_net += $order_item->geTotalPriceNet();
            $this->ek_price += $order_item->geTotalEKPrice();
            $this->income_price += $order_item->geTotalIncomePrice();
            $this->weight += ($order_item->product->weight * $order_item->qty);
        }
    }


    public function getFormattedPoints()
    {
        return formatNumber($this->points, 0);
    }

    public function getFormattedPrice()
    {
        return formatNumber($this->price);
    }

    public function getFormattedPriceNet()
    {
        return formatNumber($this->price_net);
    }

    public function getFormattedEkPrice()
    {
        return formatNumber($this->ek_price);
    }

    public function getFormattedIncomePrice()
    {
        return formatNumber($this->income_price);
    }

    public function getFormattedShippingPrice()
    {
        return formatNumber($this->shipping_price);
    }

    protected function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
    {
        if(is_null($decimals)){
            $decimals = is_null(config('cart.format.decimals')) ? 2 : config('cart.format.decimals');
        }
        if(is_null($decimalPoint)){
            $decimalPoint = is_null(config('cart.format.decimal_point')) ? '.' : config('cart.format.decimal_point');
        }
        if(is_null($thousandSeperator)){
            $thousandSeperator = is_null(config('cart.format.thousand_seperator')) ? ',' : config('cart.format.thousand_seperator');
        }

        return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
    }
}