package com.tax.token.thirdService;

import com.tax.token.model.TkServer;
import com.tax.token.model.TkZoneInfo;
import com.tax.token.model.TkToken;
import com.tax.token.thirdService.impl.GetTokenFromLocal;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public  class TokenThirdServiceFactory{
    private static final Logger logger = LoggerFactory.getLogger(TokenThirdServiceFactory.class);

    private static ConcurrentHashMap<String,TokenThirdServiceI>serviceMap = new ConcurrentHashMap<String,TokenThirdServiceI>();

    public static Map<String, Object> getToken(String uuIdFlag, TkToken tkToken, TkZoneInfo tkZoneInfo, TkServer tkServer,boolean isFirst) throws Exception {
        if("yongyou".equals(tkToken.getUserCode())){
            logger.info("taxno=" + tkToken.getTaxno() + "," +tkServer.toString());
        }
        return getService(tkServer).invoke(uuIdFlag, tkToken, tkZoneInfo, tkServer,isFirst);
    }

    private static TokenThirdServiceI getService(TkServer tkServer) throws Exception {
        String methodName = tkServer.getMethodName();
        TokenThirdServiceI thirdService = null;
        if(serviceMap.contains(methodName)){
            thirdService = serviceMap.get(methodName);
        }else{
            thirdService = (TokenThirdServiceI)Class.forName("com.tax.token.thirdService.impl." + tkServer.getMethodName()).newInstance();
            serviceMap.put(methodName,thirdService);
        }
        if(thirdService == null){
            thirdService = new GetTokenFromLocal();
        }
        return thirdService;
    }


    public static boolean isSuccess(Map<String, Object> result){
        return result!=null &&  "0".equals(result.get("code"));
    }


    public static String getToken(Map<String, Object> result){
        if(result!=null && null!=result.get("token")){
            return String.valueOf(result.get("token"));
        }else {
            return null;
        }
    }

    public static String getCode(Map<String, Object> result){
        if(result!=null){
            String code = String.valueOf(result.get("code"));
            return code;
        }else {
            return null;
        }
    }

    public static String getMessage(Map<String, Object> result){
        if(result!=null && null!=result.get("message")){
            String message = String.valueOf(result.get("message"));
            return message;
        }else {
            return null;
        }
    }

    public static String getTimes(Map<String, Object> result){
        if(result!=null && null!=result.get("times")){
            String times = String.valueOf(result.get("times"));
            return times;
        }else {
            return null;
        }
    }

    public static String getProxyIp(Map<String, Object> result){
        if(result!=null && null!=result.get("proxyip")){
            String proxyIp = String.valueOf(result.get("proxyip"));
            return proxyIp;
        }else {
            return null;
        }
    }

    public static String getUserName(Map<String, Object> result){
        if(result!=null && null!=result.get("userName")){
            String userName = String.valueOf(result.get("userName"));
            return userName;
        }else {
            return null;
        }
    }

    public static String getRequestId(Map<String, Object> result){
        if(result!=null && null!=result.get("requestId")){
            String requestId = String.valueOf(result.get("requestId"));
            return requestId;
        }else {
            return null;
        }
    }

    public static String getSession(Map<String, Object> result){
        if(result!=null && null!=result.get("session")){
            String session = String.valueOf(result.get("session"));
            return session;
        }else {
            return null;
        }
    }

    public static String getResContext(Map<String, Object> result){
        if(result!=null && null!=result.get("resContext")){
            String resContext = String.valueOf(result.get("resContext"));
            return resContext;
        }else {
            return null;
        }
    }
}
