package com.liquidnet.service.goblin.service.impl;

import com.liquidnet.commons.lang.util.CurrentUtil;
import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.PagedResult;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.goblin.dto.vo.*;
import com.liquidnet.service.goblin.service.IGoblinNftOrderAppService;
import com.liquidnet.service.goblin.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
@Slf4j
public class GoblinNftOrderAppServiceImpl implements IGoblinNftOrderAppService {

    @Autowired
    GoblinRedisUtils redisUtils;
    @Autowired
    GoblinMongoUtils mongoUtils;
    @Autowired
    QueueUtils queueUtils;
    @Autowired
    GoblinOrderUtils orderUtils;
    @Autowired
    private GoblinRedisUtils goblinRedisUtils;

    @Override
    public ResponseDto<PagedResult<GoblinNftOrderDetailsVo>> orderList(int page) {
        String uid = CurrentUtil.getCurrentUid();
        List<GoblinNftOrderDetailsVo> orderVoList = ObjectUtil.goblinNftOrderDetailsVoArrayList();
        List<String> orderIds;
        orderIds = redisUtils.getNftOrderList(uid);

        int size = 10;
        int finalCount = orderIds.size() - page * size;
        int initCount = orderIds.size() - (page - 1) * size - 1;
        if (finalCount <= 0) {
            finalCount = 0;
        }
        for (int i = initCount; i >= finalCount; i--) {
            String orderId = orderIds.get(i);
            // 订单信息
            GoblinNftOrderVo orderVo = redisUtils.getGoblinNftOrder(orderId);
            GoblinNftOrderDetailsVo orderDetails = GoblinNftOrderDetailsVo.getNew().copy(orderVo);
            // 商品信息
            GoblinGoodsSkuInfoVo goodsSkuInfoVo = goblinRedisUtils.getGoodsSkuInfoVo(orderVo.getSkuId());
            GoblinNftOrderGoodsSkuInfoVo nftGoodsSkuInfoVo = GoblinNftOrderGoodsSkuInfoVo.getNew().copy(goodsSkuInfoVo);
            orderDetails.setGoodsSkuInfoVo(nftGoodsSkuInfoVo);
            // 加入列表
            orderVoList.add(orderDetails);
        }

        PagedResult<GoblinNftOrderDetailsVo> listVoPagedResult = ObjectUtil.getGoblinNftOrderDetailsVoPagedResult();
        listVoPagedResult.setList(orderVoList).setTotal(orderIds.size(), size).setPageSize(size).setCurrentPage(page);
        return ResponseDto.success(listVoPagedResult);
    }

    @Override
    public ResponseDto<GoblinNftOrderDetailsVo> orderDetails(String orderId) {
        GoblinNftOrderVo orderVo = redisUtils.getGoblinNftOrder(orderId);
        if (null == orderVo) {
            return ResponseDto.failure("订单不存在");
        }

        String uid = CurrentUtil.getCurrentUid();
        if (!orderVo.getUserId().equals(uid)) {
            return ResponseDto.failure(ErrorMapping.get("20003"));
        }
        // 订单信息
        GoblinNftOrderDetailsVo orderDetails = GoblinNftOrderDetailsVo.getNew().copy(orderVo);
        orderDetails.setOrderCode(orderDetails.getOrderCode().substring(orderDetails.getOrderCode().length() - 10));
        // 商品信息
        GoblinGoodsSkuInfoVo goodsSkuInfoVo = goblinRedisUtils.getGoodsSkuInfoVo(orderVo.getSkuId());
        GoblinNftOrderGoodsSkuInfoVo nftGoodsSkuInfoVo = GoblinNftOrderGoodsSkuInfoVo.getNew().copy(goodsSkuInfoVo);
        orderDetails.setGoodsSkuInfoVo(nftGoodsSkuInfoVo);

        return ResponseDto.success(orderDetails);
    }

    @Override
    public ResponseDto<GoblinPayOrderDetailsVo> payOrderDetails(String skuId) {
        String uid = CurrentUtil.getCurrentUid();
        String mobile = StringUtils.defaultString(((String) CurrentUtil.getTokenClaims().get(CurrentUtil.TOKEN_MOBILE)), "");
        LocalDateTime nowTime = LocalDateTime.now();
        HashMap<String, Object> map = goblinRedisUtils.getGoodsSkuInfoVo(nowTime, skuId);
        GoblinGoodsSkuInfoVo goodsSkuInfoVo = (GoblinGoodsSkuInfoVo) map.get("vo");
        Object tagType = map.get("tagType");
        String listId = (String) map.get("listId");
        Integer whiteType = (Integer) map.get("whiteType");

        if (goblinRedisUtils.getSkuAllStatusShow(goodsSkuInfoVo)) {
            GoblinPayOrderDetailsVo payOrderDetailsVo = GoblinPayOrderDetailsVo.getNew().copy(goodsSkuInfoVo);
            payOrderDetailsVo.setNum(1);

            GoblinStoreInfoVo storeInfoVo = goblinRedisUtils.getStoreInfoVo(goodsSkuInfoVo.getStoreId());
            payOrderDetailsVo.setStoreName(storeInfoVo.getStoreName());

            // 分批、提前购黑、白名单
            Integer isListCanBuy = 1;
            if (null != tagType) {
                Boolean listCanBuy = goblinRedisUtils.getListCanBuy(listId, skuId, mobile, uid, whiteType);
                if (!listCanBuy) {
                    isListCanBuy = 0;
                }
            }
            payOrderDetailsVo.setIsListCanBuy(isListCanBuy);

            return ResponseDto.success(payOrderDetailsVo);
        } else {
            return ResponseDto.failure("藏品不存在");
        }
    }

}
