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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.zm.order.tool.server.model.BakingIndexOrder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.zm.order.tool.server.dto.BasePerformanceDTO;
import com.zm.order.tool.server.dto.OrderPerformanceDTO;
import com.zm.order.tool.server.dto.QueryPerformanceDTO;
import com.zm.order.tool.server.enums.PerformanceTypeEnum;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.RetailIndexOrder;
import com.zm.util.dto.Result;

/**
 * 订单快递处理器
 * 
 * @Author chang.n
 * @ClassName PerformanceExpressHandle
 * @Date 2020/4/14 2:54 PM
 */
@Component
public class PerformanceExpressHandle implements RetailPerformanceHandler {

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

    private static final String MOBILE = "mobile";
    
    private static final String ORDER_ADDRESS = "orderAddress";

    /**
     * 预约配送开始时间
     */
    private static final String DELIVERY_START_AT = "reserve_delivery_start_at";

    /**
     * 预约配送结束时间
     */
    private static final String DELIVERY_END_AT = "reserve_delivery_end_at";

    @Override
    public String performanceType() {
        return PerformanceTypeEnum.EXPRESS.getType().toString();
    }

    @Override
    public Result createPerformance(OrderPerformanceDTO orderPerformanceDTO) {
        return Result.successNullData();
    }

    @Override
    public Result operatePerformance(OrderPerformanceDTO orderPerformanceDTO, Order order) {
        return Result.successNullData();
    }

    @Override
    public Result<List<BasePerformanceDTO>> queryPerformance(QueryPerformanceDTO performanceDTO) {
        return Result.successNullData();
    }

    @Override
    public void buildPerformanceInfo(RetailIndexOrder indexOrder, Order order) {
        try {
            if (!StringUtils.isEmpty(order.getPerformanceInfo())) {
                String performanceInfo = order.getPerformanceInfo();
                Map<String, Object> performanceMap = JSON.parseObject(performanceInfo);
                if (Objects.nonNull(performanceMap.get(ORDER_ADDRESS))) {
                    Map<String, Object> orderAddressMap = JSON.parseObject(performanceMap.get(ORDER_ADDRESS).toString());
                    if (!StringUtils.isEmpty(orderAddressMap.get(MOBILE))) {
                        Object mobile = orderAddressMap.get(MOBILE);
                        indexOrder.setDeliveryMobile(mobile.toString());
                    }
                }
                if (!StringUtils.isEmpty(performanceMap.get(DELIVERY_START_AT))
                        && !StringUtils.isEmpty(performanceMap.get(DELIVERY_END_AT))) {
                    Object deliveryStartAt = performanceMap.get(DELIVERY_START_AT);
                    Object deliveryEndAt = performanceMap.get(DELIVERY_END_AT);
                    if (deliveryStartAt instanceof String && deliveryEndAt instanceof String) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        indexOrder.setReserveDeliveryAtStart(sdf.parse(deliveryStartAt.toString()));
                        indexOrder.setReserveDeliveryAtEnd(sdf.parse(deliveryEndAt.toString()));
                    } else {
                        Long deliveryStart = Long.valueOf(deliveryStartAt.toString());
                        Long deliveryEnd = Long.valueOf(deliveryEndAt.toString());
                        indexOrder.setReserveDeliveryAtStart(new Date(deliveryStart));
                        indexOrder.setReserveDeliveryAtEnd(new Date(deliveryEnd));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(
                "PerformanceExpressHandle[buildBaseInsertInfo]get performanceInfo failed,request:{},cause by:{}",
                order, e);
            throw new RuntimeException("查询履约信息异常");
        }
    }

    @Override
    public void buildPerformanceIndex(BakingIndexOrder indexOrder, Order order) {

    }

}
