package extractor.ui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.net.*;

import extractor.classToEjb.classToSession.*;
import extractor.classToEjb.classToEntity.*;
import ui.*;



public class EJBExtraction extends JDialog implements ActionListener
{
	private JPanel centerPanel = null;	//가운데 패널
	private JPanel bottomPanel = null;
	private JButton backBtn = new JButton("Back");
	private JButton nextBtn = new JButton("Next");
	private JButton cancelBtn = new JButton("Cancel");
	private JButton finishBtn = new JButton("Finish");
	private JLabel titleLabel;		//Title 레이블

	private InitialUI initialUI = null;
	private ExpectedUI expectedUI = null;
	//private EInputUI eInputUI = null;
	private SelectEBClass eInputUI = null;
	private FinderUI finderUI = null;
	private SeInputUI seInputUI = null;
	private SelectSBClass selectsbclass = null;
	private ProgressEB progressEBUI = null;
	private ResultEntityBean ResultEntityBeanUI = null;
	private ProgressSB progressSBUI = null;
	private ResultSessionBean ResultSessionBeanUI = null;
	
	private int currentP = 0;			//핸재 패널
	private boolean normalExitFlag = false;
	
	private boolean existEntityBean = true;
	private boolean existSessionBean = true;
	private ExtractResultsPanel ejbName = null;
	private Vector ejbs = null;
	private Vector entityResult = null;
	private Vector resultSession = new Vector();
  	Vector v = new Vector();	
	

	public EJBExtraction(JFrame f, ExtractResultsPanel ejbName)
	{
		super(f, "EJB Extraction", true);
		this.ejbName = ejbName;
		currentP = 0;	
		initialize();
	}


	private void initialize()
	{
		centerPanel = new JPanel(new BorderLayout());	//가운데 패널
		bottomPanel = new JPanel(new FlowLayout());		//버튼 있는 패널
		titleLabel = new JLabel();
		
		titleLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		titleLabel.setFont(new Font("Arial", Font.BOLD|Font.ITALIC, 18));
		titleLabel.setHorizontalAlignment(SwingConstants.CENTER);

		getContentPane().add(titleLabel, BorderLayout.NORTH);
		getContentPane().add(centerPanel, BorderLayout.CENTER);
		getContentPane().add(bottomPanel, BorderLayout.SOUTH);

		JPanel btnPanel = new JPanel(new GridLayout(1, 2));		//맨아래 이동버튼
		JPanel btnPanel_left = new JPanel(new FlowLayout(FlowLayout.CENTER));
		JPanel btnPanel_right = new JPanel(new FlowLayout(FlowLayout.CENTER));
		btnPanel.add(btnPanel_left);
		btnPanel.add(btnPanel_right);
		btnPanel_left.add(backBtn);
		btnPanel_left.add(nextBtn);
		btnPanel_right.add(cancelBtn);
		btnPanel_right.add(finishBtn);

		backBtn.addActionListener(this);
		nextBtn.addActionListener(this);
		cancelBtn.addActionListener(this);
		finishBtn.addActionListener(this);

		bottomPanel.add("Center", btnPanel);		

		//setSize(600, 400);
		setSize(600, 500);
		
		addWindowListener(
			new WindowAdapter()
			{
				public void windowClosing(WindowEvent e)
				{
					dispose();
				}
			}
		);

//		selectEJB = new SelectEJB();	//맨 처음에 New Project 화면
//		centerPanel.add(selectEJB);				//New Project 패널

		initialUI = new InitialUI(ejbName);
		centerPanel.add(initialUI);

		backBtn.setEnabled(false);
		finishBtn.setEnabled(false);

		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		Rectangle rec = getBounds();
		setBounds(dim.width/2 - (rec.width/2), dim.height/2 - (rec.height/2), rec.width, rec.height);
		setVisible(true);
	}


	private void doInitialUINext()
	{
		initialUI.execute();

		if (initialUI.getEntity() == null || initialUI.getEntity().size() < 1)
		{
			existEntityBean = false;
		}
		else
		{
			existEntityBean = true;
		}
		
		if (initialUI.getSession() == null ||initialUI.getSession().size() < 1)
		{
			existSessionBean = false;
		}		
		else
		{
			existSessionBean = true;
		}
		

		if ( !existEntityBean && !existSessionBean)
		{
			//Insert messages
			JOptionPane.showMessageDialog(this,"추출할 컴포넌트가 없습니다!","Message",JOptionPane.INFORMATION_MESSAGE);	
//			dispose();
		}
		

		expectedUI = new ExpectedUI(initialUI.getEntity(), initialUI.getSession(), ejbName);
		centerPanel.removeAll();						
		centerPanel.add(expectedUI);			
		centerPanel.validate();		
	}


