package com.util;

import java.io.InputStream;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import android.app.Activity;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chaoran.component.AntLine;
import com.chaoran.component.SelfHRule;
import com.chaoran.entiry.SelfButton;
import com.chaoran.entiry.SelfCheckBox;
import com.chaoran.entiry.SelfDateField;
import com.chaoran.entiry.SelfEditText;
import com.chaoran.entiry.SelfTextBut;
import com.chaoran.imp.InputInterface;
import com.sys.SysData;

public class LxParamPageCreate {
	public static void lxParamPageCreate(InputStream in, Activity activity, RelativeLayout layout) throws DocumentException {
		SAXReader reader = new SAXReader();
		// 将xml文档转换为Document的对象
		Document document = reader.read(in);
		// 获取文档的根元素
		Element root = document.getRootElement();
		Iterator i_pe = root.elementIterator();
		while (i_pe.hasNext()) {
			Element e_pe = (Element) i_pe.next();
			Iterator i_pe1 = e_pe.elementIterator();
			if (!i_pe1.hasNext()) {
				continue;
			}
			while (i_pe1.hasNext()) {
				Element e_pe1 = (Element) i_pe1.next();
				String name = e_pe1.attributeValue("name");
				name = name.substring(name.indexOf("::") + 2, name.length());
				int w = (int) (Double.parseDouble(e_pe1.elementText("width_attribute")));
				int h = (int) (Double.parseDouble(e_pe1.elementText("height_attribute")));
				int x = (int) (Double.parseDouble(e_pe1.elementText("x_attribute")));
				int y = (int) (Double.parseDouble(e_pe1.elementText("y_attribute")));
				w = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, w, activity.getResources().getDisplayMetrics()) * SysData.t_scale);
				h = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, h, activity.getResources().getDisplayMetrics()) * SysData.t_scale);
				x = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, x, activity.getResources().getDisplayMetrics()) * SysData.t_scale);
				y = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, y, activity.getResources().getDisplayMetrics()) * SysData.t_scale);
				RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(w, h);
				layoutParams.topMargin = y;
				layoutParams.leftMargin = x;
				if (name.equals("SelfButton")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					SelfButton but = new SelfButton(activity);
					if (SysData.t_scale < 1) {
						but.setPadding((int) (but.getPaddingLeft() * SysData.t_scale * 0.2f), (int) (but.getPaddingTop() * SysData.t_scale * 0.2f),
								(int) (but.getPaddingRight() * SysData.t_scale * 0.2f), (int) (but.getPaddingBottom() * SysData.t_scale * 0.2f));
					}
					// but.setFocusableInTouchMode(true);
					but.setText(e_pe1.elementText("label_attribute"));
					if (e_pe1.element("click_function") != null) {
						but.clickFun = e_pe1.elementText("click_function").toString();
					}
					if (e_pe1.element("enterKey_attribute") != null) {
						but.nextFocus = e_pe1.elementText("enterKey_attribute").toString().toUpperCase();
					}
					if (e_pe1.element("fontSize_style") != null) {
						but.setTextSize(Float.parseFloat(e_pe1.elementText("fontSize_style").toString()) * SysData.t_scale);
					}
					if (e_pe1.element("key_value") != null) {
						but.setTag(e_pe1.elementText("key_value").toString().trim().toUpperCase());
					}
					// but.setLayoutParams(new
					// AbsoluteLayout.LayoutParams(w,h,x,y));
					layout.addView(but, layoutParams);
				} else if (name.equals("SelfLabel")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					TextView tv = new TextView(activity);
					tv.setGravity(Gravity.CENTER_VERTICAL);
					if (e_pe1.element("fontSize_style") != null) {
						tv.setTextSize(Float.parseFloat(e_pe1.elementText("fontSize_style").toString()) * SysData.t_scale);
					}
					tv.setText(e_pe1.elementText("text_attribute"));
					layout.addView(tv, layoutParams);
				} else if (name.equals("SelfTextInput")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					if (e_pe1.attributeValue("isDouble").equals("yes")) {
						SelfTextBut stb = new SelfTextBut(activity);
						if (e_pe1.element("doubleClick_function") != null) {
							stb.setClickFun(e_pe1.elementText("doubleClick_function").toString().toUpperCase());
						}
						// setTextButAtt(stb, e_pe1, aa);
						setTextButAtt((InputInterface) stb, e_pe1);
						layout.addView(stb, layoutParams);
					} else {
						SelfEditText et = new SelfEditText(activity);
						if (e_pe1.element("borderStyle_style") != null) {
							et.showBorder = e_pe1.elementText("borderStyle_style").toString();
						}
						et.setSingleLine(true);
						setTextButAtt((InputInterface) et, e_pe1);
						layout.addView(et, layoutParams);
					}
				} else if (name.equals("SelfTextArea")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					SelfEditText area = new SelfEditText(activity);
					if (e_pe1.element("borderStyle_style") != null) {
						area.showBorder = e_pe1.elementText("borderStyle_style").toString();
					}
					// setEditTextAtt(area, e_pe1, aa);
					setTextButAtt((InputInterface) area, e_pe1);
					area.setSingleLine(false);
					// if(e_pe1.element("backgroundColor_style") !=
					// null){//backgroundColor_style
					// area.setBackgroundColor(SysUtil.toHex(e_pe1.elementText("backgroundColor_style")
					// .toString()));
					// }
					// if(e_pe1.element("color_style") !=
					// null){//backgroundColor_style
					// area.setTextColor(SysUtil.toHex(e_pe1.elementText("color_style")
					// .toString()));
					// }
					layout.addView(area, layoutParams);
				} else if (name.equals("SelfCheckBox")) {
					// if (isVisible(e_pe1)) {
					// continue;
					// }
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					SelfCheckBox box = new SelfCheckBox(activity);
					if (SysData.t_scale < 1) {
						box.setPadding((int) (box.getPaddingLeft() * SysData.t_scale * 0.2f), box.getPaddingTop(), box.getPaddingRight(), box.getPaddingBottom());
					}
					if (e_pe1.element("name_attribute") != null) {
						box.setTag(e_pe1.elementText("name_attribute").toString().trim().toUpperCase());
					}
					if (e_pe1.element("label_attribute") != null) {
						box.setText(e_pe1.elementText("label_attribute").toString());
					}
					if (e_pe1.element("fontSize_style") != null) {
						box.setTextSize(Float.parseFloat(e_pe1.elementText("fontSize_style").toString()) * SysData.t_scale); // checkbox
																																// +
																																// 5会变大
					}
					if (e_pe1.element("enabled_attribute") != null) {
						String enabled = e_pe1.elementText("enabled_attribute").toString();
						box.setEnabled(Boolean.parseBoolean(enabled));
					}
					if (e_pe1.element("text_attribute") != null) {
						String funName = e_pe1.elementText("text_attribute").toString().trim().toLowerCase();
						if (funName.equals("是")) {
							box.setChecked(true);
						}
					}
					layout.addView(box, layoutParams);
				} else if (name.equals("SelfHRule") || name.equals("SelfVRule")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					SelfHRule hrule = new SelfHRule(activity);
					if (e_pe1.element("strokeColor_style") != null) {
						// System.out.println();
						hrule.setColor(SysUtil.toHex(e_pe1.elementText("strokeColor_style").toString()));
					}
					layout.addView(hrule, layoutParams);
				} else if (name.equals("AntLine")) {
					if (Dom4jUtil.isVisible(e_pe1)) {
						continue;
					}
					AntLine al = new AntLine(activity);
					if (e_pe1.element("lineColor_attribute") != null) {
						al.setColor(SysUtil.toHex(e_pe1.elementText("lineColor_attribute").toString()));
					}
					if (e_pe1.element("lineThickness_attribute") != null) {
						al.setSize(Integer.parseInt(e_pe1.elementText("lineThickness_attribute")) * 2);
					}
					layout.addView(al, layoutParams);
				} else if (name.equals("SelfDateField")) {
					SelfDateField df = new SelfDateField(activity);
					if (e_pe1.element("formatString_attribute") != null) {
						df.formatString = e_pe1.elementText("formatString_attribute").toString().trim().toLowerCase();
						if (df.formatString.length() < 1) {
							df.formatString = "yyyy-mm-dd";
						}
						df.formatString = df.formatString.replace("mm", "MM");
					}
					setTextButAtt((InputInterface) df, e_pe1);
					layout.addView(df, layoutParams);
				}
			}
			break;
		}
	}

	/* 设置属性 */
	public static void setTextButAtt(InputInterface et, Element e_pe1) {
		et.setPadding(0, 0, 0, 0);
		et.setGravity(Gravity.CENTER_VERTICAL);
		if (e_pe1.element("name_attribute") != null) {
			et.setTag(e_pe1.elementText("name_attribute").toString().trim().toUpperCase());
		}
		if (e_pe1.element("enterKey") != null) {
			et.setNextFocus(e_pe1.elementText("enterKey").toString().toUpperCase());
		}
		if (e_pe1.element("fontSize_style") != null) {
			et.setTextSize(Float.parseFloat(e_pe1.elementText("fontSize_style").toString()) * SysData.t_scale + 5);
		}
		if (e_pe1.element("closeKeyBoard") != null) {
			boolean closeKeyBoard = Boolean.parseBoolean(e_pe1.elementText("closeKeyBoard"));
			et.setCloseKeyBoard(closeKeyBoard);
			if (closeKeyBoard) {
				et.setInputType(InputType.TYPE_NULL);
			}
		}
		if (e_pe1.element("focusOut_function") != null) {
			et.setFocusOutFun(e_pe1.elementText("focusOut_function").toString());
		}
		if (e_pe1.element("focusIn_function") != null) {
			et.setFocusInFun(e_pe1.elementText("focusIn_function").toString());
		}
		if (e_pe1.element("enabled_attribute") != null) {
			Boolean enabled = Boolean.parseBoolean(e_pe1.elementText("enabled_attribute").toString());
			et.setEnabled(enabled);
		}
		if (e_pe1.element("backgroundColor_style") != null) {// backgroundColor_style
			et.setBackgroundDrawable(SysUtil.GradientDrawable(e_pe1.elementText("backgroundColor_style").toString()));
		} else {
			et.setBackgroundDrawable(SysUtil.GradientDrawable());
		}
		if (e_pe1.element("text_attribute") != null) {
			String funName = e_pe1.elementText("text_attribute").toString().trim().toLowerCase();
			et.setText(funName);
		}
		if (e_pe1.element("isValue_attribute") != null) {
			et.setIsNull(Boolean.parseBoolean(e_pe1.elementText("isValue_attribute")));
		}
		if (e_pe1.element("label_attribute") != null) {
			et.setLabel(e_pe1.elementText("label_attribute").toString().trim());
		}
		if (e_pe1.element("label_attribute") != null) {
			et.setLabel(e_pe1.elementText("label_attribute").toString().trim());
		}
		if (e_pe1.element("color_style") != null) {
			et.setTextColor(SysUtil.toHex(e_pe1.elementText("color_style").toString().trim()));
		}
	}
}
