package com.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.chaoran.entiry.SelfCheckBox;
import com.chaoran.entiry.SelfEditText;
import com.chaoran.entiry.SelfTextBut;
import com.chaoran.imp.InputInterface;

import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TextView;

public class SqlUtil {
	public static HashMap regSql(String sql, TabHost layout, Map pageMap) {
		System.out.println("------------------===============================");
//		System.out.println(sql);
		// String regEx=":[\\w|.]*[\\s]{0}";
		String regEx = "[:|\\&]([\\w\\.]+)";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(sql);
		String paramName = null;
		String oldParam = null;
		HashMap paramMap = new HashMap();
		String paramValue = null;
		String sqlCopy = sql;
		String paramType = null;
		while (m.find()) {
			oldParam = m.group();
			 System.out.println(oldParam+"--------------------");
			paramType = oldParam.substring(0, 1);
			paramName = oldParam.toUpperCase();
			if (paramName.indexOf("MX.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else if (paramName.indexOf("HZ.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else {
				paramName = paramName.substring(1, paramName.length());
			}
			sqlCopy = sqlCopy.replaceFirst(oldParam,
					paramType.concat(paramName));
			View view = layout.findViewWithTag(paramName);
			if (view != null) {
				// if (view instanceof SelfEditText) {
				// paramValue = ((SelfEditText) view).getText().toString()
				// .trim();
				// } else if (view instanceof SelfCheckBox) {
				// SelfCheckBox sfb = (SelfCheckBox) view;
				// boolean b = sfb.isChecked();
				// if (b) {
				// paramValue = "是";
				// } else {
				// paramValue = "否";
				// }
				// } else if (view instanceof TextView) {
				// paramValue = ((TextView) view).getText().toString().trim();
				// }else if(view instanceof SelfTextBut){
				// paramValue = ((SelfTextBut)
				// view).getText().toString().trim();
				// }
				if (view instanceof InputInterface) {
					InputInterface inView = (InputInterface) view;
					paramValue = inView.getText().toString();
				} else if (view instanceof SelfCheckBox) {
					SelfCheckBox sfb = (SelfCheckBox) view;
					boolean b = sfb.isChecked();
					if (b) {
						paramValue = "是";
					} else {
						paramValue = "否";
					}
				} else if (view instanceof TextView) {
					paramValue = ((TextView) view).getText().toString();
				}
			} else if (pageMap.containsKey(paramName)) {
				paramValue = pageMap.get(paramName).toString();
			}
			if (paramValue == null) {
				paramValue = "";
			} else {
				paramValue = paramValue.trim();
			}
			if (paramType.equals(":")) {
				paramMap.put(paramName, paramValue);
			} else {
				paramValue = paramValue.replace("$", "\\$");
				sqlCopy = sqlCopy.replaceFirst(paramType.concat(paramName),
						paramValue);
			}
			if (paramValue != null) {
				paramValue = null;
			}
		}
		HashMap map = new HashMap();
		System.out.println("============="+sqlCopy+"------------------------sqlCopy");
		map.put("sql", sqlCopy);
		map.put("param", paramMap);
		return map;
	}

	public static HashMap regSql(String sql, Map item) {
		// String regEx=":[\\w|.]*[\\s]{0}";
		String regEx = "[:|\\&]([\\w\\.]+)";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(sql);
		String paramName = null;
		String oldParam = null;
		HashMap paramMap = new HashMap();
		String paramValue = "";
		String sqlCopy = sql;
		String paramType = null;
		while (m.find()) {
			oldParam = m.group();
			System.out.println(oldParam + "--------------------");
			paramType = oldParam.substring(0, 1);
			paramName = oldParam.toUpperCase();
			if (paramName.indexOf("MX.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else if (paramName.indexOf("HZ.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else {
				paramName = paramName.substring(1, paramName.length());
			}
			sqlCopy = sqlCopy.replaceFirst(oldParam,
					paramType.concat(paramName));
			if (item.containsKey(paramName)) {
				paramValue = item.get(paramName).toString();
			}
			if (paramType.equals(":")) {
				paramMap.put(paramName, paramValue);
			} else {
				paramValue = paramValue.replace("$", "\\$");
				sqlCopy = sqlCopy.replaceFirst(paramType.concat(paramName),
						paramValue);
			}
			if (!paramValue.equals("")) {
				paramValue = "";
			}
		}
		HashMap map = new HashMap();
		map.put("sql", sqlCopy);
		map.put("param", paramMap);
		return map;
	}

	public static HashMap regSql(String sql, RelativeLayout layout) {
		System.out.println(sql);
		String regEx = "[:|\\&]([\\w\\.]+)";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(sql);
		String paramName = null;
		String oldParam = null;
		HashMap paramMap = new HashMap();
		String paramValue = null;
		String sqlCopy = sql;
		String paramType = null;
		while (m.find()) {
			oldParam = m.group();
			paramType = oldParam.substring(0, 1);
			paramName = oldParam.toUpperCase();
			if (paramName.indexOf("MX.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else if (paramName.indexOf("HZ.") > -1) {
				paramName = paramName.substring(4, paramName.length());
			} else {
				paramName = paramName.substring(1, paramName.length());
			}
			sqlCopy = sqlCopy.replaceFirst(oldParam,
					paramType.concat(paramName));
			View view = layout.findViewWithTag(paramName);
			if (view != null) {
				if (view instanceof InputInterface) {
					InputInterface inView = (InputInterface) view;
					paramValue = inView.getText().toString();
				} else if (view instanceof SelfCheckBox) {
					SelfCheckBox sfb = (SelfCheckBox) view;
					boolean b = sfb.isChecked();
					if (b) {
						paramValue = "是";
					} else {
						paramValue = "否";
					}
				} else if (view instanceof TextView) {
					paramValue = ((TextView) view).getText().toString();
				}
			} 
			if (paramValue == null) {
				paramValue = "";
			} else {
				paramValue = paramValue.trim();
			}
			if (paramType.equals(":")) {
				paramMap.put(paramName, paramValue);
			} else {
				paramValue = paramValue.replace("$", "\\$");
				sqlCopy = sqlCopy.replaceFirst(paramType.concat(paramName),
						paramValue);
			}
			if (paramValue != null) {
				paramValue = null;
			}
		}
		HashMap map = new HashMap();
		map.put("sql", sqlCopy);
		map.put("param", paramMap);
		return map;
	}

}
