package com.wb.common;

import com.wb.util.DbUtil;
import com.wb.util.StringUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class KVBuffer {
	private static List<String> jns=new ArrayList<String>();
	private static List<String> kvSql=new ArrayList<String>();
	private static List<String> codSql=new ArrayList<String>();
	/**
	 * 键值数据缓存。
	 */
	public static ConcurrentHashMap<String, ConcurrentHashMap<Object, String>> buffer;




	/**
	 * 获取指定键值的键值项列表数组组成的字符串。
	 * @param //key 键名称。
	 * @return 键值项列表。
	 */
	public static String getList(String keyName, String tenancyId) {
		ConcurrentHashMap<Object, String> map = buffer.get(keyName);
		if (map == null)
			return "[]";

		StringBuilder buf = new StringBuilder();
		Set<Entry<Object, String>> es = map.entrySet();
		boolean isFirst = true;
		Object K;
		String V;

		buf.append("[");
		for (Entry<Object, String> e : es) {
			if (isFirst)
				isFirst = false;
			else
				buf.append(",");
			buf.append("{\"K\":");
			K = e.getKey();
			if (K instanceof Integer)
				buf.append(Integer.toString((Integer) K));
			else
				buf.append(StringUtil.quote((String) K));
			buf.append(",\"V\":");
			V = e.getValue();
			if (V.startsWith("@"))
				V = V.substring(1);
			else
				V = StringUtil.quote(V);
			buf.append(V);
			buf.append("}");
		}
		buf.append("]");
		return buf.toString();
	}
	/**
	 * 加载和初始化。
	 */
	public static synchronized void load() {
		try {
			buffer = new ConcurrentHashMap<String, ConcurrentHashMap<Object, String>>();
			Connection conn = null;
			Statement st = null;
			ResultSet rs = null;
			String K, keyName = null, preKeyName = null;
			ConcurrentHashMap<Object, String> map = new ConcurrentHashMap<Object, String>();

			try {
				//getSqlS("ilog_code");
				getSqlS("default");
				CodeBuffer.load(jns,codSql);
				//CodeBuffer.load();
				conn = DbUtil.getConnection();
				st = conn.createStatement();
				rs = st.executeQuery("select KEY_ID,KEY_NAME, key_name key_note,K,V,TYPE from  WB_KEY order  by  KEY_NAME  ");
				while (rs.next()) {
					keyName = rs.getString("KEY_NAME");
					if (preKeyName != null && !preKeyName.equals(keyName)) {
						buffer.put(preKeyName, map);
						map = new ConcurrentHashMap<Object, String>();
					}
					K = rs.getString("K");
					map.put(rs.getInt("TYPE") == 1 ? K : Integer.parseInt(K),
							rs.getString("V"));
					preKeyName = keyName;
				}
				if (preKeyName != null)
					buffer.put(preKeyName, map);
				for (int  i=0;i<jns.size();i++)
				{
					System.out.println(kvSql.get(i));
					loadWbKey(jns.get(i),kvSql.get(i));
				}
//				for (int  i=0;i<str.size();i++)
//				{
//					loadWbKey(str[i],Sql[i]);
//				}
				//		loadMenu.loadMenuBuffer();
			} finally {
				DbUtil.close(rs);
				DbUtil.close(st);
				DbUtil.close(conn);
			}
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}


	//不同数据源 加载对应CODE 放入wbKey中
	public static  void loadWbKey(String Jndi,String sql) {
		try {
			Connection conn = null;
			Statement st = null;
			ResultSet rs = null;
			String K, keyName = null, preKeyName = null;
			ConcurrentHashMap<Object, String> map = new ConcurrentHashMap<Object, String>();

			try {
				conn = DbUtil.getConnection(Jndi);
				st = conn.createStatement();
				rs = st.executeQuery(sql);
				while (rs.next()) {
					keyName = rs.getString("KEY_NAME");
					if (preKeyName != null && !preKeyName.equals(keyName)) {
						buffer.put(preKeyName, map);
						map = new ConcurrentHashMap<Object, String>();
					}
					K = rs.getString("K");
					map.put( K ,rs.getString("V"));
					preKeyName = keyName;
				}
				if (preKeyName != null)
					buffer.put(preKeyName, map);
			} finally {
				DbUtil.close(rs);
				DbUtil.close(st);
				DbUtil.close(conn);
			}
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
	}
	/**
	 * 获取指定键对应的值。
	 * @param map 键值对照map。
	 * @param key 键。
	 * @return 键对应的字符串值。
	 */
	public static String getValue(ConcurrentHashMap<?, ?> map, Object key) {
		Object value;
		if (key == null)
			return null;
		if (key instanceof Number)
			value = map.get(((Number) key).intValue());
		else
			value = map.get(key.toString());
		if (value == null)
			return key.toString();
		else
			return value.toString();
	}
	public static void getnames(){
		System.out.println("=======");
	}

	private static void getSqlS(String jndi) {

		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		String jnid=null;
		int i=0;
		List<String>  jn=new ArrayList<String>();
		List<String>  kvsqls=new ArrayList<String>();
		List<String>  codSqls=new ArrayList<String>();
		try {
			String  kvSql = "",
					codSql= "";
			conn = DbUtil.getConnection(jndi);
			st = conn.createStatement();
			rs = st.executeQuery("select KV_SQL ,COD_SQL,JNDI from  CODE_SET  order by JNDI,TABLE_NAME");
			while (rs.next()) {
				if (jnid != null && !jnid.equals(rs.getString("JNDI"))) {
					jn.add(i, jnid);
					kvsqls.add(i, "select  * from  ("+kvSql+")   order by KEY_NAME");
					codSqls.add(i, "select  * from  ("+codSql+")   order by KEY_NAME");
					kvSql="";
					codSql="";
					i++;
				}
				if(jnid==null||!jnid.equals(rs.getString("JNDI")))
				{
					kvSql += rs.getString("KV_SQL");
					codSql +=rs.getString("COD_SQL");
				}
				else{
					kvSql += " union all "+rs.getString("KV_SQL");
					codSql +=" union all "+ rs.getString("COD_SQL");
				}
				jnid= rs.getString("JNDI");
			}
			if (jnid != null){
				jn.add(i, jnid);
				kvsqls.add(i, "select  * from  ("+kvSql+")   order by KEY_NAME");
				codSqls.add(i, "select  * from  ("+codSql+")   order by KEY_NAME");
			}


		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jns=jn;
			kvSql=kvsqls;
			codSql=codSqls;
			DbUtil.close(rs);
			DbUtil.close(st);
			DbUtil.close(conn);
		}
	}

	public static boolean isRole(String userName, String url) {
		return true;
	}

    public static List<KvBean> getAllByKey(String db_err_info, Object o) {
		return new ArrayList<>();
    }
}