

//
// Core Program for TETDM 
// SelectionAssistData.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.*;
import javax.swing.JComponent.*;



public class SelectionAssistData
{
	Control control;

	ImageIcon icon;
	Image smallImg;	
	
	
	//ORIGINAL
	
	int originalPanelNumber[];
	int originalMiningIDs[][];
	int originalVisualizationIDs[][];
	int originalTextIDs[][];
	int originalSizeX[][];
	ImageIcon originalSmallIcon[];
	
	//manually saved
	
	int manPanelNumber[];
	int manMiningIDs[][];
	int manVisualizationIDs[][];
	int manTextIDs[][];
	int manSizeX[][];
	ImageIcon manSmallIcon[];	
	
	//automatically saved
	
	int panelNumber[];
	int miningIDs[][];
	int visualizationIDs[][];
	int textIDs[][];
	int sizeX[][];
	ImageIcon smallIcon[];		
	
	String inJapanese[];
	
	int originalSelectionNumber;
	int originalThroughNumber;
	int manSelectionNumber;	
	int selectionNumber;
	int dataNumber;
	int totalDataNumber;
	

	int originalMode[] = {0,3,3,3,4,1,1,1};

	SelectionAssistData(Control control)
	{
		this.control = control;		

		originalDataRead();
		manDataRead();		
		dataRead();
		
		countNumber();

		inJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath+"source"+File.separator+"japaneseText"+File.separator+"JapaneseAssist.txt"));

		readOriginalIcon();
		readManIcon();
		readIcon();
	}
	
	public void countNumber()
	{
		totalDataNumber = originalSelectionNumber + manSelectionNumber + selectionNumber;
		
		originalThroughNumber = 0;
		for(int i=0;i<originalSelectionNumber;i++)
			if(originalMode[i] > control.sp1.mode)
				originalThroughNumber++;		
		
		dataNumber = 0;
		switch(control.sp1.mode)
		{
			case 0:
				dataNumber = originalSelectionNumber - originalThroughNumber;
				break;
			case 1:
				dataNumber = originalSelectionNumber - originalThroughNumber + manSelectionNumber;
				break;
			case 2:
			case 3:
				dataNumber = originalSelectionNumber - originalThroughNumber + manSelectionNumber + selectionNumber;
				break;				
		}
	}
	
	public void readOriginalIcon()
	{
		originalSmallIcon = new ImageIcon[originalSelectionNumber];			
		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);
			originalSmallIcon[i] = new ImageIcon(smallImg);
		}		
	}
	
	public void readManIcon()
	{
		//manually saved panel combination
		manSmallIcon = new ImageIcon[manSelectionNumber];
		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);
			manSmallIcon[i] = new ImageIcon(smallImg);		
		}			
	}
	
	public void readIcon()
	{
		//automatically saved panel combination	
		smallIcon = new ImageIcon[selectionNumber];			
		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[i] = new ImageIcon(smallImg);			
		}		
	}

	public void manDataRead()
	{
		String manPanelInformationText = FILEIO.TextFileAllReadCode(new File(control.absolutePath+"capture"+File.separator+"manPanelInformation"));
		
		manSelectionNumber = 0;
		String ss;
		
		//Count Number
		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][];


		
		//read 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+"capture"+File.separator+"originalPanelInformation"));
		
		originalSelectionNumber = 0;
		String ss;
		
		//Count Number
		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][];
	
		
		//read original 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+"capture"+File.separator+"panelInformation"));
		
		selectionNumber = 0;
		String ss;
		
		//Count Number
		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][];

		
		//read 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");
		}		
	}
	
	//for auto save data	
	public String createPanelInformationWithoutOne(int n)
	{
		String saveSettingTexts = "";
		
		for(int k=0;k<selectionNumber;k++)
		{
			if(k == n)
				continue;
			
			try
			{
				StringWriter sw1 = new StringWriter();
				BufferedWriter bw1 = new BufferedWriter(sw1);
				
				bw1.write("panelNumber="+panelNumber[k]);
				bw1.newLine();
				
				for(int i=0;i<panelNumber[k];i++)
				{
					bw1.write(miningIDs[k][i]+" "+visualizationIDs[k][i]+" "+textIDs[k][i]);
					bw1.newLine();
				}
				
				bw1.write("#Panel Balance");
				bw1.newLine();			
				for(int i=0;i<panelNumber[k];i++)
					bw1.write(sizeX[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 createPanelInformationWithoutOne1");
			}
		}
		
		return saveSettingTexts;		
	}	
	
	//for auto save data 	//n: deleted data number
	public void dataDelete(int n)
	{
		String saveSettingTexts = createPanelInformationWithoutOne(n);
		
		FILEIO.TextFileWriteCode(new File(control.absolutePath+"capture"+File.separator+"panelInformation"), saveSettingTexts, "SJIS");	// NEW FILE	
		
		File deleteFile,file,renameFile;
		boolean result;
		
		deleteFile = new File(control.absolutePath+"capture"+File.separator+"Capture"+n+".png");
		result = deleteFile.delete();
		
		for(int i=n+1;i<selectionNumber;i++)
		{
			file = new File(control.absolutePath+"capture"+File.separator+"Capture"+i+".png");
			renameFile = new File(control.absolutePath+"capture"+File.separator+"Capture"+(i-1)+".png");
			
			result = file.renameTo(renameFile);
		}
		
		selectionNumber--;
		countNumber();
	}	
	
	public String manCreatePanelInformationWithoutOne(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 createPanelInformationWithoutOne2");
			}
		}
		
		return saveSettingTexts;		
	}	
	
	//n: deleted data number
	public void manDataDelete(int n)
	{
		String saveSettingTexts = manCreatePanelInformationWithoutOne(n);
			
		FILEIO.TextFileWriteCode(new File(control.absolutePath+"capture"+File.separator+"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--;
		countNumber();
		manDataRead();
		readManIcon();
	}
}
