

//
// Core Program for TETDM 
// PopUpForInterpretation.java Version 1.00
// Copyright(C) 2012-2018 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;
	Control control;
    
	String windowName = "INTERPRETATION";
    
    final int goalNumber = 10;  // 10 or 5 /////////////////////
    
    static boolean firstActivation = true;
    
		
//    PopUpForInterpretation(String imagePath, MiningModule mining, Control control, Select select, int panelID, String windowTitle)
        PopUpForInterpretation(String imagePath, Control control, String windowTitle)
	{
		super(windowTitle);

		this.control = control;
		
		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 + 250;
		
        posX = (windowSizeX-width)/2;
		if(posX > windowSizeX - width)
			posX = windowSizeX - width;

        posY = 200;
        
		fontSize = 18;
		if(width < 640)
			fontSize = 14;
		
		
		setBounds(posX, posY, width, height);
		
//		setAlwaysOnTop(true);
				
        WINDOWACTIONLOG windowActionLog = new WINDOWACTIONLOG(this, windowName);
        this.addComponentListener(windowActionLog.componentListener);                    //FOR LOG
        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 + "japaneseText" + File.separator+ "JapaneseInterpretation.txt"));		
		interpretationPanel = new InterpretationPanel(this);
		add(interpretationPanel);
		
		setVisible(true);
				
	}
	
	public void subWindowClose()
	{
		for(int i=0;i<starNumber;i++)
		{
            if(popForStar[i] != null)
            {
                popForStar[i].dispose();
                popForStar[i] = null;
            }
		}
	}

	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e) // x button
		{
			subWindowClose();
			control.bt79.setSelected(false);
		}		
		
		public void windowClosed(WindowEvent e) 
		{
			subWindowClose();
			control.bt79.setSelected(false);
		}
	}
	

	
	class InterpretationPanel extends JPanel implements ActionListener
	{
        JRadioButton panelSelectRadioButton[];
        
		JLabel textLabel[];
		JTextField textField[];
		

		JLabel importance;
		JRadioButton jRadioButton[];
		ButtonGroup group;
		
		JPanel radioButtonPanel;
		JPanel buttonPanel;
		
		JButton star;		

		JButton input;
		JButton quit;
        
        JFrame parentFrame;
		
		InterpretationPanel(JFrame parentFrame)
		{
            this.parentFrame = parentFrame;
			setLayout(new GridLayout(16+control.panelNumber,1));
            setBackground(new Color(0xD3,0xED,0xFB));
			initialization();
		}
		
		public void initialization()
		{
            panelSelectRadioButton = new JRadioButton[control.panelNumber];
			
			textLabel = new JLabel[11];
			textField = new JTextField[2];
			
			jRadioButton = new JRadioButton[5];
			
			if(control.sp1.inJapanese)
			{
                for(int i=0;i<control.panelNumber;i++)
                    panelSelectRadioButton[i] = new JRadioButton(inJapanese[14]+" "+(i+1)+": "+
                                               control.panel[i].miningModuleNamesInJapanese[control.panel[i].selected_module]+" + "+
                                               control.panel[i].visualizationModuleNamesInJapanese[control.panel[i].pair_visu[control.panel[i].selected_module]] +
                                               " ("+new File(control.panel[i].text.sourceFilename).getName()+") ");
                /*
				textLabel[0] = new JLabel(inJapanese[14]+" "+(select.panelID+1)+": "+
										  select.miningModuleNamesInJapanese[select.selected_module]+" + "+
										  select.visualizationModuleNamesInJapanese[select.pair_visu[select.selected_module]] +
										  " ("+new File(select.text.sourceFilename).getName()+") ");
                 */
                for(int i=0;i<11;i++)
                    textLabel[i] = new JLabel(inJapanese[i]);
				
				importance = new JLabel(inJapanese[12]);
				
				star = new JButton(inJapanese[11]);

				input = new JButton(inJapanese[13]);
				quit = new JButton(inJapanese[15]);
			}
			else
			{
                for(int i=0;i<control.panelNumber;i++)
                    panelSelectRadioButton[i] = new JRadioButton("  PANEL "+(i+1)+": "+
                                               control.panel[i].module_names[control.panel[i].selected_module]+" + "+
                                               control.panel[i].visu_module_names[control.panel[i].pair_visu[control.panel[i].selected_module]]+
                                               " ("+new File(control.panel[i].text.sourceFilename).getName()+") ");
                /*
				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[0] = new JLabel("  [PROCEDURES FOR INTERPRETATION]");
                textLabel[1] = new JLabel("  Step1: Select the panels that you want to mension");
                textLabel[2] = new JLabel("  Step2: Display STAR(s) by pushing Add '!' Button and arrange on a result where you noticed");
				textLabel[3] = new JLabel("  Step3: Write concrete <RESULT> about the arranged STAR");
				textLabel[4] = new JLabel("  Step4: Write <INTERPRETATION> as the meaning of the <RESULT>");
				textLabel[5] = new JLabel("  Step5: Select [IMPORTANCE] of the <INTERPRETATION>");
				textLabel[6] = new JLabel("  Step6: 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++)
			{
				textLabel[i].setBackground(new Color(0xD3,0xED,0xFB));
				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[0].setForeground(new Color(0x00,0x64,0x00));
            textLabel[1].setBackground(Color.pink);
            textLabel[1].setOpaque(true);
			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, 14));			
			
			for(int i=0;i<5;i++)
				jRadioButton[i] = new JRadioButton(""+(5-i));
			jRadioButton[4].setSelected(true);
			group = new ButtonGroup();
			
			//panel
            for(int i=0;i<control.panelNumber;i++)
            {

                panelSelectRadioButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
                panelSelectRadioButton[i].setBackground(Color.pink);
                panelSelectRadioButton[i].setOpaque(true);
                add(panelSelectRadioButton[i]);
            }
            if(control.panelNumber == 1)
                panelSelectRadioButton[0].setSelected(true);

			//star
			star.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
			star.setForeground(Color.red);
			star.setBackground(new Color(0xD3,0xED,0xFB));
			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));
				jRadioButton[i].setBackground(Color.yellow);
				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.setBackground(new Color(0xD3,0xED,0xFB));
			input.addActionListener(this);
			
			quit.setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			quit.setBackground(new Color(0xD3,0xED,0xFB));			
			quit.addActionListener(this);
			
			buttonPanel = new JPanel();
			buttonPanel.setOpaque(false);
			buttonPanel.setLayout(new GridLayout(1,2));
			
			buttonPanel.add(input);
			buttonPanel.add(quit);			
			
			add(buttonPanel);
			
            for(int i=0;i<9;i++)
                add(textLabel[i]);

            if(firstActivation)
            {
                control.navigationControl.setSoundNumber(702, parentFrame);
                firstActivation = false;
            }

            
        }
		
		//Action listener
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == star)
			{
				if(starNumber < maxStarNumber)
				{
					popForStar[starNumber] = new PopUpForStar(posX,posY,50,50,image,true,control,starNumber);
					starNumber++;
                    
                    StoryControl.nextSerif(5001);
				}				
			}
			
			if(e.getSource() == input)
			{

                
                String result = textField[0].getText();
				String meaning = textField[1].getText();
                
                if(result.equals("") || meaning.equals(""))
                {
                    control.navigationControl.setSoundNumber(730);
                    return;
                }
                
                if(control.interpretationNumber < control.maxInterpretationNumber)
                {
                    int selectedPanelNumber = -1;
                    int selectedPanelCount = 0;
                    boolean noticedPanelNumber[] = new boolean[control.panelNumber];
                    for(int i=0;i<control.panelNumber;i++)
                        if(panelSelectRadioButton[i].isSelected())
                        {
                            selectedPanelNumber = i;
                            selectedPanelCount++;
                            noticedPanelNumber[i] = true;
                        }
                        else
                            noticedPanelNumber[i] = false;
                    
                    if(selectedPanelCount == 0)
                        return;
                    
                    
                    
                
                    ACTIONLOG.writeActionLog("INTERPRETATION*KNOWLEDGE*INPUT*"+(control.interpretationNumber+1)+"*"+control.panelNumber);
                    

                    
                    String sourceFilename = new File(control.text.sourceFilename).getName();
                    
                    ACTIONLOG.writeActionLog("INTERPRETATION*KNOWLEDGE*SOURCE*"+sourceFilename);
                    
                    control.getPanelInformation();
                    
                    for(int i=0;i<control.panelNumber;i++)
                    {
                        ACTIONLOG.writeActionLog("INTERPRETATION*KNOWLEDGE*USED*"+control.miningIDs[i]+":"+control.visualizationIDs[i]+":"+control.textIDs[i]+"*"+noticedPanelNumber[i]);
                    }
                    
                    
                    ACTIONLOG.writeTextInputLog("INTERPRETATION", result);
                    
                    
                    ACTIONLOG.writeTextInputLog("INTERPRETATION", meaning);
                    
                    System.out.println("X="+textField[0].getX()+",Y="+textField[0].getY());
                    System.out.println("width="+textField[0].getWidth()+",height="+textField[0].getHeight());
                    System.out.println("X="+textField[1].getX()+",Y="+textField[1].getY());
                    System.out.println("width="+textField[1].getWidth()+",height="+textField[1].getHeight());
                    
                    
                    

                    
                    
                    
                    
//                    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;
                        }
                    
                    ACTIONLOG.writeActionLog("INTERPRETATION*KNOWLEDGE*IMPORTANCE*"+importanceRate);
//                    ACTIONLOG.writeButtonLog("INTERPRETATION", ""+importanceRate, (Component)e.getSource());
				

					///EXP ///
					control.ex1.addExpByinterpretation();
					if(control.expPanel != null) 
						control.expPanel.repaint();
                    
                    //LOG//
//                    System.out.println("miningID="+miningIDs[panelID]+",visualizationID="+visualizationIDs[panelID]);
                    
//                    control.writeActionLog("SELECT", "RESULT-INTERPRETATION("+miningIDs[panelID]+","+visualizationIDs[panelID]+")");
//control.panel[panelID].mining[control.panel[panelID].selected_module].imageCapture("test.png");
					
					System.out.println("Create situation"+(control.interpretationNumber+1)+".png");
                    
                    String imageFileDir = control.absolutePath + "interpretation" + File.separator;
                    String imageFileName = "situation"+(control.interpretationNumber+1)+".png";
                    
                    if(selectedPanelCount == 1)
                        control.panel[selectedPanelNumber].mining[control.panel[selectedPanelNumber].selected_module].imageCapture(imageFileDir + imageFileName);//must before add Interpretation
                    else
                        control.imageCaptureFromInterpretation(imageFileDir + imageFileName);
                    
                    EXECUTE.largeFileCopy(imageFileDir + imageFileName, ACTIONLOG.logDirectory + imageFileName);
					
                    parentFrame.toFront();
                    
                    //interpretationNumber++;
					control.addInterpretation(sourceFilename, result, meaning, importanceRate, miningIDs, visualizationIDs, textIDs, sizeX, noticedPanelNumber);

					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("");
                    
                    /////////SCENE DATA REVISION
                    PopUpForKnowledgeCreation sceneDataRevision = new PopUpForKnowledgeCreation(control);
                    sceneDataRevision = null;
                    
                    if(control.interpretationNumber == control.maxInterpretationNumber)
                    {
                        control.autoSet = true;
                        control.bt79.setSelected(false);
                        control.autoSet = false;
                        
                        control.bt79.setEnabled(false);
                        control.closePopForInterpretation();
                    }
                    
                    //NAVIGATION
                    if(control.interpretationNumber > goalNumber)
                        control.navigationControl.setSoundNumber(750+goalNumber);
                    else if(control.interpretationNumber == goalNumber)
                        control.navigationControl.setSoundNumber(740+goalNumber);
                    else if(control.interpretationNumber == 1)
                        control.navigationControl.setSoundNumber(730+goalNumber);
                    else
                    {
                        control.navigationControl.setSoundNumber(control.interpretationNumber+719-goalNumber);
                        /*
                        if(goalNumber > 5)
                            control.navigationControl.setSoundNumber(control.interpretationNumber+709);     // 2,3,4,5,6,7,8,9 -> 711 - 718 ,goalNumber=10
                        else
                            control.navigationControl.setSoundNumber(control.interpretationNumber+714);     // 2,3,4 -> 716 - 718, goalNumber = 5
                        */

                    }
                    
                    boolean shrink = false;
                    for(int i=0;i<control.panelNumber;i++)
                        if(noticedPanelNumber[i] == true)
                            if(textIDs[i] != 0)
                                shrink = true;
                    
                    if(shrink == false)
                        control.navigationControl.setSoundNumberAddition(810);
                    
				}
                

                
                StoryControl.nextSerif(5002);
				
				//FOR MISSION
				control.interpretationMission();
			}
			
			if(e.getSource() == quit)
			{
//                select.interpretation.setSelected(false);
                control.autoSet = true;
                control.bt79.setSelected(false);
                control.autoSet = false;
                StoryControl.nextSerif(5003);
//                select.closePopForInterpretation();
                control.closePopForInterpretation();
			}			
		}		
	}		
}
