package com.anji.xwl.pojo;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import com.wb.util.StringUtil;

public class XmlUtil {

	public static Conf getConf(String filePath) {
		Conf conf = new Conf();
		try {
			XMLWriter writer = null;// 声明写XML的对象
			SAXReader reader = new SAXReader();

			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");// 设置XML文件的编码格式

			File file = new File(filePath);
			if (file.exists()) {
				Document document = reader.read(file);// 读取XML文件
				List tableList = document.selectNodes("root/table");

				Element table = (Element) tableList.get(0);

				System.out.println(table.attribute("value").getText());
				conf.setTableName(table.attribute("value").getText());
				List keyList = document.selectNodes("root/table/key");

				Element key = (Element) keyList.get(0);

				System.out.println(key.attribute("value").getText());
				conf.setKey(key.attribute("value").getText());
				List searchList = document
						.selectNodes("root/table/searchcolumn/column");

				String sList = ",";
				for (int i = 0; i < searchList.size(); i++) {
					Element search = (Element) searchList.get(i);
					System.out.println(search.attribute("value").getText());
					sList = sList + search.attribute("value").getText() + ",";
				}
				conf.setSearchColumn(sList);

				List packagenameList = document.selectNodes("root/packagename");

				Element packagename = (Element) packagenameList.get(0);

				System.out.println(packagename.attribute("value").getText());
				conf.setPackagename(packagename.attribute("value").getText());

				List topathList = document.selectNodes("root/topath");

				Element topath = (Element) topathList.get(0);

				System.out.println(topath.attribute("value").getText());
				conf.setTopath(topath.attribute("value").getText());

				List baseurlList = document.selectNodes("root/baseurl");

				Element baseurl = (Element) baseurlList.get(0);

				System.out.println(baseurl.attribute("value").getText());
				conf.setBaseurl(baseurl.attribute("value").getText());

				List driverList = document.selectNodes("root/driver");
				Element driver = (Element) driverList.get(0);

				System.out.println(driver.attribute("value").getText());
				conf.setDriver(driver.attribute("value").getText());

				List urlList = document.selectNodes("root/url");
				Element url = (Element) urlList.get(0);

				System.out.println(url.attribute("value").getText());
				conf.setUrl(url.attribute("value").getText());

				List usernameList = document.selectNodes("root/username");
				Element username = (Element) usernameList.get(0);

				System.out.println(username.attribute("value").getText());
				conf.setUsername(username.attribute("value").getText());

				List passwordList = document.selectNodes("root/password");
				Element password = (Element) passwordList.get(0);

				System.out.println(password.attribute("value").getText());
				conf.setPassword(password.attribute("value").getText());

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conf;
	}

	public static String getListString(List<TableColumn> tb,int num) {
		String group = "@@@@@";
		StringBuffer sb = new StringBuffer();
		Collections.sort(tb); 
		for (int i = 0; i < tb.size(); i++) {
			TableColumn c = tb.get(i);
			if (i==0)
			{
				sb.append("  { \"configs\": {\"title\": \""+c.getGroups()+"\",  \"layout\": \"form\", \"width\": \"200\",  \"itemId\": \""+c.getGroups()+"\"  },  \"children\": [");
				sb.append(getPanelListString(c,num));
				group=c.getGroups();
			}
			else if(i==tb.size()-1)
			{
				sb.append(","+getPanelListString(c,num));
				sb.append("   ],  \"expanded\": true,\"type\": \"fieldset\"  }  ");//添加fieldset 
				group=c.getGroups();
			}
			else if (c.getGroups().equals(group))// 表示同一分组需要继续添加
			{
				sb.append(","+getPanelListString(c,num));
				group=c.getGroups();
			}
			else if (!c.getGroups().equals(group))// 表示同一分组需要继续添加
			{
				sb.append("   ],  \"expanded\": true,\"type\": \"fieldset\"  } ");//添加fieldset 
				sb.append("  , { \"configs\":  {\"title\": \""+c.getGroups()+"\", \"layout\": \"form\",  \"width\": \"200\",  \"itemId\": \""+c.getGroups()+"\" },  \"children\": [ ");//添加fieldset 首信息
				sb.append(getPanelListString(c,num));
				group=c.getGroups();
			}
			
		}

		return sb.toString();
	}

    public static String getListString(List<TableColumn> tb) {

        StringBuffer sb = new StringBuffer();
        Collections.sort(tb);
        for (int i = 0; i < tb.size(); i++) {
            TableColumn c = tb.get(i);
            if (i==0)
            {
                sb.append(getPanelListString(c,i));
            }
          else {
                sb.append("," + getPanelListString(c, i));
            }




        }

        return sb.toString();
    }
	
	/**
	 * 
	 * @param tb
	 * @return 获得单独控件
	 */
	public static String getPanelListString(TableColumn tb,int num) {

		tb.getDisp_text();
		StringBuffer str = new StringBuffer();
		str.append("{ \"configs\": {\"fieldLabel\": \"" + tb.getDisp_text()//"@Str."+tb.getField_name()
				+ "\",\"itemId\": \"" + tb.getField_name() + "\"");
		str.append(" ,\"labelAlign\": \"right\"");
		str.append(" ,\"labelWidth\": \"80\"");
        if(num!=9999999) {
            int row = num / 3, col = num % 3;
            str.append(" ,\"x\": \" " + col * 200 + "\""); //x轴位置
            str.append(" ,\"y\": \" " + (row * 30 + 8) + "\"");//y轴位置
        }
		if (!StringUtil.isEmpty(tb.getRead_only()))
			str.append(" ,\"readOnly\": \""+tb.getRead_only()+"\"");
		if (!StringUtil.isEmpty(tb.getVtype()))
			str.append(" ,\"vtype\": \"" + tb.getVtype() + "\"");
		if (!StringUtil.isEmpty(tb.getValidator()))
			str.append(" ,\"validator\": \"" + tb.getValidator() + "\"");
		if (!StringUtil.isEmpty(tb.getDisp_Width()))
			str.append(" ,\"width\": \"" + tb.getDisp_Width() + "\"");
		else
			str.append(" ,\"width\": \"200\"");
		if (!StringUtil.isEmpty(tb.getAllow_blank()))
			str.append(" ,\"allowBlank\": \"" + tb.getAllow_blank() + "\"");
		 if (tb.getType().equals("number")) {// 表示数字型控件
			if (!StringUtil.isEmpty(tb.getMaxValue()))
				str.append(" ,\"maxValue\": \"" + tb.getMaxValue() + "\"");
			if (!StringUtil.isEmpty(tb.getMaxText()))
				str.append(" ,\"maxText\": \"" + tb.getMaxText()
						+ "\"");
			if (!StringUtil.isEmpty(tb.getMinValue()))
				str.append(" ,\"minValue\": \"" + tb.getMinValue() + "\"");
			if (!StringUtil.isEmpty(tb.getMinText()))
				str.append(" ,\"minText\": \"" + tb.getMinText()
						+ "\"");
			if (!StringUtil.isEmpty(tb.getAllowDecimals()))
				str.append(" ,\"allowDecimals\": \"" + tb.getAllowDecimals() + "\"");
			if (!StringUtil.isEmpty(tb.getDecimalPrecision()))
				str.append(" ,\"decimalPrecision\": \"" + tb.getDecimalPrecision() + "\"");
		} 
		
		if (tb.getType().equals("text")) {// 表示字符型控件
			if (!StringUtil.isEmpty(tb.getMaxLength()))
				str.append(" ,\"maxLength\": \"" + tb.getMaxLength() + "\"");
			if (!StringUtil.isEmpty(tb.getMaxLengthText()))
				str.append(" ,\"maxLengthText\": \"" + tb.getMaxLengthText()
						+ "\"");
			if (!StringUtil.isEmpty(tb.getMinLength()))
				str.append(" ,\"minLength\": \"" + tb.getMinLength() + "\"");
			if (!StringUtil.isEmpty(tb.getMinLengthText()))
				str.append(" ,\"minLengthText\": \"" + tb.getMinLengthText()
						+ "\"");
		}

		
		if (tb.getType().equals("combo")) {// 表示字符型控件
			if (!StringUtil.isEmpty(tb.getKey_name()))
				str.append(" ,\"keyName\": \"" + tb.getKey_name() + "\"");
					
		}
		if (!StringUtil.isEmpty(tb.getUrl()))
		str.append(" },\"children\": [{ \"configs\": {   \"itemId\": \"store\", \"url\": \""+tb.getUrl()+"\"  },"+
                    "  \"params\": \""+tb.getParams()+"\",     \"expanded\": false,   \"children\": [],    \"type\": \"store\"  }    ], \"type\": \"combo\"}");
		else
		str.append("},\"children\": [ ],  \"expanded\": false ,\"type\": \""
				+ tb.getType() + "\"}");

		return str.toString();

		
	}

	
	public static String getColumnList(List<TableColumn> tb) {
		StringBuffer  sb=new   StringBuffer();
		Collections.sort(tb); 
		for(int i=0;i<tb.size();i++)
		{
			
			TableColumn c = tb.get(i);
			sb.append(","+getColumn(c));
		}
		
		
		return  sb.toString();
	}
	public static String getColumn(TableColumn tb) {
		StringBuffer str = new StringBuffer();
		str.append("{ \"configs\": {\"text\": \"" +tb.getDisp_text()//"@Str."+tb.getField_name()
				+ "\",\"dataIndex\": \"" + tb.getField_name() + "\"" );
		if (!StringUtil.isEmpty(tb.getDisp_Width()))
			str.append(" ,\"width\": \"" + tb.getDisp_Width()
					+ "\"");
		if (!StringUtil.isEmpty(tb.getRenderer()))
			str.append(" ,\"renderer\": \"" + tb.getRenderer()
					+ "\"");
		if (!StringUtil.isEmpty(tb.getKey_name()))
			str.append(" ,\"keyName\": \"" + tb.getKey_name()
					+ "\"");
		str.append(	" ,\"itemId\": \"" + (tb.getField_name()+"99") + "\"}, \"children\": [],\"expanded\": false, \"type\": \"column\"}");
	
		
		return  str.toString();

	}
	public static void main(String[] args) {
		
		
		List<TableColumn> list =new  ArrayList<TableColumn>();
		for(int i=0;i<=20;i++){
			
			String mc="G"+(i/5);
			TableColumn  tc=new TableColumn("11","表名","链接","url","5","超长度",
				"2","最小长度为2","最大值","最大值提示","最小值","最小值提示","1","小数位置","text","姓名","备注",
				"lianjie","列表显示","可编辑","200","###，##","换行","1","1","rool","200","2","sss","2",mc);
			list.add(tc);
			
		}
		String str=getColumnList(list);
		System.out.println("frematset 格式字符串为：===="+str);
	}
}
