package com.tax.file.utils.token;

import com.tax.file.model.FileServer;
import com.tax.file.model.TkTaxUser;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

public class GetTokenFromLocal extends AbstractTokenService {

    protected static final Logger logger = LoggerFactory.getLogger(GetTokenFromLocal.class);

    private GetTokenFromLocal(){}

    public static class SingletonHolder{
        private static GetTokenFromLocal instance = new GetTokenFromLocal();
    }

    public static  GetTokenFromLocal getInstance(){
        return SingletonHolder.instance;
    }

    @Override
    protected String getResult(Map<String, Object> retMap) {
        String tokenCode = null;
        String retCode = String.valueOf(retMap.get("errorCode"));

        if (retCode.equals("0") && null != retMap.get("data")) {
            tokenCode = String.valueOf(retMap.get("data"));
        }
        return tokenCode;
    }

    @Override
    protected ResponseContent invoke(TkTaxUser tkTaxUser, FileServer fileServer) throws IOException, HttpException {
        String dqbm = tkTaxUser.getZoneCode();
        String taxno = tkTaxUser.getTaxno();
        String username = tkTaxUser.getUserName();
        logger.info("GetTokenFromLocal " + fileServer.getTokenUrl() + "?zoneCode=" + dqbm + "&taxno=" + taxno + "&userCode=" + username);

        return HttpHelper
                .getUrlRespContent(fileServer.getTokenUrl() + "?zoneCode=" + dqbm + "&taxno=" + taxno + "&userCode=" + username);
    }
}
