

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

package source;

import java.io.*;

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

public class PopUpForInterpretationList extends JFrame
{
    SelectInterpretation selectInterpretation;
	JScrollPane scroll;
	InterpretationListPanel interpretationListPanel;
	Container pane = getContentPane();
	
	Control control;
	
	int dataNumber = 1;
		
	PopUpForInterpretationList(int X, int Y, int width, int height, Control control)
	{
		super("INTERPRETATION LIST");
		pane.setLayout(new BorderLayout());
		
		this.control = control;
        
 
		
//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		setAlwaysOnTop(true);
		pane.setBackground(Color.blue);
//		setUndecorated(true);
		
        selectInterpretation = new SelectInterpretation(this);
        selectInterpretation.setBackground(Color.blue);
        pane.add(selectInterpretation, BorderLayout.NORTH);
        
		interpretationListPanel = new InterpretationListPanel(this, control, 1);
		interpretationListPanel.setBackground(Color.blue);
		scroll = new JScrollPane(interpretationListPanel);
		scroll.setBackground(Color.blue);
		
		pane.add(scroll);
		setVisible(true);
		
		this.addWindowListener(new MyWindowListener());

		setBounds(X-100, Y, 1080, height);
	}
    
    void changeThreshold(int threshold)
    {
        pane.remove(scroll);

		interpretationListPanel = new InterpretationListPanel(this, control, threshold);
		interpretationListPanel.setBackground(Color.blue);
		scroll = new JScrollPane(interpretationListPanel);
		scroll.setBackground(Color.blue);
		
		pane.add(scroll);
        pane.validate();	//java7 revalidate()
        
    }
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e)// x button
		{
			control.bt77.setSelected(false);			
		}
		
		public void windowClosed(WindowEvent e)
		{
			control.bt77.setSelected(false);			
		}		
	}
}

class SelectInterpretation extends JPanel implements ActionListener
{
    JToggleButton importanceButton[];
	ButtonGroup group;
    
    PopUpForInterpretationList parent;
    
    SelectInterpretation(PopUpForInterpretationList parent)
    {
        this.parent = parent;
        
        setLayout(new GridLayout(1,5));
        
        importanceButton = new JToggleButton[5];
        group = new ButtonGroup();
        
        String label[] = {"ALL","2+","3+","4+","5"};
        
        for(int i=0;i<5;i++)
        {
            importanceButton[i] = new JToggleButton(label[i]);
            importanceButton[i].setOpaque(true);
			importanceButton[i].setBackground(Color.yellow);
            importanceButton[i].addActionListener(this);
            add(importanceButton[i]);
            group.add(importanceButton[i]);
        }
        importanceButton[0].setSelected(true);
    }
    
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0;i<5;i++)
			if(e.getSource() == importanceButton[i])
			{
                int importanceThreshold = i+1;
                System.out.println("THRESHOLD: "+importanceThreshold);
                parent.changeThreshold(importanceThreshold);
                return;
			}
	}

}

class InterpretationListPanel extends JPanel implements ActionListener
{
	PopUpForInterpretationList popFrame;
	Control control;
	
	JButton okButton;
	
    //Panel
    
    JPanel leftButtonPanel;
    JPanel rightInterpretationPanel;

	//single
	
	int singlePanelNumber[];
	int singleMiningIDs[][];
	int singleVisualizationIDs[][];
	int singleTextIDs[][];
	int singleSizeX[][];
    
	//all
	
	int allPanelNumber[];
	int allMiningIDs[][];
	int allVisualizationIDs[][];
	int allTextIDs[][];
	int allSizeX[][];
	
	JButton createdInterpretationButton[];	
	int createdInterpretationNumber;

	JButton panelRecallButton[];
	JButton deleteButton[];	

	InformationPanel resultPanel[];
	
	String inJapanese[];
	
	class InformationPanel extends JPanel
	{
        JLabel textName;
        JLabel panelCombination;
		JLabel result;
		JLabel meaning;
		
