

//
// 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 PopUpForSelectionAssist extends JFrame
{
	JScrollPane scroll;
	SelectionAssistPanel selectionAssistPanel;
	Container pane = getContentPane();
	
	Control control;
	
	int dataNumber = 1;
		
	PopUpForSelectionAssist(int X, int Y, int width, int height, Control control)
	{
		super("COMBINATION SELECTION");
		pane.setLayout(new BorderLayout());
		
		this.control = control;
		

//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		setAlwaysOnTop(true);
		pane.setBackground(Color.blue);
//		setUndecorated(true);
		
		selectionAssistPanel = new SelectionAssistPanel(this, control);
		selectionAssistPanel.setBackground(Color.blue);
		scroll = new JScrollPane(selectionAssistPanel);
		scroll.setBackground(Color.blue);
		
		pane.add(scroll);
		setVisible(true);
		
		this.addWindowListener(new MyWindowListener());
		
		int sizeY = ((int)((double)((double)height/width)*200+10)*dataNumber + 50);
		if(Y + sizeY > height)
			sizeY = height-Y;
		
		setBounds(X, Y, 460, sizeY);
	}
	
	class MyWindowListener extends WindowAdapter {
		
		public void windowClosing(WindowEvent e)// x button
		{
			control.writeActionLog("CLOSE-EASY-COMBINATION-WINDOW");
			control.bt72.setSelected(false);			
		}
		
		public void windowClosed(WindowEvent e)
		{
			control.writeActionLog("CLOSE-EASY-COMBINATION-WINDOW");			
			control.bt72.setSelected(false);			
		}		
	}	
	
	
}

class SelectionAssistPanel extends JPanel implements ActionListener
{
	PopUpForSelectionAssist popFrame;
	Control control;
	
	JButton okButton;
	
	ImageIcon icon, smallIcon;
	Image smallImg;	
	
	
	//ORIGINAL
	
	int originalPanelNumber[];
	int originalMiningIDs[][];
	int originalVisualizationIDs[][];
	int originalTextIDs[][];
	int originalSizeX[][];
	
	JButton originalSelectionButton[];	
	int originalSelectionNumber;		
	JButton originalDeleteButton[];
	JLabel originalLabel[];
	
	//manually saved
	
	int manPanelNumber[];
	int manMiningIDs[][];
	int manVisualizationIDs[][];
	int manTextIDs[][];
	int manSizeX[][];
	
	JButton manSelectionButton[];	
	int manSelectionNumber;		
	JButton manDeleteButton[];	
	
	//automatically saved
	
	int panelNumber[];
	int miningIDs[][];
	int visualizationIDs[][];
	int textIDs[][];
	int sizeX[][];
	
	JButton selectionButton[];
	int selectionNumber;	
	JLabel autoLabel[];
	
	String inJapanese[];
	

	
	
