package com.brilliance.isc.swift.swifteditor.tag.message;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

import com.brilliance.isc.swift.swifteditor.tag.TagFormat;
import com.brilliance.isc.swift.swifteditor.tag.TagLine;
import com.google.gson.Gson;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class JSOutputTools {

	private static Template TAG_TEMP ; 
	private static Template INDEX_TEMP;
	private static Template TAG_VALIDATE;
	static
	{
		try{
		Configuration config = new Configuration();
		config.setClassLoaderForTemplateLoading(JSOutputTools.class.getClassLoader(),"");
        config.setDefaultEncoding("UTF-8");
        
        TAG_TEMP = config.getTemplate("swift/tag.ftl");
        INDEX_TEMP = config.getTemplate("swift/index.ftl");
        TAG_VALIDATE = config.getTemplate("swift/tag_validate.ftl");
		}catch(Exception e)
		{
			
		}
	}
	
	public static void main(String[] args)
	{
//		System.out.println(TagFormat.TagMap.get("52A").toAnalyzeReg());
//		System.out.println(new Gson().toJson(TagFormat.TagMap.get("52A").toSegRegMap()));
		
		Map<String,String> rs = new HashMap<String,String>();
		for(Map.Entry<String, TagLine> entry :  TagFormat.TagMap.entrySet())
		{
			rs.put(entry.getKey(),new Gson().toJson(entry.getValue().toSegRegMap()) );
		}
		File file = new File("/Volumes/FUKAI/Git/swiftedit/src/swift");
		FileWriter fw = null;
		try {
			fw = new FileWriter(new File(file,"TagRegMap.js"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> c = new HashMap<String,Object>();
		c.put("regs", rs);
		try {
			TAG_VALIDATE.process(c, fw);
		} catch (TemplateException | IOException e) {
			e.printStackTrace();
		}
		try {
			if (fw != null) {
				fw.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void outputTags() {
		// TODO Auto-generated method stub
		 MessageFormat.loadSWFMessage();
		 Map<String,TagLine> tags = new HashMap<String,TagLine>();
		 for(Map.Entry<String, SWFMessage> entry : MessageFormat.mtCache.entrySet())
		 {
			 SWFMessage swf =  entry.getValue();
			 swf.getFields();
			 Stack<MessageArea> stack = new Stack<MessageArea>();
			 stack.add(swf);
			 while(!stack.isEmpty())
			 {
				 AbstractMessageArea aba = (AbstractMessageArea)stack.pop();
				 for(MessageArea ma:aba.getFields())
				 {
					 if(ma.getType() != MessageArea.TAG)
					 {
						 stack.add(ma);
						 continue;
					 }
					 TagLine tl = (TagLine)ma;
					 if(tl.getSuffix().size() > 0)
					 {
						 for(String tag : tl.getSuffix())
						 {	 
							 if(tags.containsKey(tag))
								 continue;
							 TagLine tl2 = tl.clone();
							 tl2.setName(tag);
							 tags.put(tag, tl2);
						 }
						 continue;
					 }
					 if(tags.containsKey(tl.getName()))
						continue; 	
					 
					 tags.put(tl.getName(), tl);	 
				 }
					 
			 }
		 }
		 File path = new File("/Volumes/FUKAI/Tags");
		 if(path.exists())
		 {
			 path.delete();
			 path.mkdirs();
		 }
		 
		 path.mkdirs();
		 Map<String,Map> root = new HashMap<String,Map>();
		 root.put("tags", tags);
		 FileWriter fw;
		try {
			fw = new FileWriter(new File(path,"index.js"));
			 INDEX_TEMP.process(root,fw );
			 fw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 for(Map.Entry<String, TagLine> entry : tags.entrySet())
		 {
			 outputTagJS(path,entry.getValue());
 
		 }
	}
	
	public static void  outputTagJS(File path,TagLine tag)
	{
		try {
			File des = new File(path,"T"+tag.getName()+".js");
			//System.out.println(des.getAbsolutePath());
			if(!des.exists() && !des.createNewFile())
				return ;
			FileWriter fw = new FileWriter(des);
			Map<String,Object> root = new HashMap<String,Object>();
			root.put("tag", tag.getName());
			root.put("desp", tag.getDesp());
			root.put("pattern", tag.getPattern()==null?"":tag.getPattern());
			//root.put("T", tag);
			TAG_TEMP.process(root, fw);
			fw.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void outputMsgFormat(File path)
	{
		
	}
}
