package control;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import boundary.DatabaseCommunicator;

public class UC3Controller 
{
	private static String investorsqlquery = "SELECT * FROM investor";
	public UC3Controller()
	{
		
	}
	
	public List getInvestors(){
		List investorList = new ArrayList();
		// String query = "SELECT email FROM user WHERE userid = user123;";
		ResultSet resultSet = DatabaseCommunicator.query(investorsqlquery);
		try{
			while(resultSet.next()){
				Investor inv = new Investor();
				inv.loadFromResultSet(resultSet);
				investorList.add(inv);
			}
		}catch(Exception e){
			e.printStackTrace();
			System.out.println("Couldn't retrieve email addresses."+e);
		}
		
		return investorList;
	}

	public static void main(String[] args) {
		
		// BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		ResultSet rs = null;
		System.out.println(DatabaseCommunicator.connect());
		UC3Controller uc3 = new UC3Controller();
		List invlist = uc3.getInvestors();
		
		MailHeaderParameters mhp = new MailHeaderParameters();
		mhp.setSmtpHostName("smtp.gmail.com");
		mhp.setSubject("This is your portfolio");
		mhp.setFromAddress("srinathreddy035@gmail.com");
		
		Mailer ml = Mailer.getInstance();
		
		Iterator ite = invlist.iterator();
		while (ite.hasNext())
		{
			Investor invs = (Investor) ite.next();
			try {
				String[] emailaddresses = new String[1];
				emailaddresses[0] = invs.getEmail();
				mhp.setToAddresses(emailaddresses);
				ml.sendMail1(mhp, 
						" " + invs.getFirstName() 
						+ "\n Total Gain = " + invs.getTotalGain() 
						+ "\n Current Available Funds = " + invs.getCurrentFunds(),
						false);
			}
			catch (Exception e)
			{
				System.out.println("Error Message: " + e.getMessage());
			}
		}
	}
}
