package LP.SDK;

import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
 

import java.util.Map.Entry;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
 
public class TaskConfig {

	/// <summary>
    /// 任务运行名称
    /// </summary>
    public String TaskName = "";

    /// <summary>
    /// 任务脚本文件，不得为空
    /// </summary>
    public String TaskFile = "";

    /// <summary>
    /// 开发版SDK的Key
    /// </summary>
    public String SKey = "";

    /// <summary>
    /// 缓存文件目录,不指定系统自动创建
    /// </summary>
    public String CacheFiles = "";

    /// <summary>
    /// 浏览器窗口显示模式，0:默认大小,1:最小化,2:最大化
    /// </summary>
    public int WindowState = 0;

    /// <summary>
    /// 日志文件目录，空为不写日志
    /// </summary>
    public String LogFilePath = "";

    /// <summary>
    /// 返回的所有变量值保存的文件,为空不返回值
    /// </summary>
    public String ResultFile = "";

    /// <summary>
    /// 脚本运行完成后界面还显示多长时间，方便查看
    /// </summary>
    public int ShowTime = 0;

    /// <summary>
    /// 所有变量的键值对
    /// </summary>
    public Map<String, String> Varlist = new HashMap<String, String>();

    /// <summary>
    /// 将配置文件生成xml
    /// </summary>
    /// <returns></returns>
    public String ToArgString() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError, UnsupportedEncodingException
    {
    	 
    	
    	DocumentBuilderFactory factory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();
        
        Element root = document.createElement("root");
        document.appendChild(root);
        
        root.setAttribute("TaskName", this.TaskName);
        root.setAttribute("TaskFile", this.TaskFile);
        root.setAttribute("SKey", this.SKey);
        root.setAttribute("WindowState", this.WindowState+"");
        if (LogFilePath!="") root.setAttribute("LogFilePath", LogFilePath);
        if (ResultFile!="") root.setAttribute("ResultFile", ResultFile);
        root.setAttribute("ShowTime", ShowTime+"");
        
        Element varList = document.createElement("Varlist");
        
        root.appendChild(varList);

        for (Entry<String, String> entry: Varlist.entrySet()) {

          String key = entry.getKey();
          String value = entry.getValue();
          Element child= document.createElement(key);
          //child.setNodeValue(value);
          child.setTextContent(value);
          varList.appendChild(child);
        }
        StringWriter writer = new StringWriter();
        javax.xml.transform.TransformerFactory.
        newInstance().
        newTransformer().
        transform(new javax.xml.transform.dom.DOMSource(document), new javax.xml.transform.stream.StreamResult(writer));
        String xml= writer.toString(); 
       return java.net.URLEncoder.encode(xml,"utf-8");
    	 
    }
}