		InformationPanel(String textName, String panelCombination, String result, String meaning, int importance)
		{
			setLayout(new GridLayout(4,1));
			
            if(control.sp1.inJapanese)
            {
                this.textName = new JLabel(inJapanese[2]+textName);
                this.panelCombination = new JLabel(inJapanese[3]+panelCombination);
				
				String displayWords = inJapanese[4]+result;
				if(displayWords.length() > 43)
					displayWords = displayWords.substring(0,43);
                this.result = new JLabel(displayWords);
				
				displayWords = inJapanese[5]+"("+importance+") "+meaning;
				if(displayWords.length() > 41)
					displayWords = displayWords.substring(0,41);
                this.meaning = new JLabel(displayWords);
            }
            else
            {
                this.textName = new JLabel("  TEXT: "+textName);
                this.panelCombination = new JLabel("  PANEL: "+panelCombination);
				
				String displayWords = "  RESULT: "+result;
				if(displayWords.length() > 86)
					displayWords = displayWords.substring(0,86);
                this.result = new JLabel(displayWords);
				
				displayWords = "  INTERPRETATION:("+importance+") "+meaning;
				if(displayWords.length() > 82)
					displayWords = displayWords.substring(0,82);
                this.meaning = new JLabel(displayWords);
            }
            
            this.textName.setFont(new Font(control.sp1.fontName, Font.BOLD, 12));
            this.panelCombination.setFont(new Font(control.sp1.fontName, Font.BOLD, 12));
            this.result.setFont(new Font(control.sp1.fontName, Font.BOLD, 12));
            this.meaning.setFont(new Font(control.sp1.fontName, Font.BOLD, 12));
            
            this.textName.setBackground(Color.pink);
            this.panelCombination.setBackground(Color.pink);
            this.result.setBackground(Color.green);
            this.meaning.setBackground(Color.yellow);
            
            this.textName.setOpaque(true);
            this.panelCombination.setOpaque(true);
            this.result.setOpaque(true);
            this.meaning.setOpaque(true);
            
            add(this.textName);
			add(this.panelCombination);
			add(this.result);
			add(this.meaning);
		}
		
	}
	
	InterpretationListPanel(PopUpForInterpretationList popFrame, Control control, int threshold)
	{
		this.popFrame = popFrame;
		this.control = control;
		
		setLayout(new GridLayout(1,2));
        
		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath+"source"+File.separator+"JapaneseList.txt"));
        
		singleDataInitialization();
		allDataInitialization();
        
        int interpretationCount = 0;
        for(int i=0;i<createdInterpretationNumber;i++)
             if(control.interpretation[i].importance >= threshold)
                 interpretationCount++;

        leftButtonPanel = new JPanel(new GridLayout(interpretationCount,2));
        rightInterpretationPanel = new JPanel(new GridLayout(interpretationCount,1));
        
        leftButtonPanel.setBackground(new Color(0xE0,0xFF,0xFF));
        rightInterpretationPanel.setBackground(new Color(0xE0,0xFF,0xFF));
        
        add(leftButtonPanel);
        add(rightInterpretationPanel);
        
		popFrame.dataNumber = createdInterpretationNumber;		
		

		createdInterpretationButton = new JButton[createdInterpretationNumber];
        panelRecallButton = new JButton[createdInterpretationNumber];
		deleteButton = new JButton[createdInterpretationNumber];



		

		for(int i=0;i<createdInterpretationNumber;i++)
		{
			createdInterpretationButton[i] = new JButton(control.interpretation[i].smallIcon);			
			createdInterpretationButton[i].addActionListener(this);

			if(control.sp1.inJapanese)
            {
				panelRecallButton[i] = new JButton(inJapanese[0]);
				deleteButton[i] = new JButton(inJapanese[1]);
            }
			else
            {
				panelRecallButton[i] = new JButton("RECALL the situation");
				deleteButton[i] = new JButton("DELETE the left one");
            }
            
			panelRecallButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			panelRecallButton[i].addActionListener(this);

			deleteButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));			
			deleteButton[i].addActionListener(this);
            
            if(control.interpretation[i].importance >= threshold)
            {
                leftButtonPanel.add(createdInterpretationButton[i]);
                leftButtonPanel.add(panelRecallButton[i]);
                leftButtonPanel.add(deleteButton[i]);
                rightInterpretationPanel.add(resultPanel[i]);
            }
		}
		

	}

	public void singleDataInitialization()
	{
		createdInterpretationNumber = control.interpretationNumber;

            System.out.println("interpretationNumber="+createdInterpretationNumber);
        
		singlePanelNumber = new int[createdInterpretationNumber];
		singleMiningIDs = new int[createdInterpretationNumber][];
		singleVisualizationIDs = new int[createdInterpretationNumber][];
		singleTextIDs = new int[createdInterpretationNumber][];
		singleSizeX = new int[createdInterpretationNumber][];
		
		for(int i=0;i<createdInterpretationNumber;i++)
		{
			singlePanelNumber[i] = 1;
				
			singleMiningIDs[i] = new int[singlePanelNumber[i]];
			singleVisualizationIDs[i] = new int[singlePanelNumber[i]];
			singleTextIDs[i] = new int[singlePanelNumber[i]];
			singleSizeX[i] = new int[singlePanelNumber[i]];			
				
            int noticed = control.interpretation[i].noticedPanelNumber;
            
			for(int j=0;j<singlePanelNumber[i];j++)
			{				
				singleMiningIDs[i][j] = control.interpretation[i].allMiningIDs[noticed];
				singleVisualizationIDs[i][j] = control.interpretation[i].allVisualizationIDs[noticed];
				singleTextIDs[i][j] = control.interpretation[i].allTextIDs[noticed];
			}
						
			for(int j=0;j<singlePanelNumber[i];j++)
				singleSizeX[i][j] = 1;				
		}
	}
    
	public void allDataInitialization()
	{
//		createdInterpretationNumber = control.interpretationNumber;
        
		allPanelNumber = new int[createdInterpretationNumber];
		allMiningIDs = new int[createdInterpretationNumber][];
		allVisualizationIDs = new int[createdInterpretationNumber][];
		allTextIDs = new int[createdInterpretationNumber][];
		allSizeX = new int[createdInterpretationNumber][];
		
		resultPanel = new InformationPanel[createdInterpretationNumber];
		for(int i=0;i<createdInterpretationNumber;i++)
        {
            int noticed = control.interpretation[i].noticedPanelNumber;
            String miningName, visualizationName;
            
            
            if(control.sp1.inJapanese)
            {
                miningName = control.moduleData.module_names[control.moduleData.findModule(control.interpretation[i].allMiningIDs[noticed])];
                visualizationName = control.moduleData.visu_module_names[control.moduleData.findVisualizationModule(control.interpretation[i].allVisualizationIDs[noticed])];
            }
            else
            {
                miningName = control.moduleData.miningModuleNamesInJapanese[control.moduleData.findModule(control.interpretation[i].allMiningIDs[noticed])];
                visualizationName = control.moduleData.visualizationModuleNamesInJapanese[control.moduleData.findVisualizationModule(control.interpretation[i].allVisualizationIDs[noticed])];
            }
            
			resultPanel[i] = new InformationPanel(control.interpretation[i].sourceFilename, miningName+" + "+visualizationName, control.interpretation[i].result, control.interpretation[i].meaning,
                                                  control.interpretation[i].importance);
        }
		
		for(int i=0;i<createdInterpretationNumber;i++)
		{
			allPanelNumber[i] = control.interpretation[i].allPanelNumber;
            
			allMiningIDs[i] = new int[allPanelNumber[i]];
			allVisualizationIDs[i] = new int[allPanelNumber[i]];
			allTextIDs[i] = new int[allPanelNumber[i]];
			allSizeX[i] = new int[allPanelNumber[i]];
            
			for(int j=0;j<allPanelNumber[i];j++)
			{
				allMiningIDs[i][j] = control.interpretation[i].allMiningIDs[j];
				allVisualizationIDs[i][j] = control.interpretation[i].allVisualizationIDs[j];
				allTextIDs[i][j] = control.interpretation[i].allTextIDs[j];
                allSizeX[i][j] = control.interpretation[i].allSizeX[j];
			}
		}
	}
    
    
