package io.github.zutherb.appstash.shop.service.order.model;

import io.github.zutherb.appstash.shop.service.user.model.UserInfo;
import io.github.zutherb.appstash.shop.service.user.model.UserInfo;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author zutherb
 */
public class OrderInfo implements Serializable {

    private Long orderId;
    private UserInfo user;
    private List<OrderItemInfo> orderItems;
    private DeliveryAddressInfo deliveryAddress;
    private Date orderDate;
    private String sessionId;

    private OrderInfo() {
    }

    public OrderInfo(UserInfo user, DeliveryAddressInfo deliveryAddress, List<OrderItemInfo> orderItems, String sessionId) {
        this.user = user;
        this.orderItems = orderItems;
        this.deliveryAddress = deliveryAddress;
        this.sessionId = sessionId;
    }

    public OrderInfo(Long orderId, String sessionId, OrderInfo orderInfo) {
        this.orderId = orderId;
        this.sessionId = sessionId;
        this.user = orderInfo.getUser();
        this.orderItems = orderInfo.getOrderItems();
        this.deliveryAddress = orderInfo.getDeliveryAddress();
    }

    public OrderInfo(Long orderId, OrderInfo orderInfo, List<OrderItemInfo> orderItemInfos, String sessionId) {
        this(orderId, sessionId, orderInfo);
        this.orderItems = orderItemInfos;
        this.sessionId = sessionId;
    }

    public String getSessionId() {
        return sessionId;
    }

    public UserInfo getUser() {
        return user;
    }

    public void setUser(UserInfo user) {
        this.user = user;
    }

    public List<OrderItemInfo> getOrderItems() {
        if (orderItems == null) {
            orderItems = new ArrayList<>();
        }
        return Collections.unmodifiableList(orderItems);
    }

    public DeliveryAddressInfo getDeliveryAddress() {
        return deliveryAddress;
    }

    public BigDecimal getDiscountSum() {
        BigDecimal totalSum = BigDecimal.ZERO;
        for (OrderItemInfo orderItemInfo : orderItems) {
            totalSum = totalSum.add(orderItemInfo.getTotalSum());
        }
        double fraction = 0.25/100;
        return totalSum.multiply(new BigDecimal(fraction));
    }

    public BigDecimal getTotalSum() {
        BigDecimal totalSum = BigDecimal.ZERO;
        for (OrderItemInfo orderItemInfo : orderItems) {
            totalSum = totalSum.add(orderItemInfo.getTotalSum());
        }
        // double fraction = 0.25/100;        
        // return totalSum.subtract(totalSum.multiply(new BigDecimal(fraction)));
        
        return totalSum;
    }
    
    
    public Long getOrderId() {
        return orderId;
    }

    public Date getOrderDate() {
        return orderDate;
    }
}

@@ -2239,26 +2239,24 @@
  fraction = 
-0.
 25/100;%0A    
@@ -2569,10 +2569,8 @@
 n = 
-0.
 25/1

Update OrderInfo.java