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

import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.Map.Entry;

public class TagFormat {
	public static Properties TagProps = new Properties();
	public static Map<String,TagLine> TagMap = new HashMap<String,TagLine>();
	public static final char[] CharTypes = "ABacdehnxyz".toCharArray();
	static{
		try {
			TagProps.load(TagFormat.class.getClassLoader().getResourceAsStream("swift/tags.properties"));
			for(Entry<Object, Object> entry :TagProps.entrySet())
			{
				String tag = (String)entry.getKey();
				String pattern = (String) entry.getValue();
				TagLine line =  WalkTagCell(tag,pattern.trim());
				TagMap.put(tag, line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static boolean validateTagValue(String tag,String value)
	{
		if(value == null || tag == null)
			return false;
		TagLine tl = TagMap.get(tag);
		if(tl == null)
			return false;
		String reg = tl.getAnalyzeReg();
		return value.matches(reg);
	}
	public static TagLine getTagLine(String tag)
	{
		TagLine tl = TagMap.get(tag);
		if(tl!=null)
			return tl.clone();
		return new TagLine();
	}
	public static TagLine WalkTagCell (String tag,String pattern)
	{
		StringReader reader = new StringReader(pattern);
		TagLine line = new TagLine();
		line.setPattern(pattern);
		line.setName(tag);
		try {
			int ic = -1;
			boolean isFixed = false;
			boolean isFunc = false;
			StringBuilder content = new StringBuilder();
			StringBuilder len = new StringBuilder();
			int lines = 1;
			int cnt = 1;
			int minLen = 1;
			boolean isConst = false;
			TagCell cell = null;
            Stack<Integer> bracket = new Stack<Integer>();
			while(true)
			{
				ic=reader.read();
				char c = (char) ic;
				if( (c< '0' || c > '9') && cell != null && "FUNC".equals(cell.type) && "AMOUNT|NUMBER".indexOf( cell.content) > -1)
				{
					//函数表示长度
					if(len.length() > 0)
						cell.length = Integer.parseInt(len.toString());
					//else 
					cell = null ;  //清理
					len.delete(0, len.length());
				}
				if(ic == -1)
					break;
				if(isConst && c!='\'')
				{
					content.append(c);
				}
				else if(isFunc && c!='>')
				{
					content.append(c);
				}
				else if(c>='0' && c <= '9') //长度区间 1
				{
					len.append(c);
					
				}
				else if(c=='!')
					isFixed = true;
				else if(c == '*')
				{
					if(len.length() > 0)
					{
						lines = Integer.parseInt(len.toString());
						len.delete(0, len.length());
					}
				}
				else if(c == '-')
				{
					if(len.length() > 0)
					{
						minLen = Integer.parseInt(len.toString());
						len.delete(0, len.length());
					}
				}
				else if(Arrays.binarySearch(CharTypes, c)  >= 0  && len.length() > 0)
				{
					//字符块
//					System.err.println("c type:"+c+"---"+Arrays.binarySearch(CharTypes, c));
//					System.err.println("c number:"+((int) c)+"--"+((int)'['));
					cell = new TagCell();
					cell.fixed = isFixed;
					cell.length = Integer.parseInt(len.toString());
					cell.type = String.valueOf(c);
					cell.cnt = cnt++;
					cell.line = lines;
					cell.minLen = minLen;
					//清理
					isFixed = false;
					minLen = 1;
					len.delete(0, len.length());
					lines = 1;
					
					line.add(cell);
				}
				else if(c == '\'')
				{
					if(!isConst)
					{
						isConst = true;
						continue;
					}
					cell = new TagCell();
					cell.fixed = true;
					cell.length = content.length();
					cell.type = TagCell.CONST;
					cell.content = content.toString();
					cell.cnt = -1;
					cell.line = 1;
					content.delete(0, content.length());
					isConst = false;
					line.add(cell);
				}else if(c == '<')
				{
					isFunc = true;
				}
				else if(c == '>')
				{
					isFunc = false;
					cell = new TagCell();
					cell.fixed = true;
					cell.length = content.length();
					cell.type = TagCell.FUNC;
					cell.content = content.toString();
					cell.cnt = cnt++;
					cell.line = 1;
					content.delete(0, content.length());
					line.add(cell);
				}
				else if(c == '[')
				{
					line.addOptions(new int[]{line.cellList.size(),-1});
                    bracket.push(line.options.size()-1);
				}
				else if(c == ']')
				{
					//if("51A".equals(tag))
					//line.fillLastOption(line.cellList.size());
                    line.fillOption(bracket.pop(),line.cellList.size());
				}
			}
			line.setMaxCnt(cnt-1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		line.getAnalyzeReg(); //生成栏位表达式
		return line;
	}
	
	
	
}