/*
	public String createPanelInformationWithoutOne(int n)
	{
		String saveSettingTexts = "";
		
		for(int k=0;k<createdInterpretationNumber;k++)
		{
			if(k == n)
				continue;
			
			try
			{
				StringWriter sw1 = new StringWriter();
				BufferedWriter bw1 = new BufferedWriter(sw1);
				
				bw1.write("panelNumber="+singlePanelNumber[k]);
				bw1.newLine();
				
				for(int i=0;i<singlePanelNumber[k];i++)
				{
					bw1.write(singleMiningIDs[k][i]+" "+singleVisualizationIDs[k][i]+" "+singleTextIDs[k][i]);
					bw1.newLine();
				}
				
				bw1.write("#Panel Balance");
				bw1.newLine();			
				for(int i=0;i<singlePanelNumber[k];i++)
					bw1.write(singleSizeX[k][i]+" ");
				bw1.newLine();
				
				bw1.flush();
				sw1.flush();
				saveSettingTexts += sw1.toString();
				bw1.close();
				sw1.close();
			}
			catch(Exception e){
				System.out.println("Text Writing ERROR in createPanelInformationWithoutOne");
			}
		}
		
		return saveSettingTexts;		
	}	
*/

	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0;i<createdInterpretationNumber;i++)
			if(e.getSource() == createdInterpretationButton[i])
			{
				control.panelSet(singleMiningIDs[i], singleVisualizationIDs[i], singleTextIDs[i], singleSizeX[i]);
			}

		for(int i=0;i<createdInterpretationNumber;i++)
			if(e.getSource() == panelRecallButton[i])
			{
				control.panelSet(allMiningIDs[i], allVisualizationIDs[i], allTextIDs[i], allSizeX[i]);

			}
        
		for(int i=0;i<createdInterpretationNumber;i++)
			if(e.getSource() == deleteButton[i])
			{
				//delete //reconstruction
				control.deleteInterpretation(i);
				popFrame.dispose();
			}			
							
		if(e.getSource() == okButton)
		{
			popFrame.dispose();
		}		
	}
}