namespace NetinaShop.Domain.Entities.Orders;

public partial class Order
{
    public static Order Create(Guid userId)
    {
        return new Order(0, 0, 0, 0, 0, 0, 0, false, OrderStatus.OrderBag, DateTime.MinValue, DateTime.MinValue, 0, string.Empty, userId, OrderPaymentMethod.OnlinePayment);

    }

    public void AddToOrderBag(Product product, int count)
    {
        var orderProduct = OrderProducts.FirstOrDefault(op => op.ProductId == product.Id);
        if (orderProduct == null)
        {
            orderProduct = OrderProduct.Create(count, product.Cost,product.PackingCost, OrderStatus.OrderBag, product.Id,product.CategoryId, this.Id);
            OrderProducts.Add(orderProduct);
        }
        else
        {
            orderProduct.SetCount(count);
        }
    }
    public void RemoveFromOrderBag(Product product, int count)
    {
        var orderProduct = OrderProducts.FirstOrDefault(op => op.ProductId == product.Id);
        if (orderProduct != null)
        {
            orderProduct.SetCount(count);
            if (orderProduct.Count == 0)
                OrderProducts.Remove(orderProduct);
        }
    }

    public void SetDiscount(string discountCode)
        => DiscountCode = discountCode;
    public void AddOrderProduct(OrderProduct orderProduct)
        => OrderProducts.Add(orderProduct);

    public void SetSubmitOrder(OrderPaymentMethod paymentMethod)
    {
        switch (paymentMethod)
        {
            case OrderPaymentMethod.OnlinePayment:
                OrderStatus = OrderStatus.Paid;
                IsPayed = true;
                PayedAt = DateTime.Now;
                OrderAt = DateTime.Now;
                PaymentMethod = paymentMethod;
                break;
            case OrderPaymentMethod.CardTransfer:
                OrderStatus = OrderStatus.Submitted;
                IsPayed = false;
                OrderAt = DateTime.Now;
                PaymentMethod = paymentMethod;
                break;
            case OrderPaymentMethod.Cash:
                OrderStatus = OrderStatus.Submitted;
                IsPayed = false;
                OrderAt = DateTime.Now;
                PaymentMethod = paymentMethod;
                break;
        }
    }

    public void AddOrderDelivery(string address, string postalCode, string receiverPhoneNumber, string receiverFullName, double deliveryCost, Guid shippingId, Guid orderId)
    {
        var orderDelivery = OrderDelivery.Create(address, postalCode, receiverPhoneNumber, receiverFullName, deliveryCost, shippingId, orderId);
        OrderDeliveries.Add(orderDelivery);
    }

    public void SetTotalPrice(double totalProductPrice, 
        double packingPrice, 
        double servicePrice, 
        double deliveryPrice,
        double discountPrice, 
        double taxesPrice)
    {
        TotalProductsPrice = totalProductPrice;
        PackingPrice = packingPrice;
        ServicePrice = servicePrice;
        DeliveryPrice = deliveryPrice;
        TaxesPrice = taxesPrice;
        DiscountPrice = discountPrice;
        TotalPrice = (totalProductPrice + packingPrice + servicePrice + deliveryPrice + taxesPrice) - discountPrice;
    }

}

public partial class OrderProduct
{
    public static OrderProduct Create(int count, double productFee, double packingFee, OrderStatus orderProductStatus, Guid productId, Guid productCategoryId, Guid orderId)
    {
        var productCost = count * productFee;
        var packingCost = count * packingFee;
        return new OrderProduct(count, productFee, productCost,packingFee,packingCost, orderProductStatus, productId,productCategoryId, orderId);
    }

    public void SetCount(int count)
    {
        Count = count;
        var productCost = ProductFee * Count;
    }
}

public partial class OrderDelivery
{
    public static OrderDelivery Create(string address, string postalCode, string receiverPhoneNumber, string receiverFullName, double deliveryCost, Guid shippingId, Guid orderId)
    {
        return new OrderDelivery(address, postalCode, receiverPhoneNumber, receiverFullName, deliveryCost, shippingId, orderId);
    }
}