package com.gamizi.util;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.io.SAXReader;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class DDFileUtil {
	public final String PLAYER_VERSION;
	public final String PROXY_URL;
	public final String PROXY_PROT;
	public final String INST_NOTI_URL;
	//public final String NEXT_URL;
	Element rootElement;//
	Element moInfoElement;
	Document document;
	XmlUtil xmlutil;
	
	/**
	 * »ý¼ºÀÚ
	 */
	public DDFileUtil(){
		

		//TODO ÇØ´ç Á¤º¸´Â Properties Á¤º¸·Î ¼³Á¤ ¿ä¸Á
		PLAYER_VERSION="001.000.000";
		PROXY_URL="211.255.17.10";
		PROXY_PROT="5002";
		INST_NOTI_URL="http://witoptest.sktelecom.com/WIPI/status";
		//NEXT_URL="http://58.102.122.181/";
		xmlutil = new XmlUtil();
	}
	

	
	/**
	 * ¸ÞÀÎ ÇÔ¼ö
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			DDFileUtil util = new DDFileUtil();
			Hashtable hm = new Hashtable();
			
			//ddÆÄÀÏ ÀÔ·ÂµÉ °æ·Î
			hm.put("ddInputFilePath", "C:/java_dev/eclipse/workspace/dd_generator/file/");
			//ddÆÄÀÏ ÀÔ·ÂµÉ ÆÄÀÏ¸í
			hm.put("ddInputFileName", "secu.dd_org");
			//smf ÆÄÀÏ URL Á¤º¸
			hm.put("smfFileDownLoadUrl", "http://211.255.17.10/mgz/test/wipi/secu.smf");
			//º¯°æµÈ ddÆÄÀÏ Ãâ·Â °æ·Î
			hm.put("ddOutputFilePath", "C:/java_dev/eclipse/workspace/wipi_proxy_apps/tools/SMF_DD/");
			//º¯°æµÈ ddÆÄÀÏ Ãâ·Â ÆÄÀÏ¸í
			hm.put("ddOutputFileName", "secu.dd");
			util.execute(hm);
			
		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	/**
	 * ½ÇÇà ¸Þ¼Òµå
	 * 1. DTD ÆÄÀÏÀ» »ý¼ºÇÑ´Ù.
	 * 2. dd ÆÄÀÏÀ» proxy server¿¡ ¸Â°Ô º¯°æÇÑ´Ù.
	 * 3. »õ·Î¿î dd ÆÄÀÏÀ» ÀÛ¼ºÇÑ´Ù.
	 * @param hm
	 * @throws Exception
	 */
	public void execute(Hashtable hm) throws Exception {

		String ddInputFilePath=(String) hm.get("ddInputFilePath");
		String ddInputFileName=(String) hm.get("ddInputFileName");
		String ddOutputFilePath=(String) hm.get("ddOutputFilePath");
		String ddOutputFileName=(String) hm.get("ddOutputFileName");
		makeFile(ddInputFilePath+"media.DTD","");
		
		
		document = xmlutil.parse(ddInputFilePath,ddInputFileName);
		
		rootElement =document.getDocumentElement();
		modifyDdFile(hm);
		
		xmlutil.makeFile(document, ddOutputFilePath+ddOutputFileName,"euc-kr","media.DTD");
	}	
	
	/**
	 * DDÆÄÀÏ º¯°æÇÑ´Ù.
	 * @param hm
	 * @throws Exception
	 */
	public void modifyDdFile(Hashtable hm) throws Exception{
		
		moInfoElement = xmlutil.getElement(rootElement,"mo_info");		
		xmlutil.addElementAttribute(moInfoElement,document,"proxy_url","data", PROXY_URL,true);
		xmlutil.addAttribute(moInfoElement, "down_url", (String) hm.get("smfFileDownLoadUrl"));
		xmlutil.addAttribute(xmlutil.getElement(moInfoElement,"player_version"), "data", PLAYER_VERSION);
		xmlutil.addElementAttribute(moInfoElement,document,"inst_noti_url","data", INST_NOTI_URL,true);	
		//xmlutil.addElementAttribute(moInfoElement,document,"next_url","data", NEXT_URL,true);
		xmlutil.addElementAttribute(moInfoElement,document,"proxy_port","data", PROXY_PROT,true);	
		
	}

	/**
	 * ÇØ´ç ÆÄÀÏ °æ·Î¿¡ content ³»¿ëÀ¸·Î ÆÄÀÏÀ» ÀÛ¼ºÇÑ´Ù.
	 * @param fname ÆÄÀÏ °æ·Î
	 * @param content ÆÄÀÏ ÀÛ¼º ³»¿ª
	 * @throws Exception
	 */
	public void makeFile(String fname, String content) throws Exception {		
		DataOutputStream myStream = new DataOutputStream(new FileOutputStream(fname));
		myStream.writeChars(content); //ÀÌºÎºÐ¿¡¼­ ÅØ½ºÆ®¸¦ ½á ÁÝ´Ï´Ù.		
		//myStream.writeUTF(content);
		//myStream.writeBytes(content);
		myStream.close();
	}
}
