/**
 * 
 */
package org.mynonglam.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mynonglam.connectdb.ConnectionDTB;
import org.mynonglam.entitys.Place;

/**
 * @author Dao
 *
 */
public class PlaceDAO {
	
	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;

	/**
	 * 
	 */
	public PlaceDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public List<Place> getListPlaceForAccount(String idacc) {
		List<Place> result = new ArrayList<Place>();
		
		String sql = "SELECT idplace, address, city, startyear, endyear, livinghere FROM place where email=?";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				Place temp = new Place();
				temp.setIdplace(resultSet.getString("idplace"));
				temp.setAddress(resultSet.getString("address"));
				temp.setCity(resultSet.getString("city"));
				temp.setStartyear(resultSet.getString("startyear"));
				temp.setEndyear(resultSet.getString("endyear"));
				temp.setLivinghere(resultSet.getBoolean("livinghere"));
				
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Place getPlaceForAccount(String idplace) {
		Place result = null;
		
		String sql = "SELECT idplace, address, city, startyear, endyear, livinghere FROM place where idplace=?";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idplace);
			
			resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Place();
				result.setIdplace(resultSet.getString("idplace"));
				result.setAddress(resultSet.getString("address"));
				result.setCity(resultSet.getString("city"));
				result.setStartyear(resultSet.getString("startyear"));
				result.setEndyear(resultSet.getString("endyear"));
				result.setLivinghere(resultSet.getBoolean("livinghere"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean insertPlaceAccount(Place place) {
		boolean result = false;
		String sql = "INSERT INTO place(idplace, email, address, city, startyear, endyear, livinghere) VALUES (?,?,?,?,?,?,0)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, place.getIdplace());
			statement.setString(2, place.getAccount().getEmail());
			statement.setString(3, place.getAddress());
			statement.setString(4, place.getCity());
			statement.setString(5, place.getStartyear());
			statement.setString(6, place.getEndyear());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean updatePlace(Place place) {
		boolean result = false;
		String sql = "UPDATE place SET address=?, city=?, startyear=?, endyear=? WHERE idplace=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, place.getAddress());
			statement.setString(2, place.getCity());
			statement.setString(3, place.getStartyear());
			statement.setString(4, place.getEndyear());
			statement.setString(5, place.getIdplace());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean deletePlace(String idplace, String email) {
		boolean result = false;
		String sql = "DELETE FROM place WHERE idplace=? AND email=?";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idplace);
			statement.setString(2, email);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDPlaceLast() {
		String result = "";
		
		String sql = "SELECT idplace FROM place ORDER BY idplace DESC LIMIT 0,1";
		try {
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idplace");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}

}
