package cn.net.connor.std.processedit.util;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;

import com.teamcenter.rac.util.Registry;

import cn.net.connor.std.aifrcp.util.KSqlUtil;

public class KSqlTable {

	public static final Registry REG = Registry.getRegistry(KSqlTable.class);

	public static class Config {
		private String colName;
		private String dbColName;
		private String additionalInfo;
		private int prefWidth;

		public Config(String colName, String dbColName, String additionalInfo, int prefWidth) {
			this.colName = colName;
			this.dbColName = dbColName;
			this.additionalInfo = additionalInfo;
			this.prefWidth = prefWidth;
			if (this.prefWidth < 0) {
				this.prefWidth = 100;
			}
		}

		public String getColName() {
			return colName;
		}

		public String getDbColName() {
			return dbColName;
		}

		public String getAdditionalInfo() {
			return additionalInfo;
		}

		public int getPrefWidth() {
			return prefWidth;
		}

	}

	private List<Config> configs;
	private List<String> dbCols = new ArrayList<String>();
	private List<String> tableCols = new ArrayList<String>();
	private List<String> additionalInfos = new ArrayList<String>();
	private List<Integer> prefWidth = new ArrayList<Integer>();
	private DefaultTableModel tableModel;
	private JTable table;
	private String sql;
	public static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy/MM/dd");

	public JTable getTable() {
		return table;
	}

	public KSqlTable(List<Config> configs, String sql) throws Exception {
		this.configs = configs;
		this.sql = sql;
		parseConfigs();
		initTable();
	}

	private void parseConfigs() throws Exception {
		for (Config config : configs) {
			dbCols.add(config.dbColName);
			tableCols.add(config.colName);
			additionalInfos.add(config.additionalInfo);
			prefWidth.add(config.prefWidth);
		}
	}

	@SuppressWarnings("serial")
	private void initTable() throws Exception {
		this.tableModel = new DefaultTableModel();
		this.table = new JTable(tableModel) {
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		table.getTableHeader().setReorderingAllowed(false);
		table.setRowHeight(table.getRowHeight() + 7);
		tableModel.setDataVector(null, tableCols.toArray(new String[] {}));
		if (tableCols.size() > 4) {
			table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		}
		int colCnt = prefWidth.size();
		TableColumnModel cm = table.getColumnModel();
		for (int i = 0; i < colCnt; i++) {
			cm.getColumn(i).setPreferredWidth(prefWidth.get(i));
			if(prefWidth.get(i)==0) {
				cm.getColumn(i).setMinWidth(0);
				cm.getColumn(i).setMaxWidth(0);
			}
		}
	}

	public LinkedHashMap<Config, String> getSelectedData() {
		int rowNum = table.getSelectedRow();
		if (rowNum < 0) {
			return null;
		}
		int colCnt = this.configs.size();
		LinkedHashMap<Config, String> res = new LinkedHashMap<KSqlTable.Config, String>();
		for (int i = 0; i < colCnt; i++) {
			res.put(configs.get(i), (String) table.getValueAt(rowNum, i));
		}
		return res;
	}

	public void performSearch(Map<String, String> searchInfo, Connection conn) throws Exception {
		StringBuilder sql = new StringBuilder();
		sql.append(this.sql);
		List<String> params = new ArrayList<>();
		if (searchInfo.size() > 0) {
			// sql.append("\nwhere 1=1 ");
			for (String colName : searchInfo.keySet()) {
				String value = searchInfo.get(colName);
				if (value.contains("*")) {
					value = value.replace('*', '%');
				}
				if (value.contains("%")) {
					sql.append("\nand " + colName + " like ? ");
				} else {
					sql.append("\nand " + colName + " = ? ");
				}
				params.add(value);
			}
		}
		System.out.println("Sql: \n"+sql);
		System.out.println("Params: "+params);
		List<String[]> res = KSqlUtil.queryStringArray(conn, sql.toString(), params.toArray(new String[] {}), this.dbCols.size());
		int rowCnt = res == null ? 0 : res.size();
		System.out.println("Find Data Count: " + rowCnt);
		String[][] data = res.toArray(new String[][] {});
		tableModel.setDataVector(data, tableCols.toArray(new String[] {}));
		int colCnt = prefWidth.size();
		TableColumnModel cm = table.getColumnModel();
		for (int i = 0; i < colCnt; i++) {
			cm.getColumn(i).setPreferredWidth(prefWidth.get(i));
			if(prefWidth.get(i)==0) {
				cm.getColumn(i).setMinWidth(0);
				cm.getColumn(i).setMaxWidth(0);
			}
		}
	}

}