package com.saga.tool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.hadoop.conf.Configuration;
import org.zillion.util.log.LogUtil;

import com.saga.entity.EntityPoint;
import com.saga.util.Constant;
import com.zillion.database.agent.ZillionAgent;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class ImportPoints {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// agentStart();
		// agentsStart();
		// importPoints();
		// System.exit(0);
	}

	private static Map<Integer, Map<Integer, Map<Integer, String>>> readExcel(File file) {
		Map<Integer, Map<Integer, Map<Integer, String>>> result = new HashMap<Integer, Map<Integer, Map<Integer, String>>>();
		try {
			Workbook book = Workbook.getWorkbook(file);
			// System.out.println(book.getNumberOfSheets());
			// 遍历sheet
			for (int k = 0; k < book.getNumberOfSheets(); k++) {
				// excel导入map存储,sheet页/行/列
				result.put(k, new HashMap<Integer, Map<Integer, String>>());
				Sheet sheet = book.getSheet(k);
				// System.out.println("Received:" + sheet.getName());
				int rownum = sheet.getRows();
				int colnum = sheet.getColumns();
				if (rownum != 0) {
					for (int j = 0; j < rownum; j++) {
						result.get(k).put(j, new HashMap<Integer, String>());
						for (int i = 0; i < colnum; i++) {
							Cell cell = sheet.getCell(i, j);
							String content = cell.getContents();
							result.get(k).get(j).put(i, content);
						}
					}
				}
			}

		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	private static Map<Integer, Map<Integer, String>> readExcel(File file, String sheetName) {
		Map<Integer, Map<Integer, String>> result = new HashMap<Integer, Map<Integer, String>>();
		try {
			Workbook book = Workbook.getWorkbook(file);

			// excel导入map存储,行/列

			Sheet sheet = book.getSheet(sheetName);
			// System.out.println("Received:" + sheet.getName());
			if (sheet == null) {
				return result;
			}
			int rownum = sheet.getRows();
			int colnum = sheet.getColumns();
			if (rownum != 0) {
				for (int j = 0; j < rownum; j++) {
					result.put(j, new HashMap<Integer, String>());
					for (int i = 0; i < colnum; i++) {
						Cell cell = sheet.getCell(i, j);
						String content = cell.getContents();
						result.get(j).put(i, content);
						// System.out.println("(" + i + "," + j + ")=" +
						// content);
					}
				}
			}
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IndexOutOfBoundsException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	// private static void importPoints() {
	// // TODO Auto-generated method stub
	// try {
	// String filepathPoints = "\\pointlistadd.xls";
	// File file = new File(System.getProperty("user.dir") + filepathPoints);
	// Map<Integer, Map<Integer, Map<Integer, String>>> points =
	// readExcel(file);
	// Map<String, List<EntityPoint>> buildingPointList = new HashMap<String,
	// List<EntityPoint>>();
	// for (Entry<Integer, Map<Integer, Map<Integer, String>>> s :
	// points.entrySet()) {
	// Map<String, Integer> cv = new HashMap<String, Integer>();
	// for (Entry<Integer, Map<Integer, String>> r : s.getValue().entrySet()) {
	// if (r.getKey() == 0) {
	// for (Entry<Integer, String> c : r.getValue().entrySet()) {
	// cv.put(c.getValue(), c.getKey());
	// }
	// } else {
	//
	// EntityPoint ep = new EntityPoint();
	//
	// ep.building = r.getValue().get(cv.get("building"));
	// ep.meter = r.getValue().get(cv.get("meter"));
	// ep.funcid = Long.valueOf(r.getValue().get(cv.get("funcid")));
	// ep.system = r.getValue().get(cv.get("system"));
	// ep.source = r.getValue().get(cv.get("source"));
	// ep.address_1 = r.getValue().get(cv.get("address_1"));
	// ep.ip_port = r.getValue().get(cv.get("ip_port"));
	// ep.address_2 = r.getValue().get(cv.get("address_2"));
	// ep.address_3 = r.getValue().get(cv.get("address_3"));
	// ep.address_4 = r.getValue().get(cv.get("address_4"));
	// ep.data_type = r.getValue().get(cv.get("data_type"));
	// ep.collect_cycle = r.getValue().get(cv.get("collect_cycle"));
	// ep.unit = r.getValue().get(cv.get("unit"));
	// ep.remark1 = r.getValue().get(cv.get("remark1"));
	// ep.remark2 = r.getValue().get(cv.get("remark2"));
	// ep.remark3 = r.getValue().get(cv.get("remark3"));
	//
	// if (!buildingPointList.containsKey(ep.building)) {
	// buildingPointList.put(ep.building, new ArrayList<EntityPoint>());
	// }
	// buildingPointList.get(ep.building).add(ep);
	// }
	// }
	// }
	//
	// for (Entry<String, List<EntityPoint>> e : buildingPointList.entrySet()) {
	// for (EntityPoint p : e.getValue()) {
	// try {
	// // HBaseUtil.Insert_dy_pointlist(Constant.agent,
	// // Constant.BuildingDB.get(p.building), p.building,
	// // p.meter, p.funcid, p.system, p.source, p.address_1,
	// // p.ip_port, p.address_2, p.address_3,
	// // p.address_4, p.collect_cycle, p.data_type, p.unit);
	// Constant.Insert_agents_dy_pointlist(p.building, p.meter, (int) p.funcid,
	// p.system, p.source,
	// p.address_1, p.ip_port, p.address_2, p.address_3, p.address_4,
	// p.collect_cycle,
	// p.data_type, p.unit);
	// } catch (Exception e1) {
	// // TODO Auto-generated catch block
	// e1.printStackTrace();
	// }
	// }
	// LogUtil.info(" batch_insert dy_pointlist " + e.getKey() + " complete!");
	// }
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	//
	// }

	private static void agentsStart() {
		// TODO Auto-generated method stub
		try {
			// String resource = "/hbase-site.xml";
			// Configuration configuration = new Configuration();
			// configuration.addResource(new
			// FileInputStream(System.getProperty("user.dir") + resource));
			// int zookeeper_sessionTimeout = 300000;

			List<String> qrlist = new ArrayList<String>();
			qrlist.add("zookeeper1:2181,zookeeper2:2181,zookeeper3:2181");
			qrlist.add("zookeeper1,zookeeper2,zookeeper3");
			qrlist.add("zookeeper1");
			qrlist.add("zookeeper1.saga");

			for (String qr : qrlist) {
				try {
					Configuration configuration = new Configuration();
					configuration.set("hbase.cluster.distributed", "true");
					configuration.set("hbase.zookeeper.quorum", qr);
					Constant.agent = new ZillionAgent(configuration);
					Constant.agent.setFull_name(qr);
					Constant.agent.Start();

				} catch (Exception e) {
					e.printStackTrace();
					LogUtil.error("Main.init.agentsStart: " + e.toString());
				}
			}
			// Constant.agents.add(new ZillionAgent(configuration,
			// "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181",
			// zookeeper_sessionTimeout));
			// Constant.agents
			// .add(new ZillionAgent(configuration,
			// "zookeeper1,zookeeper2,zookeeper3", zookeeper_sessionTimeout));
			// Constant.agents.add(new ZillionAgent(configuration, "zookeeper1",
			// zookeeper_sessionTimeout));
			// Constant.agents.add(new ZillionAgent(configuration,
			// "zookeeper1.saga", zookeeper_sessionTimeout));

			// Iterator<ZillionAgent> it = Constant.agents.iterator();
			// while (it.hasNext()) {
			// ZillionAgent agent = it.next();
			// try {
			// agent.Start();
			// LogUtil.info(agent.getFull_name() + " starting ...");
			// } catch (Exception e) {
			// // TODO Auto-generated catch block
			// // agent启动失败则移除，以免造成资源浪费
			// LogUtil.warn(agent.getFull_name() + " failed to start ,now remove
			// !");
			// it.remove();
			// LogUtil.error("Main.init.agentsStart: " + e.toString());
			// }
			// }
		} catch (Exception e) {
			// TODO Auto-generated catch block
			LogUtil.error("Main.init.agentsStart: " + e.toString());
		}
	}

	// private static void agentStart() {
	// // TODO Auto-generated method stub
	// try {
	// String resource = "/hbase-site.xml";
	// Configuration configuration = new Configuration();
	// configuration.addResource(new
	// FileInputStream(System.getProperty("user.dir") + resource));
	// int zookeeper_sessionTimeout = 300000;
	//
	// Constant.agents.add(new ZillionAgent(configuration,
	// "zookeeper1:2181,zookeeper2:2181,zookeeper3:2181",
	// zookeeper_sessionTimeout));
	// Constant.agents
	// .add(new ZillionAgent(configuration, "zookeeper1,zookeeper2,zookeeper3",
	// zookeeper_sessionTimeout));
	// for (ZillionAgent agent : Constant.agents) {
	// try {
	// agent.Start();
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// LogUtil.error("Main.init.agentsStart: " + e.toString());
	// }
	// }
	//
	// } catch (Exception e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// }
}
