/**
 * 
 */
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.Phone;

/**
 * @author Dao
 *
 */
public class PhoneDAO {

	private static Connection connection;
	private static PreparedStatement statement = null;
	private ResultSet resultSet;
	
	/**
	 * 
	 */
	public PhoneDAO() {
		connection = ConnectionDTB.getConnection();
	}


	public List<Phone> getPhoneForAccount(String idacc) {
		List<Phone> result = new ArrayList<Phone>();
		
		String sql = "SELECT idphone, number FROM phone where email=?";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idacc);
			
			resultSet = statement.executeQuery();
			while(resultSet.next()) {
				Phone temp = new Phone();
				temp.setIdphone(resultSet.getString("idphone"));
				temp.setNumber(resultSet.getString("number"));
				
				result.add(temp);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	public Phone getPhoneForAccountByIDPhone(String idphone) {
		Phone result = null;
		
		String sql = "SELECT idphone, number FROM phone where idphone=? ";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idphone);
			
			resultSet = statement.executeQuery();
			if(resultSet.next()) {
				result = new Phone();
				result.setIdphone(resultSet.getString("idphone"));
				result.setNumber(resultSet.getString("number"));
			}
		} catch (SQLException e) {
			result = null;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean insertPhone(Phone phone) {
		boolean result = false;
		String sql = "INSERT INTO phone(idphone, email, number) VALUES (?,?,?)";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, phone.getIdphone());
			statement.setString(2, phone.getAccount().getEmail());
			statement.setString(3, phone.getNumber());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public boolean updatePhone(Phone phone) {
		boolean result = false;
		String sql = "UPDATE phone SET number=? WHERE idphone=?";
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, phone.getNumber());
			statement.setString(2, phone.getIdphone());
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
	public String getIDPhoneLast() {
		String result = "";
		
		String sql = "SELECT idphone FROM phone ORDER BY idphone DESC LIMIT 0,1";
		try {
			statement = connection.prepareStatement(sql);
			resultSet = statement.executeQuery();
			
			if(resultSet.next()) {
				result = resultSet.getString("idphone");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return result;
	}


	public boolean deletePhoneNumber(String idphone, String email) {
		boolean result = false;
		String sql = "DELETE FROM phone WHERE idphone=? AND email=?";
		
		try {
			statement = connection.prepareStatement(sql);
			statement.setString(1, idphone);
			statement.setString(2, email);
			
			if(statement.executeUpdate() > 0) {
				result = true;
			}
		} catch (SQLException e) {
			result = false;
			e.printStackTrace();
		}
		
		return result;
	}
	
}