	private void doExpectedUINext()
	{
		//
		if ( !existEntityBean && !existSessionBean)
		{
			//Insert messages
			JOptionPane.showMessageDialog(this,"후보 컴포넌트가 없습니다!","Message",JOptionPane.INFORMATION_MESSAGE);	
			dispose();
		}
		else
		{
			if(existEntityBean)
			{
			        Object[] options = {"Yes",
			                            "No",
			                            "Cancel"};
			        int n = JOptionPane.showOptionDialog(this,
			                        "entity bean 추출을 시작할까요 ???",
			                        "entity bean extraction",
			                        JOptionPane.YES_NO_CANCEL_OPTION,
			                        JOptionPane.QUESTION_MESSAGE,
			                        null,
			                        options,
			                        options[2]);
			        if (n == JOptionPane.YES_OPTION) { 
		          	    //eInputUI = new EInputUI(ejbName);
		          	    ejbs = new Vector();
		          	    ejbs.add("Board");
		          	    //eInputUI = new SelectEBClass(ejbs, ejbName);
		          	    doSelectEBClassUI(ejbs, ejbName);
				    centerPanel.removeAll();						
				    centerPanel.add(eInputUI);			
				    centerPanel.validate();
				} else if (n == JOptionPane.NO_OPTION) { 
	  			    centerPanel.removeAll();
				    centerPanel.add(expectedUI);		    
			        } else if (n == JOptionPane.CANCEL_OPTION) {
			            dispose();
			        } else {
			            System.out.println("Please tell me what you want!");
			        }
			 }
			 else
			 {
			        Object[] session_options = {"Yes",
			                            "No",
			                            "Cancel"};
			        int nn = JOptionPane.showOptionDialog(this,
			                        "session bean 추출을 시작할까요 ???",
			                        "session bean extraction",
			                        JOptionPane.YES_NO_CANCEL_OPTION,
			                        JOptionPane.QUESTION_MESSAGE,
			                        null,
			                        session_options,
			                        session_options[2]);
			        if (nn == JOptionPane.YES_OPTION) { 
					//seInputUI = new SeInputUI(ExpectedUI.sess, ejbName);
					selectsbclass = new SelectSBClass(ExpectedUI.sess);
					centerPanel.removeAll();						
					//centerPanel.add(seInputUI);			
					centerPanel.add(selectsbclass, new BorderLayout().CENTER);
					centerPanel.validate();	
					currentP += 2;	
					nextBtn.setEnabled(true);
					finishBtn.setEnabled(false);
				} else if (nn == JOptionPane.NO_OPTION) { 
	  			    centerPanel.removeAll();
				    centerPanel.add(expectedUI);		    
			        } else if (nn == JOptionPane.CANCEL_OPTION) {
			            dispose();
			        } else {
			            System.out.println("Please tell me what you want!");
			        }
			 }
			 
			/*if ( existEntityBean)
			{		
				eInputUI = new EInputUI(ejbName);
				centerPanel.removeAll();						
				centerPanel.add(eInputUI);			
				centerPanel.validate();
			}
			else
			{
				seInputUI = new SeInputUI(ExpectedUI.sess, ejbName);
				centerPanel.removeAll();						
				centerPanel.add(seInputUI);			
				centerPanel.validate();	
				currentP += 2;	
				nextBtn.setEnabled(false);
				finishBtn.setEnabled(true);
			}*/
		}
		

	}
	
	/*public void doEInputUINext()
	{
		eInputUI.execute();
		
		finderUI = new FinderUI(ejbName);
		centerPanel.removeAll();						
		centerPanel.add(finderUI);			
		centerPanel.validate();			
	}*/

	public void doEInputUINext()
	{
		progressEBUI = new ProgressEB(ejbs);		
		centerPanel.removeAll();			
		centerPanel.add(progressEBUI);			
		centerPanel.validate();	
	}
	
	public void doFinderUINext()
	{
		finderUI.execute();
		JOptionPane.showMessageDialog(this,"Entity bean 추출을 완료합니다!","Message",JOptionPane.INFORMATION_MESSAGE);	
		
		if ( !existSessionBean)
		{
			dispose();
		}
		else
		{
			seInputUI = new SeInputUI(finderUI.getSession(), ejbName);
			centerPanel.removeAll();						
			centerPanel.add(seInputUI);			
			centerPanel.validate();			
		}
	}
	
	public void doSelectEBClassUI(Vector entity, ExtractResultsPanel ejbName)
	{
		eInputUI = new SelectEBClass(ejbs, ejbName);
		entityResult = eInputUI.execute();
		currentP = 1;
	}
	

	public void doSelectEBClassUINext()
	{
		
	}
		
	public void doProgressEBUINext()
	{
		EntityBeanExtractor eb = progressEBUI.execute();
		ResultEntityBeanUI = new ResultEntityBean(eb);
		centerPanel.removeAll();						
		centerPanel.add(ResultEntityBeanUI);			
		centerPanel.validate();			
		
	}	

