package com.tax.file.utils.token;

import com.tax.file.model.FileServer;
import com.tax.file.model.TkTaxUser;
import com.tax.file.model.token.TokenInfoModel;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.http.HttpException;

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

public class GetTokenFromTaxBureau extends AbstractTokenService {

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

    public static  GetTokenFromTaxBureau getInstance(){
        return SingletonHolder.instance;
    }
    @Override
    protected TokenInfoModel getResult(Map<String, Object> retMap) {

        TokenInfoModel tokenModel = new TokenInfoModel();

        String token = null;
        String tokenCode = String.valueOf(retMap.get("code"));

        if(tokenCode.equals("0") && null != retMap.get("token")) {
            tokenCode = String.valueOf(retMap.get("token"));
        }
        tokenModel.setToken(token);
        tokenModel.setTokenCode(tokenCode);

        return tokenModel;
    }

    @Override
    protected ResponseContent invoke(TkTaxUser tkTaxUser, FileServer fileServer) throws IOException, HttpException {
        Map<String, Object> paramsMap = new HashMap<String, Object>();
        String taxno = tkTaxUser.getTaxno();
        String dqbm = tkTaxUser.getZoneCode();
        String force = "true";
        paramsMap.put("sh", taxno);
        paramsMap.put("dqbm", dqbm);
        paramsMap.put("force", force);
        paramsMap.put("username", fileServer.getUsername());
        paramsMap.put("sign", MD5Util.MD5Encode(taxno + dqbm + force + fileServer.getUsername() + fileServer.getPassword()));
        return HttpHelper.postEntity(fileServer.getTokenUrl(), paramsMap);
    }
}
