package cn.freemud.management.service.adapter;

import cn.freemud.base.entity.BaseResponse;
import cn.freemud.management.entities.dto.request.order.OrderManagerRequest;
import cn.freemud.management.entities.dto.response.order.OrderManagerResponse;
import cn.freemud.management.enums.OperateType;
import cn.freemud.management.enums.OrderStatus;
import cn.freemud.management.enums.RefundStatus;
import cn.freemud.management.enums.ResponseResult;
import cn.freemud.management.service.OrderBaseService;
import cn.freemud.management.service.impl.OrderVerifyService;
import cn.freemud.management.util.ResponseUtil;
import com.freemud.application.sdk.api.ordercenter.enums.OrderType;
import com.freemud.sdk.api.assortment.order.enums.OldOrderType;
import com.freemud.sdk.api.assortment.order.response.order.QueryOrdersResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderManagerAdapter {

    @Autowired
    private OrderVerifyService orderVerifyService;
    @Autowired
    private OrderBaseService orderBaseService;
    @Autowired
    private OrderBeanFactory orderBeanFactory;


    /**
     * 接单
     *
     * @param request
     * @return
     */
    public BaseResponse<OrderManagerResponse> orderConfirmTake(OrderManagerRequest request) {
        //查询订单
        QueryOrdersResponse.DataBean.OrderBean orderBean = orderBaseService.getByOrderCode(request.getOrderId());
        //订单接单参数校验
        BaseResponse verifyResponse = orderVerifyService.orderConfirmTakeVerify(orderBean, request);
        if (ObjectUtils.notEqual(verifyResponse.getCode(), ResponseResult.SUCCESS.getCode())) {
            return verifyResponse;
        }
        return orderBeanFactory.getOrderManagerService(orderBean).orderConfirm(request, orderBean);
    }

    /**
     * 制作完成
     * @param request
     * @return
     */
    public BaseResponse<OrderManagerResponse> orderDone(OrderManagerRequest request) {
        //查询订单
        QueryOrdersResponse.DataBean.OrderBean orderBean = orderBaseService.getByOrderCode(request.getOrderId());
        //订单接单参数校验
        BaseResponse verifyResponse = orderVerifyService.orderDoneVerify(orderBean, request);
        if (ObjectUtils.notEqual(verifyResponse.getCode(), ResponseResult.SUCCESS.getCode())) {
            return handleResponse(request,orderBean,verifyResponse);
        }
        BaseResponse<OrderManagerResponse> orderManagerResponse = orderBeanFactory.getOrderManagerService(orderBean).orderDone(request, orderBean);
        return handleResponse(request,orderBean,orderManagerResponse);
    }

    /**
     *  送出
     * @param request
     * @return
     */
    public BaseResponse<OrderManagerResponse> orderDelivery(OrderManagerRequest request) {
        //查询订单
        QueryOrdersResponse.DataBean.OrderBean orderBean = orderBaseService.getByOrderCode(request.getOrderId());
        /*****兼容爱马哥，送出即完成start******/
        if (orderBean.getType().equals(OldOrderType.TAKE_OUT.getCode())){
            orderAffirm(request,orderBean);
        }
        /*****容爱马哥，送出即完成end******/
        //订单接单参数校验
        BaseResponse verifyResponse = orderVerifyService.orderDeliveryVerify(orderBean, request);
        if (ObjectUtils.notEqual(verifyResponse.getCode(), ResponseResult.SUCCESS.getCode())) {
            return StringUtils.equals(verifyResponse.getCode(), ResponseResult.ORDER_IS_COMPLETE_CAN_NOT_DELIVERY.getCode()) ? handleResponse(request,orderBean,ResponseUtil.success()) : handleResponse(request,orderBean,verifyResponse);
        }
        BaseResponse<OrderManagerResponse> orderManagerResponse = orderBeanFactory.getOrderManagerService(orderBean).orderDelivery(request, orderBean);
        return handleResponse(request,orderBean,orderManagerResponse);
    }

    /**
     * 完成
     * @param request
     * @return
     */
    public BaseResponse<OrderManagerResponse> orderAffirm(OrderManagerRequest request,QueryOrdersResponse.DataBean.OrderBean orderBean) {
        if (orderBean==null){
            //查询订单
            orderBean = orderBaseService.getByOrderCode(request.getOrderId());
        }
        //订单接单参数校验
        BaseResponse verifyResponse = orderVerifyService.orderAffirmVerify(orderBean, request);
        if (ObjectUtils.notEqual(verifyResponse.getCode(), ResponseResult.SUCCESS.getCode())) {
            return handleResponse(request,orderBean,verifyResponse);
        }
        BaseResponse<OrderManagerResponse> orderManagerResponse = orderBeanFactory.getOrderManagerService(orderBean).orderAffirm(request, orderBean);
        return handleResponse(request,orderBean,orderManagerResponse);
    }

    private BaseResponse<OrderManagerResponse> handleResponse(OrderManagerRequest request, QueryOrdersResponse.DataBean.OrderBean orderBean,BaseResponse result) {
        if (orderBean==null){
            return result;
        }
        RefundStatus refundStatus = CollectionUtils.isNotEmpty(orderBean.getRefundList()) ? RefundStatus.getByCode(orderBean.getRefundList().get(0).getStatus()) : null;
        OrderStatus orderStatus = OrderStatus.getByCode(orderBean.getStatus());
        if (ObjectUtils.equals(ResponseResult.SUCCESS.getCode(),result.getCode())){
            OperateType byOpType = OperateType.getByOpType(request.getOperateType());
            switch (byOpType) {
                case ORDER_CONFIRM:
                    orderStatus=OrderStatus.RECEIPT;
                    break;
                case ORDER_DONE:
                    orderStatus=OrderStatus.COMPLETE_MAKE;
                    break;
                case ORDER_SEND:
                    orderStatus=OrderStatus.DISTRIBUTION;
                    break;
                case ORDER_AFFIRM:
                    orderStatus=OrderStatus.COMPLETE;
                    break;
                default:
            }
            return ResponseUtil.success(new OrderManagerResponse(orderStatus,refundStatus));
        }
        result.setResult(new OrderManagerResponse(orderStatus,refundStatus));
        return result;
    }

    /**
     * 拒单
     *
     * @param request
     * @return
     */
    public BaseResponse orderReject(OrderManagerRequest request) {
        return null;
    }

}
