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.OrderCart;
import com.bonus.canteen.core.order.domain.param.OrderCartQueryParam;
import com.bonus.common.core.exception.ServiceException;

import java.util.List;

public class OrderCartParamChecker {
    public static void listCheck(OrderCartQueryParam orderCartQueryParam) {
        checkOrderShoppingCart(orderCartQueryParam);
        checkCanteenId(orderCartQueryParam.getCanteenId());
        checkStallId(orderCartQueryParam.getStallId());
        checkOrderType(orderCartQueryParam.getOrderType());
        checkMenuDateList(orderCartQueryParam.getMenuDateList());
    }
    public static void addCheck(OrderCart orderCart) {
        checkOrderShoppingCart(orderCart);
        checkUserId(orderCart);
        checkGoodsId(orderCart);
        checkQuantity(orderCart);
        checkCanteenId(orderCart.getCanteenId());
        checkStallId(orderCart.getStallId());
        checkOrderDate(orderCart);
        checkMealtimeType(orderCart);
        checkDetailType(orderCart);
        checkMenuId(orderCart);
    }

    public static void editCheck(OrderCart orderCart) {
        checkOrderShoppingCart(orderCart);
        checkCartId(orderCart);
        checkQuantity(orderCart);
    }

    private static void checkOrderShoppingCart(Object orderCart) {
        if (orderCart == null) {
            throw new ServiceException("订单购物车对象为空");
        }
    }
    private static void checkCartId(OrderCart orderCart) {
        if (orderCart.getCartId() == null) {
            throw new ServiceException("购物车ID为空");
        }
    }

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

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

    private static void checkQuantity(OrderCart orderCart) {
        if (orderCart.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(OrderCart orderCart) {
        if (orderCart.getOrderDate() == null) {
            throw new ServiceException("订单日期为空");
        }
    }

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

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

    private static void checkMenuId(OrderCart orderCart) {
        if (orderCart.getRecipeId() == 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("菜谱日期列表为空");
        }
    }
}
