package org.jerrymouse.database.hadoop.jpa;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Map.Entry;

import javax.persistence.FlushModeType;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;

public class HadoopQuery implements Query {
	HadoopTable hadoopTable;
	Class type;

	public HadoopQuery(HadoopTable hadoopTable, String qlString) {
		// SELECT e FROM " + type.getName() + " e"
		this.hadoopTable = hadoopTable;
		String[] tokens = qlString.split(" ");
		String className = null;
		for (int i = 0; i < tokens.length; i++) {
			if ("from".equalsIgnoreCase(tokens[i])) {
				className = tokens[i + 1];
				break;
			}
		}
		try {
			type = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	public int executeUpdate() {
		// TODO Auto-generated method stub
		return 0;
	}

	public List getResultList() {
		List list = new ArrayList();
		try {
			ResultScanner resultScanner = hadoopTable.getTable(type)
					.getScanner(new Scan());
			Result result = null;
			do {
				result = resultScanner.next();
				if (result != null) {
					HadoopEntity bean = new HadoopEntity(type, hadoopTable);
					NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> map = result
							.getMap();
					for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> e : map
							.entrySet()) {
						String key = Bytes.toString(e.getKey());
						byte[] value = null;
						for (Entry<byte[], NavigableMap<Long, byte[]>> e2 : e
								.getValue().entrySet()) {
							for (Entry<Long, byte[]> e3 : e2.getValue()
									.entrySet()) {
								value = e3.getValue();
							}
						}
						bean.set(key, value);
					}
					list.add(bean.getObject());
				}
			} while (result != null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public Object getSingleResult() {
		return null;
	}

	public Query setFirstResult(int startPosition) {
		throw new UnsupportedOperationException();
	}

	public Query setFlushMode(FlushModeType flushMode) {
		throw new UnsupportedOperationException();
	}

	public Query setHint(String hintName, Object value) {
		throw new UnsupportedOperationException();

	}

	public Query setMaxResults(int maxResult) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(int position, Calendar value,
			TemporalType temporalType) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(int position, Date value,
			TemporalType temporalType) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(int position, Object value) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(String name, Calendar value,
			TemporalType temporalType) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(String name, Date value, TemporalType temporalType) {
		throw new UnsupportedOperationException();
	}

	public Query setParameter(String name, Object value) {
		throw new UnsupportedOperationException();
	}

}