	public void doResultEntityBeanNext()
	{
		ResultEntityBeanUI.execute();

	        Object[] session_options = {"Yes",
	                            "No",
	                            "Cancel"};
	        int nn = JOptionPane.showOptionDialog(this,
	                        "session bean 추출을 시작할까요 ???",
	                        "session bean extraction",
	                        JOptionPane.YES_NO_CANCEL_OPTION,
	                        JOptionPane.QUESTION_MESSAGE,
	                        null,
	                        session_options,
	                        session_options[2]);
	        if (nn == JOptionPane.YES_OPTION) { 
			//seInputUI = new SeInputUI(ExpectedUI.sess, ejbName);
			selectsbclass = new SelectSBClass(ExpectedUI.sess);
			resultSession = selectsbclass.execute();
			centerPanel.removeAll();						
			//centerPanel.add(seInputUI);			
			centerPanel.add(selectsbclass, new BorderLayout().CENTER);
			centerPanel.validate();	
			//currentP++;	
			nextBtn.setEnabled(true);
			finishBtn.setEnabled(false);
		} else if (nn == JOptionPane.NO_OPTION) { 
		    centerPanel.removeAll();
		    centerPanel.add(expectedUI);		    
	        } else if (nn == JOptionPane.CANCEL_OPTION) {
	            dispose();
	        } else {
	            System.out.println("Please tell me what you want!");
	        }		
	}
	
	private void doSInputUINext()
	{
		if(seInputUI != null)
		{
			seInputUI.execute();
		}
		else
		{
			JOptionPane.showMessageDialog(this,"Enterprise bean 추출을 완료합니다!","Message",JOptionPane.INFORMATION_MESSAGE);	
			System.out.println("I got it.");
		}
		

		// tree에 추출결과 보이기		
	 	
	 	Vector temp = new Vector();
	 	for(int i=0; i<EMetaforUI.beanName.size(); i++)
	 	{
	 		temp.add(EMetaforUI.beanName.get(i));
	 	}
	 	for(int i=0; i<SEMetaforUI.beanName.size(); i++)
	 	{
	 		temp.add(SEMetaforUI.beanName.get(i));
	 	}
	 	
	 	ejbName.setTreeNode(temp);
		
		
		//ejbName.addExtractionTab();  /// 송문섭's code
		
		dispose();	
	}
	
	private void doProgressSBUI()
	{
		progressSBUI = new ProgressSB(ExpectedUI.sess, resultSession);
		centerPanel.removeAll();						
		centerPanel.add(progressSBUI);			
		centerPanel.validate();			
	}
	
	private void doProgressSBUINext()
	{
		ResultSessionBeanUI = new ResultSessionBean(resultSession);
		centerPanel.removeAll();						
		centerPanel.add(ResultSessionBeanUI);			
		centerPanel.validate();			
	}

	public void actionPerformed(ActionEvent e) 
	{
		 if(e.getSource() == nextBtn)
		 {		//다음 버튼 클릭시
		 	System.out.println("currentP = " + currentP);
			switch (currentP)
			{
				case 0:
					doInitialUINext();
					backBtn.setEnabled(true);
					break;
				case 1:
					doExpectedUINext();
					break;
				case 2:
					doEInputUINext();
					if ( !existSessionBean)
					{
						nextBtn.setEnabled(false);
						finishBtn.setEnabled(true);
					}
					
					break;
				/*case 3:
					doFinderUINext();
					nextBtn.setEnabled(false);
					finishBtn.setEnabled(true);
					break;*/
				case 3:
					doProgressEBUINext();
					nextBtn.setEnabled(true);
					finishBtn.setEnabled(false);
					break;		
				case 4:
					doResultEntityBeanNext();
					nextBtn.setEnabled(true);
					finishBtn.setEnabled(false);
					break;										
				case 5:
					doProgressSBUI();
					break;
				case 6:
					doProgressSBUINext();
					nextBtn.setEnabled(false);
					finishBtn.setEnabled(true);
					break;				
				}
			currentP = currentP >= 6 ? 6 : ++currentP;
				
		}
		else if(e.getSource() == backBtn)
		{		//이전 버튼 클릭시
			switch (currentP)
			{
				case 1:		//New Project로 이동
					centerPanel.removeAll();
					centerPanel.add(initialUI);
					break;
				case 2:		//New Project로 이동
					centerPanel.removeAll();
					centerPanel.add(expectedUI);
					break;
				case 3:		//New Project로 이동
					centerPanel.removeAll();
					centerPanel.add(eInputUI);
					break;
				case 4:		//New Project로 이동
				
					if ( existEntityBean)
					{
						centerPanel.removeAll();
						centerPanel.add(finderUI);
					}
					else
					{
						currentP -= 2;
						centerPanel.removeAll();
						centerPanel.add(expectedUI);
					}
					break;
				case 5:		//New Project로 이동
					centerPanel.removeAll();
					centerPanel.add(seInputUI);
					break;
			}
			centerPanel.repaint();
			currentP = currentP <= 1 ? 0 : --currentP;
		}
		else if(e.getSource() == cancelBtn)
		{					//Cancel 버튼 클릭시
			normalExitFlag = false;
			dispose();
		}
		else if(e.getSource() == finishBtn)
		{	//Finish 버튼 클릭시
			normalExitFlag = true;
			doSInputUINext();			
		}
	
	}


	public boolean isNormalExit()
	{
		return normalExitFlag;
	}
}




 
