package com.fdkankan.platform.user.factory;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.common.util.SpringUtil;
import com.fdkankan.platform.order.request.RequestPlaceOrder;
import lombok.extern.slf4j.Slf4j;
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;

@Component
@Slf4j
public class PayFactory implements InitializingBean {

    @Autowired
    private SpringUtil applicationContextHelper;

    private Map<String, PayEntity> payMap = new ConcurrentHashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        Map<String, PayEntity> payHandles = applicationContextHelper.getBeansOfType(PayEntity.class);
        for (Map.Entry<String, PayEntity> payHandle : payHandles.entrySet()) {
            payMap.put(payHandle.getValue().getType(), payHandle.getValue());
        }
    }

    public JSONObject scanPay(String type, RequestPlaceOrder order) throws Exception {
        log.warn("scanPay:" + type + "_" + order.getOrderType() + "----------payMap:" + payMap.get(type + "_" + order.getOrderType()));
        return payMap.get(type + "_" + order.getOrderType()).scanPay(order);
    }

    public JSONObject h5Pay(String type, RequestPlaceOrder order, String ipAddr) throws Exception {
        return payMap.get(type + "_" + order.getOrderType()).h5Pay(order, ipAddr);
    }
}
