package com.bonus.canteen.core.order.utils;

import cn.hutool.core.collection.CollUtil;
import com.bonus.canteen.core.order.constants.OrderDetailTypeEnum;
import com.bonus.canteen.core.order.constants.OrderTypeEnum;
import com.bonus.canteen.core.order.domain.OrderShoppingCart;
import com.bonus.canteen.core.order.domain.param.OrderShoppingCartQueryParam;
import com.bonus.common.core.exception.ServiceException;

import java.util.List;

public class ShoppingCartParamChecker {
    public static void listCheck(OrderShoppingCartQueryParam orderShoppingCart) {
        checkOrderShoppingCart(orderShoppingCart);
        checkCanteenId(orderShoppingCart.getCanteenId());
        checkStallId(orderShoppingCart.getStallId());
        checkOrderType(orderShoppingCart.getOrderType());
        checkMenuDateList(orderShoppingCart.getMenuDateList());
    }
    public static void addCheck(OrderShoppingCart orderShoppingCart) {
        checkOrderShoppingCart(orderShoppingCart);
        checkUserId(orderShoppingCart);
        checkGoodsId(orderShoppingCart);
        checkQuantity(orderShoppingCart);
        checkCanteenId(orderShoppingCart.getCanteenId());
        checkStallId(orderShoppingCart.getStallId());
        checkOrderDate(orderShoppingCart);
        checkMealtimeType(orderShoppingCart);
        checkDetailType(orderShoppingCart);
        checkMenuId(orderShoppingCart);
    }

    public static void editCheck(OrderShoppingCart orderShoppingCart) {
        checkOrderShoppingCart(orderShoppingCart);
        checkShoppingCartId(orderShoppingCart);
        checkQuantity(orderShoppingCart);
    }

    private static void checkOrderShoppingCart(Object orderShoppingCart) {
        if (orderShoppingCart == null) {
            throw new ServiceException("订单购物车对象为空");
        }
    }
    private static void checkShoppingCartId(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getShoppingCartId() == null) {
            throw new ServiceException("购物车ID为空");
        }
    }

    private static void checkUserId(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getUserId() == null) {
            throw new ServiceException("用户ID为空");
        }
    }

    private static void checkGoodsId(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getGoodsId() == null) {
            throw new ServiceException("餐品ID为空");
        }
    }

    private static void checkQuantity(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getQuantity() == null) {
            throw new ServiceException("餐品数量为空");
        }
    }

    private static void checkCanteenId(Long canteenId) {
        if (canteenId == null) {
            throw new ServiceException("食堂ID为空");
        }
    }

    private static void checkStallId(Long stallId) {
        if (stallId == null) {
            throw new ServiceException("档口ID为空");
        }
    }

    private static void checkOrderDate(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getOrderDate() == null) {
            throw new ServiceException("订单日期为空");
        }
    }

    private static void checkMealtimeType(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getMealtimeType() == null) {
            throw new ServiceException("餐次类型为空");
        }
    }

    private static void checkDetailType(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getDetailType() == null) {
            throw new ServiceException("订单明细类别为空");
        }
        if(!OrderDetailTypeEnum.isValidKey(orderShoppingCart.getDetailType())) {
            throw new ServiceException("订单明细类别错误");
        }
    }

    private static void checkMenuId(OrderShoppingCart orderShoppingCart) {
        if (orderShoppingCart.getMenuId() == null) {
            throw new ServiceException("菜谱ID为空");
        }
    }
    private static void checkOrderType(Integer orderType) {
        if (orderType == null) {
            throw new ServiceException("订单类型为空");
        }
        if(!OrderTypeEnum.isValidKey(orderType)) {
            throw new ServiceException("订单类型错误");
        }
    }

    private static void checkMenuDateList(List<String> menuDateList) {
        if (CollUtil.isEmpty(menuDateList)) {
            throw new ServiceException("菜谱日期列表为空");
        }
    }
}
