package extractor.ui.servletUI;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;

import java.util.*;

public class ServletMainTFUI extends JDialog implements ActionListener {
	
	private JPanel btnPane;
	private JButton okBtn;
	private JButton nextServletBtn;
	private JButton finishBtn;
	
	private int currentPane;
	private JDialog jd;
	Vector selected = new Vector();
	Dimension dim;
	ServletSplitPaneUI servletSplitPane;
	
	
	
	public ServletMainTFUI(JDialog jd, Vector selected)
	{
		super(jd, "서블릿을 클래스로 변환하는 화면입니다.", true);	
		
		this.jd = jd;
		this.selected = selected;
		
		dim = Toolkit.getDefaultToolkit().getScreenSize();				

		btnPane = new JPanel(new FlowLayout(FlowLayout.CENTER, 50, 0));
		
		okBtn = new JButton("OK");
		okBtn.addActionListener(this);
		nextServletBtn = new JButton("Next Servlet");		
		nextServletBtn.setEnabled(false);
		nextServletBtn.addActionListener(this);
		finishBtn = new JButton("Finish");		
		finishBtn.setEnabled(false);
		finishBtn.addActionListener(this);
		btnPane.add(okBtn);
		btnPane.add(nextServletBtn);
		btnPane.add(finishBtn);
				
		if(jd != null)
			setLocation(jd.getLocation().x+300, jd.getLocation().y+100);  
		
		getContentPane().setLayout(new BorderLayout(30, 10));
		servletSplitPane = new ServletSplitPaneUI(selected);
		
		currentPane = 0;
		getContentPane().add(servletSplitPane, BorderLayout.CENTER);		
		getContentPane().add(btnPane, BorderLayout.SOUTH);
		
		setSize((int)dim.getWidth()*2/3, (int)dim.getHeight()*2/3);
		
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					dispose();
				}
			}
		);		
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) 
	{
		Object obj = e.getSource();
		if(obj == okBtn)
		{
			System.out.println("I'm an ok button.");
			switch (currentPane)
			{
				/*case 0:
					addServletNameTFUI();
					nextServletBtn.setEnabled(false);
					break;*/
				case 0:
					addServletVariableTFUI();
					nextServletBtn.setEnabled(false);
					break;
				case 1:
					addServletMethodTFUI();
					nextServletBtn.setEnabled(false);
					break;
				case 2:
					addServletResultUI();
					if(selected.size() == 0)
					{
						okBtn.setEnabled(false);
						nextServletBtn.setEnabled(false);
						finishBtn.setEnabled(true);
					}
					else
						nextServletBtn.setEnabled(true);
					break;
			}
			
			//currentP = currentP >= 6 ? 6 : ++currentP;				


		}
		else if(obj == nextServletBtn)
		{
			System.out.println("I'm next servlet button.");
			if(selected.size() > 0)
			{
				getContentPane().remove(servletSplitPane);
				servletSplitPane = new ServletSplitPaneUI(selected);
				getContentPane().add(servletSplitPane, BorderLayout.CENTER);
				validate();
				currentPane = 0;
			}			
		}
		else if(obj == finishBtn)		
		{
			dispose();
		}			
	}
	
	private void addServletNameTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletNameTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletVariableTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletVariableTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletMethodTFUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletMethodTFUI(), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

	private void addServletResultUI()
	{
		servletSplitPane.getRightPane().removeAll();
		servletSplitPane.getRightPane().add(new ServletResultUI(jd), BorderLayout.CENTER);
		servletSplitPane.getRightPane().validate();
		
		currentPane++;
	}	

}