package finanny;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;//A multi-line region that displays text
import javax.swing.JTextField;//Allows for the editing of a single line of text.

// Declaration of our presentation manager. This class operates at the presentation
// level. It will listen for mouse clicks on the checkboxes
public class PresentationManager implements ActionListener {
	JFrame frame;
	JPanel symbolPanel;
	JPanel optionsPanel;
	JPanel outputPanel;
	JButton submitButton;
	JTextArea textArea;
	JTextField stockSymbol;
	ArrayList<JCheckBox> checkBoxes = new ArrayList<>();
	StockQuote stockQuote;

	void init() {
		// Initialize the options you want as well as the output panel
		initOptionsPanel();
		initOutputPanel();
		// Initialize the main frame
		initFrame();
		// Instantiate (or create) the stockQuote object.
		stockQuote = new StockQuote();
	}

	void initOptionsPanel() {
		// The options will be organized as a flow starting from the left side of the
		// frame
		symbolPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		stockSymbol = new JTextField(10);// Enter the stock symbol in this text field
		JPanel stockSymbolPanel = new JPanel();// Graphical container for the stock symbol
		stockSymbolPanel.add(stockSymbol);
		JLabel stockLabel = new JLabel("Stock Symbol");// Label for the stock symbol
		JPanel stockLabelPanel = new JPanel();
		stockLabelPanel.add(stockLabel);// Add the label to the label panel
		symbolPanel.add(stockLabelPanel);// Add the label and the text field to the symbol panel
		symbolPanel.add(stockSymbolPanel);

		optionsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));// Create the options panel
		optionsPanel.setLayout(new GridLayout(3, 1));
		checkBoxes.add(new JCheckBox("Global Quote"));// Add checkboxes for each option
		checkBoxes.add(new JCheckBox("Realtime Bulk Quote"));
		checkBoxes.add(new JCheckBox("Realtime Options"));
		checkBoxes.add(new JCheckBox("Historical Options"));
		JPanel checkBoxesPanel = new JPanel(new GridLayout(6, 6));// Arrange them 6 by 6
		for (JCheckBox checkBox : checkBoxes) {
			checkBoxesPanel.add(checkBox);// Add the checkboxes to their respective panels
		}
		// checkBoxes.stream().forEach((checkBox) -> {//Another way to go through the
		// loop
		// checkBoxesPanel.add(checkBox);
		// });

		submitButton = new JButton("Submit");// Create the submit button
		submitButton.addActionListener(this);
		JPanel submitButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		submitButtonPanel.add(submitButton);// Add the submit button to a panel

		optionsPanel.add(symbolPanel);// Add the symbol, checkboxes and submit button to the options panel
		optionsPanel.add(checkBoxesPanel);
		optionsPanel.add(submitButtonPanel);

		optionsPanel.setPreferredSize(new Dimension(500, 200));
	}

	void initOutputPanel() {
		// Create the output panel
		outputPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		// Create the text area for the output data
		textArea = new JTextArea("Stock Data");
		textArea.setBounds(50, 50, 250, 400);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		outputPanel.add(textArea);
		outputPanel.setPreferredSize(new Dimension(500, 400));
	}

	void initFrame() {
		// Initialize our frame
		frame = new JFrame("Financial Analysis");
		// Divide the frame in two: the options panel and the output panel
		frame.setLayout(new GridLayout(2, 1));
		frame.getContentPane().add(optionsPanel, BorderLayout.NORTH);
		frame.getContentPane().add(outputPanel, BorderLayout.SOUTH);
		// Pack everything together and make visible
		frame.pack();
		frame.setVisible(true);

	}

	// Override the actionPerformed in our action listener with our own handler
	@Override
	public void actionPerformed(ActionEvent e) {
		String functionString = new String();
		ArrayList<String> tagNames = new ArrayList<>();
		tagNames.add(stockSymbol.getText());
		// We are going to pass the function string to the stockQuote class
		for (JCheckBox checkBox : checkBoxes) {
			if (checkBox.isSelected() == true) {
				String checkBoxText = checkBox.getText();
				tagNames.add(checkBoxText);
				switch (checkBoxText) {
					case "Global Quote":
						functionString = "GLOBAL_QUOTE";
						break;
					case "Realtime Bulk Quote":
						functionString = "REALTIME_BULK_QUOTES";
						break;
					case "Realtime Options":
						functionString = "REALTIME_OPTIONS";
						break;
					case "Historical Options":
						functionString = "HISTORICAL_OPTIONS";
						break;
				}
			}
		}
		// Send the tag names of each option and the tag string to our stockQuote class.
		String result = stockQuote.printStockQuote(stockSymbol.getText(), tagNames, functionString);
		// Display the result in the text area of our output panel.
		System.out.println(result);
		textArea.setText(result);
	}
}
