

//
// Core Program for TETDM 
// PopUpWindow.java Version 0.44
// Copyright(C) 2012-2014 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.
//

package source;

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


public class PopUpForInterpretation extends JFrame
{
	InterpretationPanel interpretationPanel;
	
	int posX, posY;
	int width, height;	
	
	PopUpForStar popForStar[];
	int starNumber = 0;
	int maxStarNumber = 10;
	
	int fontSize;
	
	String inJapanese[];
	
	Image image;
	MiningModule mining;
	Control control;
	Select select;
		
	PopUpForInterpretation(String imagePath, MiningModule mining, Control control, Select select)
	{
		super("Result and Interpretation");

		
		this.mining = mining;
		this.control = control;
		this.select = select;
		
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		DisplayMode displayMode = env.getDefaultScreenDevice().getDisplayMode();		
		
		int windowSizeX = displayMode.getWidth();

		width = windowSizeX/3;
		
		if(width > 640)
			width = 640;
		
		if(width < 480)
			width = 480;
		
		height = width/3 + 150;
		
		if(control.panelNumber == 1)
		{
			posX = (windowSizeX-width)/2;
		}
		else
		{
			if(select.panelID <= control.panelNumber/2)
				posX = (select.panelID+1) * (windowSizeX/control.panelNumber);
			else
				posX = (select.panelID) * (windowSizeX/control.panelNumber)-width;			
		}
		
		posY = 200;
					
		if(posX > windowSizeX - width)
			posX = windowSizeX - width;

		fontSize = 20;
		if(width < 640)
			fontSize = 14;
		
		
		setBounds(posX, posY, width, height);
		
		setAlwaysOnTop(true);
				
		this.addWindowListener(new MyWindowListener());		
		
		ImageIcon icon = new ImageIcon(imagePath);
		image = icon.getImage();		

		starNumber = 0;
		popForStar = new PopUpForStar[maxStarNumber];
		
		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "JapaneseInterpretation.txt"));		
		interpretationPanel = new InterpretationPanel();		
		add(interpretationPanel);
		
		setVisible(true);		
	}
	
	public void subWindowClose()
	{
		for(int i=0;i<starNumber;i++)
		{
			popForStar[i].dispose();
		}		
	}
	
	public void quit()
	{
		dispose();
	}
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e) // x button
		{
			subWindowClose();
			select.interpretation.setSelected(false);
		}		
		
		public void windowClosed(WindowEvent e) 
		{
			subWindowClose();
			select.interpretation.setSelected(false);
		}
	}
	
	class InterpretationPanel extends JPanel implements ActionListener
	{
		JLabel textLabel[];
		JTextField textField[];
		
		JLabel importance;
		JRadioButton jRadioButton[];
		ButtonGroup group;
		
		JPanel radioButtonPanel;
		JPanel buttonPanel;
		
		JButton star;		

		JButton input;
		JButton quit;
		
		InterpretationPanel()
		{
			setLayout(new GridLayout(16,1));
			setBackground(new Color(0xD3,0xED,0xFB));
			initialization();
		}
		
		public void initialization()
		{

			
			textLabel = new JLabel[11];
			textField = new JTextField[2];
			
			jRadioButton = new JRadioButton[5];
			
			if(control.sp1.inJapanese)
			{
				textLabel[0] = new JLabel(inJapanese[13]+" "+(select.panelID+1)+": "+
										  select.miningModuleNamesInJapanese[select.selected_module]+" + "+
										  select.visualizationModuleNamesInJapanese[select.pair_visu[select.selected_module]] +
										  " ("+new File(select.text.sourceFilename).getName()+") ");
				textLabel[1] = new JLabel(inJapanese[0]);
				textLabel[2] = new JLabel(inJapanese[1]);
				textLabel[3] = new JLabel(inJapanese[2]);
				textLabel[4] = new JLabel(inJapanese[3]);
				textLabel[5] = new JLabel(inJapanese[4]);
				textLabel[6] = new JLabel(inJapanese[5]);	
				textLabel[7] = new JLabel(inJapanese[6]);
				textLabel[8] = new JLabel(inJapanese[7]);
				textLabel[9] = new JLabel(inJapanese[8]);
				textLabel[10] = new JLabel(inJapanese[9]);				
				
				importance = new JLabel(inJapanese[11]);
				
				star = new JButton(inJapanese[10]);

				input = new JButton(inJapanese[12]);
				quit = new JButton(inJapanese[14]);
			}
			else
			{
				textLabel[0] = new JLabel("  PANEL "+(select.panelID+1)+": "+
										  select.module_names[select.selected_module]+" + "+
										  select.visu_module_names[select.pair_visu[select.selected_module]]+
										  " ("+new File(select.text.sourceFilename).getName()+") ");
				textLabel[1] = new JLabel("  [PROCEDURES FOR INTERPRETATION]");
				textLabel[2] = new JLabel("  Step1: Display STAR(s) by pushing Add '!' Button and arrange on a result where you noticed");				
				textLabel[3] = new JLabel("  Step2: Write concrete <RESULT> about the arranged STAR");
				textLabel[4] = new JLabel("  Step3: Write <INTERPRETATION> as the meaning of the <RESULT>");
				textLabel[5] = new JLabel("  Step4: Select [IMPORTANCE] of the <INTERPRETATION>");				
				textLabel[6] = new JLabel("  Step5: Push <SUBMIT> (Save RESULT, INTERPRETATION, IMPORTANCE, IMAGE)");
				textLabel[7] = new JLabel("  * RESULT and INTERPRETATION are desired to be written concretely");				
				textLabel[8] = new JLabel("  * Registered INTERPRETATIONS are confirmed from LIST AND CREATION Button on the menu window");								
				textLabel[9] = new JLabel("  <RESULT>");
				textLabel[10] = new JLabel("  <INTERPRETATION>");


				
				importance = new JLabel(" [IMPORTANCE]");
				
				star = new JButton("Add !");

				input = new JButton("[SUBMIT]");
				quit = new JButton("Quit");				
			}
			
			for(int i=0;i<11;i++)
				if(i >= 0 && i <= 8)
					textLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize-4));
				else
					textLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
			textLabel[1].setForeground(new Color(0x00,0x64,0x00));
			textLabel[7].setForeground(Color.blue);
			textLabel[8].setForeground(Color.blue);			
			textLabel[8].setBackground(Color.green);
			textLabel[8].setOpaque(true);			
			
			importance.setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			
			textField[0] = new JTextField();
			textField[1] = new JTextField();	
			
			for(int i=0;i<2;i++)
				textField[i].setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));			
			
			for(int i=0;i<5;i++)
				jRadioButton[i] = new JRadioButton(""+(5-i));
			jRadioButton[4].setSelected(true);
			group = new ButtonGroup();
			
			//panel
			textLabel[0].setBackground(Color.pink);
			textLabel[0].setOpaque(true);
			add(textLabel[0]);

			//star
			star.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
			star.setForeground(Color.red);			
			star.addActionListener(this);			
			add(star);			
			
			//result
			textLabel[9].setBackground(Color.green);
			textLabel[9].setOpaque(true);			
			add(textLabel[9]);
			add(textField[0]);	
			
			//interpretation
			textLabel[10].setBackground(Color.yellow);
			textLabel[10].setOpaque(true);			
			add(textLabel[10]);			
			add(textField[1]);			
			

			//importance
			radioButtonPanel = new JPanel();
			radioButtonPanel.setOpaque(true);
			radioButtonPanel.setBackground(Color.yellow);
			radioButtonPanel.setLayout(new GridLayout(1,6));			

			radioButtonPanel.add(importance);
			
			for(int i=0;i<5;i++)
			{
				jRadioButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
				radioButtonPanel.add(jRadioButton[i]);
				group.add(jRadioButton[i]);
			}
			
			add(radioButtonPanel);
			
			
			input.setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			input.setForeground(Color.blue);
			input.addActionListener(this);
			
			quit.setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			quit.addActionListener(this);
			
			buttonPanel = new JPanel();
			buttonPanel.setOpaque(false);
			buttonPanel.setLayout(new GridLayout(1,2));
			
			buttonPanel.add(input);
			buttonPanel.add(quit);			
			
			add(buttonPanel);
			
			add(textLabel[1]);
			add(textLabel[2]);
			add(textLabel[3]);
			add(textLabel[4]);
			add(textLabel[5]);
			add(textLabel[6]);
			add(textLabel[7]);			
			add(textLabel[8]);			

		}
		
		//Action listener
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == star)
			{
				if(starNumber < maxStarNumber)
				{
					popForStar[starNumber] = new PopUpForStar(posX,posY,50,50,image,true);
					starNumber++;
				}
				
			}
			
			if(e.getSource() == input)
			{
				String result = textField[0].getText();
				String meaning = textField[1].getText();
				
				if(result.equals("") || meaning.equals(""))
					return;
				
                control.getPanelInformation();
                int miningIDs[] = new int[control.panelNumber];
                int visualizationIDs[] = new int[control.panelNumber];
                int textIDs[] = new int[control.panelNumber];
                int sizeX[] = new int[control.panelNumber];
                
                for(int i=0;i<control.panelNumber;i++)
                {
                    miningIDs[i] = control.miningIDs[i];
                    visualizationIDs[i] = control.visualizationIDs[i];
                    textIDs[i] = control.textIDs[i];
                    sizeX[i] = control.balancedPanelSizeX[i];
                }
                
				int importanceRate = 1;
				for(int i=0;i<5;i++)
					if(jRadioButton[i].isSelected())
					{
						importanceRate = 5-i;
						break;
					}
                
				if(control.interpretationNumber < control.maxInterpretationNumber)
				{
					System.out.println("Create situation"+(control.interpretationNumber+1)+".png");
					mining.imageCapture(control.absolutePath + "interpretation" + File.separator + "situation"+(control.interpretationNumber+1)+".png");//must before add Interpretation
					
					control.addInterpretation(new File(select.text.sourceFilename).getName(), result, meaning, importanceRate, miningIDs, visualizationIDs, textIDs, sizeX, select.panelID);

					control.writeInterpretation(control.interpretationNumber);
					
//					control.interpretation[control.interpretationNumber-1].icon = new ImageIcon(control.absolutePath+"interpretation"+File.separator+"situation"+(control.interpretationNumber)+".png");
//					control.interpretation[control.interpretationNumber-1].smallImg = control.interpretation[control.interpretationNumber-1].icon.getImage().getScaledInstance(160, 200, Image.SCALE_SMOOTH);
//					control.interpretation[control.interpretationNumber-1].smallIcon = new ImageIcon(control.interpretation[control.interpretationNumber-1].smallImg);
					
					System.out.println("RESULT = "+result);
					System.out.println("INTERPRETATION = "+meaning);
					System.out.println("IMPORTANCE = "+importanceRate);
					
					textField[0].setText("");
					textField[1].setText("");					
				}
				
				//FOR MISSION
				control.interpretationMission();
			}
			
			if(e.getSource() == quit)
			{
				subWindowClose();
				select.interpretation.setSelected(false);
				quit();
			}
			
		}		
	}		
}
