

//
// Core Program for TETDM 
// PopUpForInterpretationList.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.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();
	
    int threshold;

	String windowName = "INTERPRETLIST";	
	
	Control control;
	
	int dataNumber = 1;
		
	PopUpForInterpretationList(int X, int Y, int width, int height, Control control, String windowTitle)
	{
		super(windowTitle);
		
		pane.setLayout(new BorderLayout());
		
		this.control = control;
 
        WINDOWACTIONLOG windowActionLog = new WINDOWACTIONLOG(this, windowName);
        this.addComponentListener(windowActionLog.componentListener);                    //FOR LOG
        this.addWindowListener(new MyWindowListener());
        
        
        threshold = 1;
		
//		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);
		

		setBounds(X-100, Y, 1080, height);
		
		
	}
    
    void changeThreshold()
    {
        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()
        
    }
    
    void changeList()
    {
        pane.remove(scroll);
        
        interpretationListPanel = new InterpretationListPanel(this, control, 1);
        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
{
//    int importanceThreshold = 1;
    
    JButton listButton[];
    JLabel folderSelect;
    JTextField folderName;
    
    
    JToggleButton importanceButton[];
	ButtonGroup group;
    
	String inJapanese[];
    
    PopUpForInterpretationList parent;
    
    SelectInterpretation(PopUpForInterpretationList parent)
    {
        this.parent = parent;
        
        setLayout(new GridLayout(2,5));
        
        String buttonNames[] = {"Move List","Load List","Delete List","Move-To or Load-From :"};
		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(parent.control.absolutePath+"source"+File.separator+"japaneseText"+File.separator+"JapaneseList.txt"));
        
        listButton = new JButton[3];
        for(int i=0;i<3;i++)
        {
            if(parent.control.sp1.inJapanese)
                listButton[i] = new JButton(inJapanese[6+i]);
            else
                listButton[i] = new JButton(buttonNames[i]);
            listButton[i].setOpaque(true);
            listButton[i].setBackground(Color.green);
            listButton[i].addActionListener(this);
            add(listButton[i]);
        }
        
        if(parent.control.sp1.inJapanese)
            folderSelect = new JLabel(inJapanese[9]);
        else
            folderSelect = new JLabel(buttonNames[3]);
        folderSelect.setFont(new Font(parent.control.sp1.fontName, Font.BOLD, 12));
        folderSelect.setOpaque(true);
        folderSelect.setBackground(Color.green);
        add(folderSelect);
        
        folderName = new JTextField();
        folderName.addActionListener(this);
        add(folderName);
        
        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)
	{
        int option;
        
		for(int i=0;i<5;i++)
			if(e.getSource() == importanceButton[i])
			{
                parent.threshold = i+1;
                System.out.println("THRESHOLD: "+parent.threshold);
                parent.changeThreshold();
                return;
			}
        
        String fromFolder, toFolder;
        
        if(e.getSource() == listButton[0])  //COPY
        {
            fromFolder = parent.control.absolutePath+"interpretation";
            toFolder = folderName.getText();
            
            if(toFolder.equals(""))
                return;
            else
                toFolder = parent.control.absolutePath+toFolder;
            
            if(parent.control.sp1.inJapanese)
                option = JOptionPane.showConfirmDialog(parent.control, inJapanese[16]+toFolder+inJapanese[17], inJapanese[15], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            else
                option = JOptionPane.showConfirmDialog(parent.control, "COPY", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            
            parent.toFront();
            
            if(option != JOptionPane.YES_OPTION)
                return;
            
            System.out.println("From: "+fromFolder+" To: "+toFolder);
            
            if(FILEIO.copyFolder(fromFolder, toFolder))
            {
                if(parent.control.sp1.inJapanese)
                    folderName.setText(inJapanese[10]);
                else
                    folderName.setText("List Copied");
            }
            else
            {
                if(parent.control.sp1.inJapanese)
                    folderName.setText(inJapanese[12]);
                else
                    folderName.setText("List NOT Copied");
            }
            
            return;
        }
        if(e.getSource() == listButton[1])  //LOAD
        {
            fromFolder = folderName.getText();
            toFolder = parent.control.absolutePath+"interpretation";
            
            if(fromFolder.equals(""))
                return;
            else
                fromFolder = parent.control.absolutePath+fromFolder;
            
            if(parent.control.sp1.inJapanese)
                option = JOptionPane.showConfirmDialog(parent.control, inJapanese[18]+fromFolder+inJapanese[19], inJapanese[15], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            else
                option = JOptionPane.showConfirmDialog(parent.control, "LOAD", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            
            parent.toFront();
            
            if(option != JOptionPane.YES_OPTION)
                return;
            
            FILEIO.deleteFolder(toFolder, false);
            
            if(FILEIO.copyFolder(fromFolder, toFolder))
            {
                if(parent.control.sp1.inJapanese)
                    folderName.setText(inJapanese[11]);
                else
                    folderName.setText("List Loaded");
            }
            else
            {
                if(parent.control.sp1.inJapanese)
                    folderName.setText(inJapanese[13]);
                else
                    folderName.setText("List NOT Loaded");
                
                FILEIO.createFolder(toFolder);
            }
            
            parent.control.readInterpretation();
            parent.changeThreshold();
            
            return;
        }
        if(e.getSource() == listButton[2])  // DELETE
        {
            if(parent.control.sp1.inJapanese)
                option = JOptionPane.showConfirmDialog(parent.control, inJapanese[20], inJapanese[15], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            else
                option = JOptionPane.showConfirmDialog(parent.control, "DELETE", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            
            parent.toFront();
            
            if(option != JOptionPane.YES_OPTION)
                return;
            
            toFolder = parent.control.absolutePath+"interpretation";
            
            FILEIO.deleteFolder(toFolder, true);
            
            folderName.setText("");
            
            parent.control.readInterpretation();
            parent.changeThreshold();
            
            return;
        }
	}

}

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

	//single
	
	int singlePanelNumber[];
	int singleMiningIDs[][];
	int singleVisualizationIDs[][];
	int singleTextIDs[][];
	int singleSizeX[][];
    
    int noticedPanelList[][];
    int panelCount[];

    
	//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 toolNameList, 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);
                this.panelCombination = new JLabel(toolNameList);
				
				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(toolNameList);
				
				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+"japaneseText"+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].setBackground(new Color(0xE0,0xFF,0xFF));
			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].setBackground(new Color(0xE0,0xFF,0xFF));
			panelRecallButton[i].addActionListener(this);

			deleteButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
			deleteButton[i].setBackground(new Color(0xE0,0xFF,0xFF));
			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][];
		
        panelCount = new int[createdInterpretationNumber];
        noticedPanelList = new int[createdInterpretationNumber][];
        
		for(int i=0;i<createdInterpretationNumber;i++)
		{
            panelCount[i] = 0;
            noticedPanelList[i] = new int[control.interpretation[i].allPanelNumber];
            for(int j=0;j<control.interpretation[i].allPanelNumber;j++)
                if(control.interpretation[i].noticedPanelNumber[j] == true)
                    noticedPanelList[i][panelCount[i]++] = j;
            
			singlePanelNumber[i] = panelCount[i];
				
			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;
//            int noticed = 0;
            
			for(int j=0;j<singlePanelNumber[i];j++)
			{				
				singleMiningIDs[i][j] = control.interpretation[i].allMiningIDs[noticedPanelList[i][j]];
				singleVisualizationIDs[i][j] = control.interpretation[i].allVisualizationIDs[noticedPanelList[i][j]];
				singleTextIDs[i][j] = control.interpretation[i].allTextIDs[noticedPanelList[i][j]];
                singleSizeX[i][j] = control.interpretation[i].allSizeX[noticedPanelList[i][j]];
			}
		}
	}
    
	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;
            
//            int noticed = 0;
            
            String miningName, visualizationName;
            //noticedPanelList
            String toolName = "<html><body>";
            
            if(control.sp1.inJapanese)
            {
                for(int k=0;k<panelCount[i];k++)
                {
                    miningName = control.moduleData.module_names[control.moduleData.findModule(control.interpretation[i].allMiningIDs[noticedPanelList[i][k]])];
                    visualizationName = control.moduleData.visu_module_names[control.moduleData.findVisualizationModule(control.interpretation[i].allVisualizationIDs[noticedPanelList[i][k]])];
                    
                    toolName += (inJapanese[3]+noticedPanelList[i][k]+inJapanese[14]+miningName+" + "+visualizationName);
                    if(k != panelCount[i]-1)
                        toolName += "<br>";
                }
            }
            else
            {
                for(int k=0;k<panelCount[i];k++)
                {
                    miningName = control.moduleData.miningModuleNamesInJapanese[control.moduleData.findModule(control.interpretation[i].allMiningIDs[noticedPanelList[i][k]])];
                    visualizationName = control.moduleData.visualizationModuleNamesInJapanese[control.moduleData.findVisualizationModule(control.interpretation[i].allVisualizationIDs[noticedPanelList[i][k]])];
                    
                    toolName += ("  PANEL"+noticedPanelList[i][k]+": "+miningName+" + "+visualizationName);
                    if(k != panelCount[i]-1)
                        toolName += "<br>";
                }
            }
            toolName += "</body></html>";
            
			resultPanel[i] = new InformationPanel(control.interpretation[i].sourceFilename, toolName, 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])
			{
                ACTIONLOG.writeActionLog("INTERPRETATION*KNOWLEDGE*DELETE*"+(i+1));
                //delete //reconstruction //interpretationNumber--;
				control.deleteInterpretation(i);
                
                control.readInterpretation();
                popFrame.changeThreshold();
//                control.closePopForInterpretationList();
                
                
                //DELETE SCENEDATA
//                FILEIO.deleteFile(new File(control.absolutePath+"interpretation"+File.separator+"sceneData"));
                
                PopUpForKnowledgeCreation sceneDataRevision = new PopUpForKnowledgeCreation(control,i);
                sceneDataRevision = null;
			}
							
		if(e.getSource() == okButton)
		{
            control.closePopForInterpretationList();
		}
	}
}
