package studentClient;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.*;

import common.components.layout.QuestionLayout;
import common.components.actionListeners.NavIconActionListener;
import common.components.actionListeners.ReturnActionListener;

public class StudentTest extends JPanel {
	// The "Navigation Icons" at the top.
	JPanel navIconsPanel;
	JButton testTitleButton;
	JButton testDescriptionButton;
	ArrayList<JButton> navIcons;
	
	// The left/right navigations at the bottom.
	JPanel navButtonsPanel;
	JButton navLeft;
	JButton navRight;
	
	int numQuestions;
	int curPanel;
	JPanel questionPanel; // Panel that holds all question panels.
	
	boolean agreed;	// Whether or not the student has agreed not to cheat.
		
	/* TODO: This constructor won't take an int */
	public StudentTest(int numQuestions) {
		this.numQuestions = numQuestions;
		
		agreed = false;

		// Nav icons.
		navIcons = new ArrayList<JButton>();
		
		testTitleButton = new JButton("Title");
		testTitleButton.addActionListener(new NavIconActionListener(this, 0));
		
		testDescriptionButton = new JButton("Description");
		testDescriptionButton.addActionListener(new NavIconActionListener(this, 1));
		
		navIconsPanel = new JPanel();
		navIconsPanel.setLayout(new BoxLayout(navIconsPanel, BoxLayout.LINE_AXIS));
		navIconsPanel.add(testTitleButton);
		navIconsPanel.add(testDescriptionButton);
		for (int i = 0; i < numQuestions; ++i) {
			JButton curButton = new JButton("" + i);
			curButton.addActionListener(new NavIconActionListener(this, i + 2));
			curButton.setEnabled(false);
			navIcons.add(curButton);
			navIconsPanel.add(curButton);
		}
		
		// Question panels.
		questionPanel = new JPanel();
		questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.LINE_AXIS));
		questionPanel.add(new TitlePanel("Sample Title", "Sample Subtitle"));
		questionPanel.add(new DescriptionPanel(this, "Sample Description"));
		for (int i = 0; i < numQuestions; ++i) {
			QuestionLayout temp = new QuestionLayout(i, "This is a sample question.", new JTextArea(5, 15));
			questionPanel.add(temp);
		}
		
		// Nav buttons.
		navLeft = new JButton("<-");
		navLeft.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setCurrentPanel(curPanel - 1);
			}
		});
		navRight = new JButton("->");
		navRight.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setCurrentPanel(curPanel + 1);
			}
		});
		navButtonsPanel = new JPanel();
		navButtonsPanel.setLayout(new BoxLayout(navButtonsPanel, BoxLayout.LINE_AXIS));
		navButtonsPanel.add(navLeft);
		navButtonsPanel.add(navRight);
		
		setCurrentPanel(0);
		
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		this.add(navIconsPanel);
		this.add(questionPanel);
		this.add(navButtonsPanel);
				
		this.setVisible(true);
	}
	
	public void setCurrentPanel(int panelNum) {
		if (panelNum >= 0 && panelNum < numQuestions + 2) {
			questionPanel.getComponent(curPanel).setVisible(false);
			questionPanel.getComponent(panelNum).setVisible(true);
			curPanel = panelNum;
		}
		
		if (panelNum == 0)
			navLeft.setEnabled(false);
		else
			navLeft.setEnabled(true);
		
		if (panelNum == 1 && !agreed)
			navRight.setEnabled(false);
		else
			navRight.setEnabled(true);
	}

	public void agreed() {
		agreed = true;
		
		for (JButton button : navIcons) 
			button.setEnabled(true);
		
		setCurrentPanel(2);
	}
}