	SelectionAssistPanel(PopUpForSelectionAssist popFrame, Control control)
	{
		this.popFrame = popFrame;
		this.control = control;
		
		originalDataRead();
		manDataRead();		
		dataRead();
		
		control.manSaveCount = manSelectionNumber;
		
		setLayout(new GridLayout(originalSelectionNumber + selectionNumber + manSelectionNumber,2));
		popFrame.dataNumber = originalSelectionNumber + selectionNumber + manSelectionNumber;
		
		selectionButton = new JButton[selectionNumber];
		autoLabel = new JLabel[selectionNumber];
		
		manSelectionButton = new JButton[manSelectionNumber];
		manDeleteButton = new JButton[manSelectionNumber];
		
		originalSelectionButton = new JButton[originalSelectionNumber];
		originalDeleteButton = new JButton[originalSelectionNumber];
		originalLabel = new JLabel[originalSelectionNumber];

		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath+"source"+File.separator+"JapaneseAssist.txt"));
		
		
		//originally prepared panel combination		
		for(int i=0;i<originalSelectionNumber;i++)
		{
			icon = new ImageIcon(control.absolutePath+"capture"+File.separator+"originalCapture"+i+".png");
			smallImg = icon.getImage().getScaledInstance(200, -1, Image.SCALE_SMOOTH);
			smallIcon = new ImageIcon(smallImg);
			
			originalSelectionButton[i] = new JButton(smallIcon);
			originalSelectionButton[i].addActionListener(this);
			add(originalSelectionButton[i]);
			
			if(i == 0)
			{
				if(control.sp1.inJapanese)
					okButton = new JButton(inJapanese[0]);
				else
					okButton = new JButton("OK");					
				okButton.setFont(new Font(control.sp1.fontName, Font.BOLD, 14));
				okButton.addActionListener(this);		
				add(okButton);				
			}
			else
			{
				if(control.sp1.inJapanese)				
					originalLabel[i] = new JLabel(inJapanese[2+i]);
				else
					originalLabel[i] = new JLabel("-----");
				originalLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));				
				originalLabel[i].setForeground(Color.white);
				add(originalLabel[i]);
			}
		}		
		
		//manually saved panel combination
		for(int i=0;i<manSelectionNumber;i++)
		{
			icon = new ImageIcon(control.absolutePath+"capture"+File.separator+"ManCapture"+i+".png");
			smallImg = icon.getImage().getScaledInstance(200, -1, Image.SCALE_SMOOTH);
			smallIcon = new ImageIcon(smallImg);
			
			manSelectionButton[i] = new JButton(smallIcon);
			manSelectionButton[i].addActionListener(this);
			add(manSelectionButton[i]);
			

			if(control.sp1.inJapanese)			
				manDeleteButton[i] = new JButton(inJapanese[1]);
			else
				manDeleteButton[i] = new JButton("DELETE the left one");
			manDeleteButton[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));			
			manDeleteButton[i].addActionListener(this);
			add(manDeleteButton[i]);			
		}		
		
		//automatically saved panel combination		
		for(int i=0;i<selectionNumber;i++)
		{
			icon = new ImageIcon(control.absolutePath+"capture"+File.separator+"Capture"+i+".png");
			smallImg = icon.getImage().getScaledInstance(200, -1, Image.SCALE_SMOOTH);
			smallIcon = new ImageIcon(smallImg);

			selectionButton[i] = new JButton(smallIcon);
			selectionButton[i].addActionListener(this);
			add(selectionButton[i]);
			
			if(control.sp1.inJapanese)
				autoLabel[i] = new JLabel(inJapanese[2]);
			else			
				autoLabel[i] = new JLabel("AUTO SAVED");
			autoLabel[i].setFont(new Font(control.sp1.fontName, Font.BOLD, 14));			
			autoLabel[i].setForeground(Color.white);
			add(autoLabel[i]);			
		}
	}

	public void manDataRead()
	{
		String manPanelInformationText = FILEIO.TextFileAllReadCode(new File(control.absolutePath+"manPanelInformation"));
		
		manSelectionNumber = 0;
		String ss;
		
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(manPanelInformationText));
			
			while((ss = br1.readLine()) != null)
			{		
				manSelectionNumber++;
				
				String lineData[] = ss.split("=");
				for(int i=0;i<Integer.parseInt(lineData[1]);i++)
					ss = br1.readLine();
				
				ss = br1.readLine();//Comment
				ss = br1.readLine();//Balance
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR manPanelInformationText");
		}		
		
		manPanelNumber = new int[manSelectionNumber];
		manMiningIDs = new int[manSelectionNumber][];
		manVisualizationIDs = new int[manSelectionNumber][];
		manTextIDs = new int[manSelectionNumber][];
		manSizeX = new int[manSelectionNumber][];
		

		
		//manually saved data
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(manPanelInformationText));
			
			for(int i=0;i<manSelectionNumber;i++)
			{
				ss = br1.readLine();				
				String lineData[] = ss.split("=");
				manPanelNumber[i] = Integer.parseInt(lineData[1]);
				
				manMiningIDs[i] = new int[manPanelNumber[i]];
				manVisualizationIDs[i] = new int[manPanelNumber[i]];
				manTextIDs[i] = new int[manPanelNumber[i]];
				manSizeX[i] = new int[manPanelNumber[i]];				
				
				for(int j=0;j<manPanelNumber[i];j++)
				{				
					ss = br1.readLine();
					lineData = ss.split(" ");
					manMiningIDs[i][j] = Integer.parseInt(lineData[0]);
					manVisualizationIDs[i][j] = Integer.parseInt(lineData[1]);					
					manTextIDs[i][j] = Integer.parseInt(lineData[2]);
				}
				
				ss = br1.readLine(); // comment
				
				ss = br1.readLine();
				lineData = ss.split(" ");
				for(int j=0;j<manPanelNumber[i];j++)
					manSizeX[i][j] = Integer.parseInt(lineData[j]);				
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR manPanelInformationText2");
		}		
	}	

	public void originalDataRead()
	{
		String originalPanelInformationText = FILEIO.TextFileAllReadCode(new File(control.absolutePath+"originalPanelInformation"));
		
		originalSelectionNumber = 0;
		String ss;
		
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(originalPanelInformationText));
			
			while((ss = br1.readLine()) != null)
			{		
				originalSelectionNumber++;
				
				String lineData[] = ss.split("=");
				for(int i=0;i<Integer.parseInt(lineData[1]);i++)
					ss = br1.readLine();
				
				ss = br1.readLine();//Comment
				ss = br1.readLine();//Balance
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR originalPanelInformationText");
		}		
		
		originalPanelNumber = new int[originalSelectionNumber];
		originalMiningIDs = new int[originalSelectionNumber][];
		originalVisualizationIDs = new int[originalSelectionNumber][];
		originalTextIDs = new int[originalSelectionNumber][];
		originalSizeX = new int[originalSelectionNumber][];
		
		//automatically saved data
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(originalPanelInformationText));
			
			for(int i=0;i<originalSelectionNumber;i++)
			{
				ss = br1.readLine();				
				String lineData[] = ss.split("=");
				originalPanelNumber[i] = Integer.parseInt(lineData[1]);
				
				originalMiningIDs[i] = new int[originalPanelNumber[i]];
				originalVisualizationIDs[i] = new int[originalPanelNumber[i]];
				originalTextIDs[i] = new int[originalPanelNumber[i]];
				originalSizeX[i] = new int[originalPanelNumber[i]];				
				
				for(int j=0;j<originalPanelNumber[i];j++)
				{				
					ss = br1.readLine();
					lineData = ss.split(" ");
					originalMiningIDs[i][j] = Integer.parseInt(lineData[0]);
					originalVisualizationIDs[i][j] = Integer.parseInt(lineData[1]);					
					originalTextIDs[i][j] = Integer.parseInt(lineData[2]);
				}
				
				ss = br1.readLine(); // comment
				
				ss = br1.readLine();
				lineData = ss.split(" ");
				for(int j=0;j<originalPanelNumber[i];j++)
					originalSizeX[i][j] = Integer.parseInt(lineData[j]);				
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR originalPanelInformationText2");
		}		
	}	
	
	public void dataRead()
	{
		String panelInformationText = FILEIO.TextFileAllReadCode(new File(control.absolutePath+"panelInformation"));
		
		selectionNumber = 0;
		String ss;
		
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(panelInformationText));
			
			while((ss = br1.readLine()) != null)
			{		
				selectionNumber++;
				
				String lineData[] = ss.split("=");
				for(int i=0;i<Integer.parseInt(lineData[1]);i++)
					ss = br1.readLine();

				ss = br1.readLine();//Comment
				ss = br1.readLine();//Balance
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR panelInformationText");
		}		
		
		panelNumber = new int[selectionNumber];
		miningIDs = new int[selectionNumber][];
		visualizationIDs = new int[selectionNumber][];
		textIDs = new int[selectionNumber][];
		sizeX = new int[selectionNumber][];
		
		//automatically saved data
		try
		{
			BufferedReader br1 = new BufferedReader(new StringReader(panelInformationText));
			
			for(int i=0;i<selectionNumber;i++)
			{
				ss = br1.readLine();				
				String lineData[] = ss.split("=");
				panelNumber[i] = Integer.parseInt(lineData[1]);
				
				miningIDs[i] = new int[panelNumber[i]];
				visualizationIDs[i] = new int[panelNumber[i]];
				textIDs[i] = new int[panelNumber[i]];
				sizeX[i] = new int[panelNumber[i]];				
				
				for(int j=0;j<panelNumber[i];j++)
				{				
					ss = br1.readLine();
					lineData = ss.split(" ");
					miningIDs[i][j] = Integer.parseInt(lineData[0]);
					visualizationIDs[i][j] = Integer.parseInt(lineData[1]);					
					textIDs[i][j] = Integer.parseInt(lineData[2]);
				}

				ss = br1.readLine(); // comment
				
				ss = br1.readLine();
				lineData = ss.split(" ");
				for(int j=0;j<panelNumber[i];j++)
					sizeX[i][j] = Integer.parseInt(lineData[j]);				
			}
		}
		catch(Exception e){
			System.out.println("Text Reading ERROR panelInformationText2");
		}		
	}
	
	public String createPanelInformationWithoutOne(int n)
	{
		String saveSettingTexts = "";
		
		for(int k=0;k<manSelectionNumber;k++)
		{
			if(k == n)
				continue;
		
			try
			{
				StringWriter sw1 = new StringWriter();
				BufferedWriter bw1 = new BufferedWriter(sw1);
			
				bw1.write("panelNumber="+manPanelNumber[k]);
				bw1.newLine();
			
				for(int i=0;i<manPanelNumber[k];i++)
				{
					bw1.write(manMiningIDs[k][i]+" "+manVisualizationIDs[k][i]+" "+manTextIDs[k][i]);
					bw1.newLine();
				}
			
				bw1.write("#Panel Balance");
				bw1.newLine();			
				for(int i=0;i<manPanelNumber[k];i++)
					bw1.write(manSizeX[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;		
	}	
	
	//n: deleted data number
	public void manDataDelete(int n)
	{
		String saveSettingTexts = createPanelInformationWithoutOne(n);
			
		FILEIO.TextFileWriteCode(new File(control.absolutePath+"manPanelInformation"), saveSettingTexts, "SJIS");	// NEW FILE	
		
		File deleteFile,file,renameFile;
		boolean result;
		
		deleteFile = new File(control.absolutePath+"capture"+File.separator+"manCapture"+n+".png");
		result = deleteFile.delete();
		
		for(int i=n+1;i<manSelectionNumber;i++)
		{
			file = new File(control.absolutePath+"capture"+File.separator+"manCapture"+i+".png");
			renameFile = new File(control.absolutePath+"capture"+File.separator+"manCapture"+(i-1)+".png");
			
			result = file.renameTo(renameFile);
		}
		
		manSelectionNumber--;
		control.manSaveCount = manSelectionNumber;
	}
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		for(int i=0;i<selectionNumber;i++)
			if(e.getSource() == selectionButton[i])
			{
				control.panelSet(miningIDs[i], visualizationIDs[i], textIDs[i], sizeX[i]);
			}
		
		for(int i=0;i<manSelectionNumber;i++)
			if(e.getSource() == manSelectionButton[i])
			{
				control.panelSet(manMiningIDs[i], manVisualizationIDs[i], manTextIDs[i], manSizeX[i]);
			}
		
		for(int i=0;i<originalSelectionNumber;i++)
			if(e.getSource() == originalSelectionButton[i])
			{
				control.panelSet(originalMiningIDs[i], originalVisualizationIDs[i], originalTextIDs[i], originalSizeX[i]);
			}
		
		for(int i=0;i<manSelectionNumber;i++)
			if(e.getSource() == manDeleteButton[i])
			{
				//delete //reconstruction
				manDataDelete(i);
				popFrame.dispose();
			}			
							
		if(e.getSource() == okButton)
		{
			popFrame.dispose();
		}		
	}
}