 package ibgroup.services;
 
 import ibgroup.securtity.auth.client.lib.SsoCombined;
 import ibgroup.Config;
 import ibgroup.http.DataIntercept;
 import io.vertx.core.AsyncResult;
 import io.vertx.core.Handler;
 import io.vertx.core.Vertx;
 import io.vertx.core.buffer.Buffer;
 import io.vertx.core.http.HttpClient;
 import io.vertx.core.http.HttpClientRequest;
 import io.vertx.core.http.HttpClientResponse;
 import io.vertx.core.http.HttpMethod;
 import io.vertx.core.http.HttpServerRequest;
 import io.vertx.core.logging.Logger;
 import io.vertx.core.logging.LoggerFactory;
 import java.math.BigInteger;
 import java.util.concurrent.TimeUnit;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class SsoService
   extends BaseServiceProxy
 {
   private static final Logger logger = LoggerFactory.getLogger(SsoService.class);
   private BigInteger K = BigInteger.ZERO;
   private final int ssoPing;
   
   public SsoService(Vertx vertx, HttpClient client, Config config) {
     super(vertx, client, config);
     this.ssoPing = config.getSsoPing();
   }
   
   public void complete(String B, String chlgRes) {
     String url = host() + ServiceEndPoints.SSODH_COMPLETE + "&B=" + B + "&CHRESPONSE=" + chlgRes;
     HttpClientRequest clientReq = this.proxy.getClient().requestAbs(HttpMethod.GET, url, res -> {
           logger.info("sso complete url status={}, url={}", new Object[] { Integer.valueOf(res.statusCode()), url });
 
           
           traceServerResponse(url, res);
           
           res.exceptionHandler(());
           
           res.handler(());
           
           res.endHandler(());
         });
     
     this.proxy.prepareRequest(clientReq);
     traceClientRequest("sso", url, clientReq);
     clientReq.end();
   }
   
   public BigInteger getK() {
     return this.K;
   }
 
   
   public void onTimerEvent(Long time) {
     tickleSession();
   }
   
   public void reset() {
     this.K = BigInteger.ZERO;
   }
   
   public void ssoDHInit() {
     stopTimer();
     startTimer(TimeUnit.MINUTES.toMillis(this.ssoPing));
     String url = host() + ServiceEndPoints.SSODH_INIT_URL;
     HttpClientRequest clientReq = this.proxy.getClient().requestAbs(HttpMethod.GET, url, res -> {
           logger.info("sso init status={}, url={}", new Object[] { Integer.valueOf(res.statusCode()), url });
           traceServerResponse(url, res);
           res.exceptionHandler(());
           res.handler(());
           res.endHandler(());
         });
     HttpClientRequest request = this.proxy.prepareRequest(clientReq);
     traceClientRequest("sso", url, clientReq);
     request.end();
   }
 
   
   String baseUrl() {
     return "sso";
   }
 
   
   void onProxyRequest(HttpServerRequest clientReq, Handler<HttpClientResponse> onEnd) {
     this.proxy.internalProxy(clientReq.uri(), clientReq, onEnd, new DataIntercept()
         {
           public String intercept(Buffer in)
           {
             String data = in.toString();
             data = data.replaceAll("http.*?\\/images", "\\/images");
             data = data.replaceAll("http.*?\\/scripts", "\\/scripts");
             data = data.replaceAll("http.*?\\/fonts", "\\/fonts");
             data = data.replaceAll("http.*?\\/stylesheet", "\\/stylesheet");
             return data;
           }
         });
   }
   
   private BigInteger extractBigInt(String data, String elem) {
     try {
       Pattern p = Pattern.compile("<" + elem + ">(.*)</" + elem + ">");
       Matcher m = p.matcher(data);
       if (m.find()) {
         return new BigInteger(m.group(1), 16);
       }
     } catch (Exception e) {
       logger.error("failed to extract bigint {}", new Object[] { elem });
     } 
     return null;
   }
   
   private void tickleSession() {
     this.proxy.get(ServiceEndPoints.SSO_PING, data -> logger.info("sso ping {}", new Object[] { data.result()
           
           })).end();
   }
 }
