package org.bdware.sc.blockdb;

import org.bdware.sc.commParser.BDLedger.Block;
import org.bdware.sc.commParser.BDLedger.BlockBody;
import org.bdware.sc.commParser.BDLedger.BlockHeader;
import org.bdware.sc.util.HashUtil;

import java.io.File;
import java.sql.*;

public class SqliteDBUtil {
	private Connection conn;

	public static SqliteDBUtil connect(String url) {
		try {
			String name = "org.sqlite.JDBC";
			SqliteDBUtil util = new SqliteDBUtil();
			String path = "jdbc:sqlite:";
			File file = new File(url);
			path = path + file.getAbsolutePath();
			System.out.println("[SqliteDBUtil] connect:" + path);
			Class.forName(name);
			util.conn = DriverManager.getConnection(path);
			return util;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public BlockBody getBlockBody(String headerHash, String bodyHash) {
		try {
			Statement stmt = conn.createStatement();
			// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
			// " + headerHash);
			String sql = "select * from BlockBody where ID = ?";
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setBytes(1, HashUtil.str16ToBytes(bodyHash));
			ResultSet result = pre.executeQuery();
			// Assert we get only one!!!!
			while (result.next()) {
				System.out.println(result.getBytes("ID"));
				return BlockBody.fromBytes(result.getBytes("Data"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public Block getBlock(String headerHash, String bodyHash) {
		try {
			BlockHeader header = new BlockHeader();
			BlockBody body = new BlockBody();
			Statement stmt = conn.createStatement();
			String sql = "select * from BlockBody where ID = ?";
			PreparedStatement pre = conn.prepareStatement(sql);
			pre.setBytes(1, HashUtil.str16ToBytes(bodyHash));
			ResultSet result = pre.executeQuery();
			while (result.next()) {
				body = BlockBody.fromBytes(result.getBytes("Data"));
			}
			String sql1 = "select * from BlockHeader where Hash = ?";
			pre = conn.prepareStatement(sql1);
			pre.setBytes(1, HashUtil.str16ToBytes(headerHash));
			result = pre.executeQuery();
			while (result.next()) {
				header.index = result.getInt(1);
				header.hash = result.getBytes(2);
				header.version = result.getInt(3);
				header.timestamp = result.getInt(4);
				header.prevblockID = result.getBytes(5);
				header.merkleroot = result.getBytes(6);
				header.creatorID = result.getBytes(7);
				Block block = new Block(header,body);
				return block;
				//return Block.fromBytes(result.getBytes("Data"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public byte[] bodyselectall() {
		try {
			Statement stmt = conn.createStatement();
			// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
			// " + headerHash);
			String sql = "select * from BlockBody limit 1,1";
			PreparedStatement pre = conn.prepareStatement(sql);
			ResultSet result = pre.executeQuery();
			// Assert we get only one!!!!
			while (result.next()) {
				return result.getBytes(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public byte[] headerselectall() {
		try {
			Statement stmt = conn.createStatement();
			// ResultSet result = stmt.executeQuery("select * from BlockHeader where hash =
			// " + headerHash);
			String sql = "select * from BlockHeader limit 1,1";
			PreparedStatement pre = conn.prepareStatement(sql);
			ResultSet result = pre.executeQuery();
			// Assert we get only one!!!!
			while (result.next()) {
				return result.getBytes(2);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
