package com.iformall.controller;

import com.iformall.common.ErrorCode;
import com.iformall.common.ResultData;
import com.iformall.domain.po.WxCUser;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxOrder;
import com.iformall.enums.EnumOrderPressStatus;
import com.iformall.enums.EnumOrderStatus;
import com.iformall.exception.MallinkException;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxOrderPressService;
import com.iformall.service.WxOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/press")
@Api(description = "砍价接口")
public class WxPressOrderController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxOrderService wxOrderService;

    @Autowired
    WxCouponService wxCouponService;

    @Autowired
    WxOrderPressService wxOrderPressService;

    // 砍价列表 - /api/wxCouponChannel/list
    // 我的砍价 - /api/order/pressOrderList
    // 砍价订单详情 -  /api/order/pressOrderDetail

    // 发起砍价 - /api/order/save

    // 砍价券详情 - /api/order/pressOrderDetail

    @ApiOperation(value = "参与砍价", notes = "{\"orderId\":\"String\"}")
    @PostMapping("pressOrderJoin")
    public ResultData pressOrderJoin(@RequestBody Map<String, String> paramMap) {
        Date curDate = new Date();
        String orderIdStr = paramMap.get("orderId");
        if (StringUtils.isBlank(orderIdStr)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
        }
        Long orderId = 0L;
        try {
            orderId = Long.valueOf(orderIdStr);
        } catch (NumberFormatException e) {
            logger.error("orderId convert error, " + orderIdStr + ", e:" + e.getMessage());
            return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "orderId: " + orderIdStr + ", e:" + e.getMessage());
        }
        WxOrder order = wxOrderService.getById(orderId);
        if(order == null) {
            logger.error("订单不存在:" + orderId);
            return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
        }
        WxCoupon coupon = wxCouponService.getById(order.getProductId());
        if(coupon == null) {
            logger.error("券不存在:" + order.getProductId());
            return new ResultData(ErrorCode.COUPON_IS_EMPTY);
        }

        if(curDate.after(order.getPressEndDate())) {
            logger.error("砍价已超时:" + orderId);
            return new ResultData(ErrorCode.COUPON_PRESS_IS_OVERTIME);
        }

        if(order.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode())) {
            logger.error("砍价已支付:" + orderId);
            return new ResultData(ErrorCode.ORDER_HAD_PAY);
        }

        if(order.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_PRESS_COMPLETE.getCode())) {
            logger.error("砍价已完成:" + orderId);
            return new ResultData(ErrorCode.ORDER_PRESS_IS_COMPLETE);
        }

        if(order.getOrderStatus().equals(EnumOrderStatus.ORDER_STATUS_PRESS_CANCEL.getCode())) {
            logger.error("砍价已取消:" + orderId);
            return new ResultData(ErrorCode.ORDER_HAD_CANCEL);
        }

        if(order.getPressCurrentNum() + 1 > coupon.getPressLimitNum()) {
            logger.error("砍价已结束:" + orderId);
            return new ResultData(ErrorCode.COUPON_PRESS_HAD_FINISHED);
        }

        try {
            wxOrderPressService.pressCouponJoin(order, coupon, getUserId());
        } catch (MallinkException e) {
            logger.error(e.getMessage());
            return new ResultData(e.getErrorCode(), e.getMessage());
        }

        return new ResultData();
    }

    @ApiOperation(value = "砍价状态(1：我发起的砍价，2：未参与的砍价, 3:已参与的砍价)", notes = "")
    @GetMapping("getPressOrderStatus")
    @ApiImplicitParam(name = "orderId", value = "orderId", dataType = "String", paramType = "query", required = true)
    public ResultData getPressOrderStatus(String orderId) {
        Map ret = new HashMap();
        if (StringUtils.isBlank(orderId)) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL.getCode(), "orderId不能为空");
        }
        Long orderIdL = 0L;
        try {
            orderIdL = Long.valueOf(orderId);
        } catch (NumberFormatException e) {
            logger.error("orderId convert error, " + orderId + ", e:" + e.getMessage());
            return new ResultData(ErrorCode.SYS_PARAMETER_CAST_ERROR.getCode(), "orderId: " + orderId + ", e:" + e.getMessage());
        }
        WxOrder order = wxOrderService.getById(orderIdL);
        if(order == null) {
            logger.error("订单不存在:" + orderId);
            return new ResultData(ErrorCode.ORDER_IS_NOT_FIND);
        }
        if(order.getCUserId().equals(getUserId())) {
            ret.put("status", EnumOrderPressStatus.FIRST.getCode());
            return new ResultData(ret);
        }
        int status = wxOrderPressService.pressCouponStatus(order, getUserId());
        ret.put("status", status);
        return new ResultData(ret);
    }

    /// 砍价 - 统计 相关 ？？？？？？


}
