package net.woodyfolsom.cs6601.p3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

import net.woodyfolsom.cs6601.p3.domain.Company;
import net.woodyfolsom.cs6601.p3.domain.Headline;
import net.woodyfolsom.cs6601.p3.svc.HeadlineService;
import net.woodyfolsom.cs6601.p3.svc.YahooHeadlineServiceImpl;

@Component
public class HeadlinePuller {
	private static final File stockSymbolsCSV = new File("stock_symbols.csv");
	private static final int IO_EXCEPTION = 1;
	private static final int STOCK_SYMBOL_CSV_NOT_FOUND = 2;

	@Autowired
	HeadlineService mySQLHeadlineServiceImpl;
	@Autowired
	HeadlineService yahooHeadlineServiceImpl;
	
	public static void main(String... args) {
		ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"/AppContext.xml"});
		HeadlinePuller headlinePuller = context.getBean(HeadlinePuller.class);
		try {
			List<Company> fortune50 = headlinePuller.getFortune50(stockSymbolsCSV);
			for (Company company : fortune50) {
				System.out.println("Getting headlines for Fortune 50 company #" + company.getId() + " (" + company.getName() + ")...");
				Date today = new Date();
				List<Headline> headlines = headlinePuller.pullHeadlines(company.getStockSymbol(), today);
				headlinePuller.insertHeadlines(company.getStockSymbol(), today, headlines);
				System.out.println("Waiting 10 seconds to accommodate Yahoo throttling...");
				try {
					Thread.sleep(10000L);
				} catch (InterruptedException ie) {
					System.out.println("Interrupted while waiting, exiting");
					System.exit(0);
				}
			}
		} catch (FileNotFoundException fnfe) {
			System.out.println("Stock symbol CSV file does not exist: "
					+ stockSymbolsCSV);
			System.exit(STOCK_SYMBOL_CSV_NOT_FOUND);
		} catch (IOException ioe) {
			System.out.println("Stock symbol CSV file does not exist: "
					+ stockSymbolsCSV);
			System.exit(IO_EXCEPTION);
		}
	}

	private void insertHeadlines(String stockSymbol, Date date, List<Headline> headlines) {
		for (Headline headline : headlines) {
			mySQLHeadlineServiceImpl.insertHeadline(headline);
		}
	}
	
	private List<Headline> pullHeadlines(String stockSymbol, Date date) {
		List<Headline> headlines = yahooHeadlineServiceImpl.getHeadlines(stockSymbol, date);
		for (Headline headline : headlines) {
			System.out.println("Got headline: " + headline);
		}
		
		return headlines;
	}
	
	private List<Company> getFortune50(File csvFile) throws FileNotFoundException,
			IOException {
		List<Company> fortune50 = new ArrayList<Company>();
		FileInputStream fis = new FileInputStream(csvFile);
		InputStreamReader reader = new InputStreamReader(fis);
		BufferedReader buf = new BufferedReader(reader);
		String csvline = null;
		while ((csvline = buf.readLine()) != null) {
			if (csvline.length() == 0) {
				continue;
			}
			String[] fields = csvline.split(",");
			if (fields.length != 3) {
				throw new RuntimeException("Badly formatted csv file name (3 values expected): " + csvline);
			}
			int id = Integer.valueOf(fields[0]);
			fortune50.add(new Company(id,fields[1],fields[2]));
		}
		return fortune50;
	}
}
