package com.fitbank.uci.webservice.client;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.apache.axis.Message;
import org.apache.axis.client.Call;
import org.apache.axis.message.SOAPEnvelope;

import org.w3c.dom.NodeList;

public class SoapClient {

    private String urlService = null;
    private String nameSpace = null;
    private String soapAction = null;

    public SoapClient() {}

    public String getHomePinBlock(String numeroTarjeta, String pinEncriptado) {
        String result = null;

        try {
            Call call = new Call(this.urlService);

            call.setSOAPActionURI(this.nameSpace + this.soapAction);
            call.setUseSOAPAction(true);

            String soapString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><fnGeneraPinBlock xmlns=\"http://tempuri.org/WSHEPS/ServHeps\"><ptxtpin>" +
                            pinEncriptado + "</ptxtpin><ptxtpan>" + numeroTarjeta +
                            "</ptxtpan></fnGeneraPinBlock></soap:Body></soap:Envelope>";
            InputStream soapStream = new ByteArrayInputStream(soapString.getBytes());
            SOAPEnvelope soapEnvelope = new SOAPEnvelope(soapStream);

            call.invoke(soapEnvelope);

            Message msg = call.getResponseMessage();
            NodeList nodeList = msg.getSOAPPart().getElementsByTagName("fnGeneraPinBlockResult");

            result = nodeList.item(0).getChildNodes().item(0).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = null;
        }
        return result;
    }

    public String getOfficePinBlock(String ruc, String identifier, String pin) {
        String result = null;

        try {
            Call call = new Call(this.urlService);

            call.setSOAPActionURI(this.nameSpace + this.soapAction);
            call.setUseSOAPAction(true);

            String soapString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><fnGeneraPinBlockTarjeta xmlns=\"http://tempuri.org/WSHEPS/ServHeps\"><txtruc>" +
                            ruc + "</txtruc><txtusuario>" + identifier + "</txtusuario><txtpin>" + pin +
                            "</txtpin></fnGeneraPinBlockTarjeta></soap:Body></soap:Envelope>";
            InputStream soapStream = new ByteArrayInputStream(soapString.getBytes());
            SOAPEnvelope soapEnvelope = new SOAPEnvelope(soapStream);

            call.invoke(soapEnvelope);

            Message msg = call.getResponseMessage();
            //System.out.println(msg.getSOAPPartAsString());
            NodeList nodeList = msg.getSOAPPart().getElementsByTagName("fnGeneraPinBlockTarjetaResult");

            for (int i = 0; i < nodeList.getLength(); i++) {//System.out.println(nodeList.item(i));
            }
            result = nodeList.item(0).getChildNodes().item(0).toString();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = null;
        }
        return result;
    }

    public void setUrlService(String urlService) {
        this.urlService = urlService;
    }

    public void setNameSpace(String nameSpace) {
        this.nameSpace = nameSpace;
    }

    public void setSoapAction(String soapAction) {
        this.soapAction = soapAction;
    }

    public static void main(String[] args) {
        //StopWatch sw = new StopWatch();
        //sw.start();
        SoapClient soapClient = new SoapClient();

        soapClient.setUrlService("http://10.100.20.209/WSHEPS/ServHeps.asmx");
        soapClient.setNameSpace("http://tempuri.org/");
        soapClient.setSoapAction("WSHEPS/ServHeps/fnGeneraPinBlockTarjeta");

        String result = soapClient.getOfficePinBlock("20108068281", "10309673", "1903");

        //soapClient.setSoapAction("WSHEPS/ServHeps/fnGeneraPinBlock");
        //String result = soapClient.getHomePinBlock("4912490109967200", "2511");

        //sw.stop();
        //System.out.println(sw.getTime());
        System.out.println(result);
    }
}
