package com.style24.core.biz.thirdparty;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;

import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import lombok.extern.slf4j.Slf4j;

@Component
@Slf4j
public class Yes24Giftcard {

	private String apiUrl = "http://api.yes24.com/Yes24GiftTicketAPI/Yes24GiftTicketService.asmx";

	public String soapDataTransfer(String pubNo, String ip, Integer userId) throws Exception {

		// request SOAP message DOMSource create
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		stringBuilder.append("    <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/\">");
		stringBuilder.append("        <soap:Body>");
		stringBuilder.append("            <Approval xmlns=\"http://tempuri.org/\">");
		stringBuilder.append("                <ticPubNo>");
		stringBuilder.append(pubNo);
		stringBuilder.append("                </ticPubNo>");
		stringBuilder.append("                <ipAddress>");
		stringBuilder.append(ip);
		stringBuilder.append("                </ipAddress>");
		stringBuilder.append("                <iStyle24UserID>");
		stringBuilder.append(userId);
		stringBuilder.append("                </iStyle24UserID>");
		stringBuilder.append("            </Approval>");
		stringBuilder.append("        </soap:Body>");
		stringBuilder.append("     </soap:Envelope>");

		URL url = new URL(apiUrl);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();

		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.addRequestProperty("Content-Type", "text/xml");
		OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
		wr.write(stringBuilder.toString());
		wr.flush();

		String inputLine = null;
		StringBuilder buffer = new StringBuilder();
		BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
		while ((inputLine = in.readLine()) != null) {
			buffer.append(inputLine);
		}

		log.info("Result Message ===> {}", buffer.toString());

		// xml 파싱하기
		InputSource is = new InputSource(new StringReader(buffer.toString()));
		Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);

		String childnodevalue = "";
		if (document.getFirstChild().getChildNodes().getLength() > 0) {
			String getNodeName = document.getFirstChild().getFirstChild().getNodeName();
			NodeList nodeList = document.getElementsByTagName(getNodeName);
			for (int i = 0; i < nodeList.getLength(); i++) {
				NodeList childList = nodeList.item(i).getChildNodes();
				for (int j = 0; j < childList.getLength(); j++) {
					childnodevalue = childList.item(j).getTextContent();
				}
			}
		}
		return childnodevalue;
	}

}
