package in.gov.digilocker.uidai.services.resources;

import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;

import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.util.encoders.Base64;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

public class Utilities {

	public static String getCurrentDateTimeISOFormat() {
		String cTime = null;
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
		String currentTime = sdf.format(dt);
		cTime = currentTime;
		return cTime;
	}

	public static String getUniqueTransactionId() {
		String uTime = null;
		Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmssSSS");
		String uniqueTime = sdf.format(dt);
		uTime = uniqueTime;
		return uTime;
	}

	public static String getXpathValue(XPath xPath, String RequestPath, Document doc) throws XPathExpressionException {
		String XpathValue = xPath.compile(RequestPath).evaluate(doc);
		xPath.reset();
		return XpathValue;
	}

	public static Document convertStringToDocument(String xmlStr) throws Exception {
		Document finalDoc = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));
		finalDoc = doc;
		return finalDoc;
	}

	public static String convertDocumentToString(Document doc) throws TransformerException {
		String finalOutput = null;
		TransformerFactory tf = TransformerFactory.newInstance();

		Transformer transformer = tf.newTransformer();
		StringWriter writer = new StringWriter();
		transformer.transform(new DOMSource(doc), new StreamResult(writer));
		String output = writer.getBuffer().toString();
		finalOutput = output;
		return finalOutput;
	}

	public static String convertDocumentToStringWithoutXMLHeader(Document doc) throws TransformerException {
		String finalOutput = null;
		TransformerFactory tf = TransformerFactory.newInstance();

		Transformer transformer = tf.newTransformer();
		StringWriter writer = new StringWriter();
		transformer.setOutputProperty("omit-xml-declaration", "yes");
		transformer.transform(new DOMSource(doc), new StreamResult(writer));
		String output = writer.getBuffer().toString();
		finalOutput = output;
		return finalOutput;
	}

	public static String sha256String(String myval) throws UnsupportedEncodingException {
		byte[] message = myval.getBytes();
		Digest digest = new SHA256Digest();
		digest.reset();
		byte[] output = new byte[digest.getDigestSize() - 1 + 1];
		digest.update(message, 0, message.length);
		digest.doFinal(output, 0);
		myval = new String(Base64.encode(output));
		return myval;
	}

	public static void checkProxy(boolean proxy, String host, String port) {
		if (proxy) {
			System.out.println("Proxy host:- "+host+ ", and port is:- " +port);
			System.setProperty("https.proxyHost", host);
			System.setProperty("https.proxyPort", port);
			System.setProperty("http.proxyHost", host);
			System.setProperty("http.proxyPort", port);
			System.setProperty("http.nonProxyHosts", host);
		}
	}
}
