package com.zm.order.tool.server.handler;

import com.alibaba.fastjson.JSON;
import com.github.dozermapper.core.Mapper;
import com.zm.order.tool.server.dao.OrderCommentDetailsDao;
import com.zm.order.tool.server.enums.BizTypeEnum;
import com.zm.order.tool.server.enums.CommentContentEnum;
import com.zm.order.tool.server.enums.CommentDetailsTypeEnum;
import com.zm.order.tool.server.model.*;
import com.zm.order.tool.server.newdatasource.NewDataSourceOrderDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 烘焙评价文档处理器
 *
 * @author nianchang
 * @date 2022/04/25 6:42 下午
 */
@Component
public class BakingCommentDocHandler implements CommentDocHandler {

    private static final Logger logger = LoggerFactory.getLogger(BakingCommentDocHandler.class);

    @Autowired
    private Mapper commentMapper;

    @Autowired
    private NewDataSourceOrderDao orderDao;

    @Autowired
    private OrderCommentDetailsDao detailsDao;

    @Override
    public Integer bizType() {
        return BizTypeEnum.BAKING.getType();
    }

    private static final String GOODS_NAME = "itemName";

    private static final String FROM = "from";

    private static final String ITEM_BARCODE = "itemBarcode";

    @Override
    public BaseIndexComment buildCommentDoc(OrderItemComment orderItemComment) {
        BakingIndexComment indexComment = new BakingIndexComment();
        try {
            indexComment = commentMapper.map(orderItemComment, BakingIndexComment.class);
            buildCommentBaseInfo(indexComment, orderItemComment);
            buildCommentDetailInfo(indexComment, orderItemComment);
            buildItemInfo(indexComment, orderItemComment);
        } catch (Throwable e) {
            logger.error("BakingCommentDocHandler[buildCommentDoc] failed!, cause by:{}", e);
        }
        return indexComment;
    }


    private void buildCommentBaseInfo(BakingIndexComment indexComment, OrderItemComment orderItemComment) {
        indexComment.setCommentId(orderItemComment.getId());

        Order order = orderDao.selectByOrderNo(orderItemComment.getOrderNo(), orderItemComment.getUserId());
        if (order != null) {
            buildCommentByOrderExtra(indexComment, order);
            indexComment.setMobile(order.getMobile());
            indexComment.setSource(order.getSource());
            indexComment.setOrderType(order.getOrderType());
            indexComment.setSourceNo(order.getSourceNo());
            indexComment.setPerformanceType(order.getPerformanceType());
            indexComment.setOrderAt(order.getOrderAt());
        }

        //文字
        if(!StringUtils.isEmpty(orderItemComment.getCommentInfo()) && StringUtils.isEmpty(orderItemComment.getCommentImage())){
            indexComment.setCommentContent(CommentContentEnum.TEXT.getType());
        }
        //图文
        if(!StringUtils.isEmpty(orderItemComment.getCommentInfo()) && !StringUtils.isEmpty(orderItemComment.getCommentImage())){
            indexComment.setCommentContent(CommentContentEnum.TEXT_ANG_IMAGE.getType());
        }
        //无内容
        if(StringUtils.isEmpty(orderItemComment.getCommentInfo()) && StringUtils.isEmpty(orderItemComment.getCommentImage())){
            indexComment.setCommentContent(CommentContentEnum.NULL.getType());
        }
    }

    private void buildCommentByOrderExtra(BakingIndexComment indexComment, Order order) {
        Map<String, Object> extraMap = order.getExtraMap();
        if (!CollectionUtils.isEmpty(extraMap)) {
            if (Objects.nonNull(extraMap.get(FROM))) {
                indexComment.setGoodsOrderSource(Integer.valueOf(extraMap.get(FROM).toString()));
            }
        }
    }

    private void buildCommentDetailInfo(BakingIndexComment indexComment, OrderItemComment orderItemComment) {
        List<OrderCommentDetails> orderCommentDetails = detailsDao.selectByOrderNo(orderItemComment.getOrderNo(), orderItemComment.getSellerId());
        if (CollectionUtils.isEmpty(orderCommentDetails)) {
            return;
        }
        for (OrderCommentDetails details : orderCommentDetails) {
            CommentDetailsTypeEnum typeEnum = CommentDetailsTypeEnum.getValue(details.getCommentType());
            switch (typeEnum) {
                case TASTES_SCORE:
                    indexComment.setTasteScore(details.getCommentScore());
                    break;
                case SERVE_SCORE:
                    indexComment.setServerScore(details.getCommentScore());
                    break;
                case ENV_SCORE:
                    indexComment.setEnvScore(details.getCommentScore());
                    break;
                case DELIVERY_SCORE:
                    indexComment.setDeliveryScore(details.getCommentScore());
                    break;
                case PACK_SCORE:
                    indexComment.setPackScore(details.getCommentScore());
                    break;
                default:
                    break;
            }
        }
    }

    private void buildItemInfo(BakingIndexComment indexComment, OrderItemComment orderItemComment) {
        String itemInfo = orderItemComment.getItemInfo();
        if (!StringUtils.isEmpty(itemInfo)) {
            Map<String, Object> itemInfoMap = JSON.parseObject(itemInfo, Map.class);
            if (Objects.nonNull(itemInfoMap.get(GOODS_NAME))) {
                indexComment.setGoodsName(itemInfoMap.get(GOODS_NAME).toString());
            }
            if (Objects.nonNull(itemInfoMap.get(ITEM_BARCODE))) {
                indexComment.setItemBarcode(itemInfoMap.get(ITEM_BARCODE).toString());
            }
        }
    }
}
