package com.fdkankan.platform.user.strategy;

import com.fdkankan.common.util.SpringUtil;
import com.fdkankan.platform.user.vo.ResponseOrderItem;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Log4j2
@Component
public class OrderStrategyFactory implements InitializingBean {

    @Autowired
    private SpringUtil applicationContextHelper;

    private Map<String, OrderStrategy> orderHandleMap = new ConcurrentHashMap<>();

    private Map<String, OrderItemStrategy> orderItemHandleMap = new ConcurrentHashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, OrderItemStrategy> orderItemHandles = applicationContextHelper.getBeansOfType(OrderItemStrategy.class);
        for (Map.Entry<String, OrderItemStrategy> orderHandle : orderItemHandles.entrySet()) {

            String type = orderHandle.getValue().getType();
            String[] types = type.split(",");
            if (types.length > 1){
                for (String str : types){
                    orderItemHandleMap
                            .put(str, orderHandle.getValue());
                }
            }
            else{
                orderItemHandleMap
                        .put(orderHandle.getValue().getType(), orderHandle.getValue());
            }
        }


        Map<String, OrderStrategy> orderHandles = applicationContextHelper.getBeansOfType(OrderStrategy.class);
        for (Map.Entry<String, OrderStrategy> orderHandle : orderHandles.entrySet()) {
            orderHandleMap.put(orderHandle.getValue().getType(), orderHandle.getValue());
        }
    }

    public void doHandler(Long userId, ResponseOrderItem t) throws Exception{
//        log.warn("userId:"+userId);
        log.warn("sku:"+t.getSkuSn());
//        log.warn("orderItemHandleMap:"+orderItemHandleMap);
//        log.warn(" orderHandleMap.get(type):"+ orderItemHandleMap.get(t.getSkuSn()));
        orderItemHandleMap.get(t.getSkuSn()).handleOrderItem(userId, t);
    }

    public void doHandler(String orderSn, String tradeNo, String openId, String type, int paymentTypeName) throws Exception{
//        log.warn("orderSn:"+orderSn);
//        log.warn("tradeNo:"+tradeNo);
//        log.warn("openId:"+openId);
//        log.warn("type:"+type);
//        log.warn("paymentTypeName:"+paymentTypeName);
//        log.warn("orderHandleMap:"+orderHandleMap);
//        log.warn(" orderHandleMap.get(type):"+ orderHandleMap.get(type));
        Long spaceId = null;
        log.info("进入doHandler");
        if(orderSn.split("_").length == 2){
            spaceId = Long.valueOf(orderSn.split("_")[1]);
            orderSn = orderSn.split("_")[0];
        }
        if(type.matches("^-?[0-9]+")){
            type = "expansion";
        }
        log.info("type:" + type);
        log.info("orderSn:" + orderSn);
        orderHandleMap.get(type).handleOrder(orderSn, tradeNo, openId, paymentTypeName, spaceId);
    }
}
