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

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

/**
 * @author Dao
 *
 */
public class AccountDAO {
	
	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	/**
	 * 
	 */
	public AccountDAO() {
		connection = ConnectionDTB.getConnection();
	}
	
	public boolean insertAccount(Account account) {
		boolean result = false;
		
		String sql = "INSERT INTO account(email, fullname, password, gender, dayofbirth, avatar, coverphoto, isblock, country) VALUES (?,?,?,?,?,'images/no-avatar-men.jpg','images/winter3.jpg',0, '')";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, account.getEmail());
			statement.setString(2, account.getFullname());
			statement.setString(3, account.getPassword());
			statement.setBoolean(4, account.getGender());
			statement.setString(5, account.getDayofbirth());
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean checkEmailExits(String username) {
		boolean result = false;
		
		String sql = "SELECT email FROM account where email=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean checkLogin(String username, String password) {
		boolean result = false;
		
		String sql = "SELECT email FROM account where email=? and password=?";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			statement.setString(2, password);
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean checkIsClock(String username) {
		boolean result = false;
		
		String sql = "SELECT email FROM account where email=? and isblock=0";
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, username);
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}
	
	public Account getAccount(String idacc) {
		Account result = null;
		String sql = "SELECT email, fullname, gender, dayofbirth, country, avatar, coverphoto, orthername FROM account where email=?";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Account();
				result.setEmail(resultSet.getString("email"));
				result.setFullname(resultSet.getString("fullname"));
				result.setGender(resultSet.getBoolean("gender"));
				result.setDayofbirth(resultSet.getString("dayofbirth"));
				result.setCountry(resultSet.getString("country"));
				result.setAvatar(resultSet.getString("avatar"));
				result.setCoverphoto(resultSet.getString("coverphoto"));
				result.setOrthername(resultSet.getString("orthername"));
				
				result.setPhones(new PhoneDAO().getPhoneForAccount(idacc));
				
				result.setProfesstions(new ProfesstionDAO().getListProfesstionForAccount(idacc));
				
				result.setPlaces(new PlaceDAO().getListPlaceForAccount(idacc));
				
				result.setLiking(new LikingDAO().getLinkingForAccount(idacc));
			}
		} catch (SQLException e) {
			result = null;
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updateInfo(Account account) {
		boolean result = false;
		
		String sql = "UPDATE account SET gender=?, dayofbirth=?, country=? WHERE email=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setBoolean(1, account.getGender());
			statement.setString(2, account.getDayofbirth());
			statement.setString(3, account.getCountry());
			statement.setString(4, account.getEmail());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Account getAccountInfoByComment(String idacc) {
		Account result = null;
		String sql = "SELECT email, fullname, avatar, coverphoto FROM account where email=?";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			ResultSet resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Account();
				result.setEmail(resultSet.getString("email"));
				result.setFullname(resultSet.getString("fullname"));
				result.setAvatar(resultSet.getString("avatar"));
				result.setCoverphoto(resultSet.getString("coverphoto"));
			}
		} catch (SQLException e) {
			result = null;
			e.printStackTrace();
		}
		return result;
	}
	
	public static void main(String[] args) {
//		System.out.println(new AccountDAO().getAccountInfoByComment("dlduynam@gmail.com"));
	}
	
}
