//
// Mining module for TETDM 
// ProfileCheck.java Version 0.39
// Copyright(C) 2011 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//

package module.MiningModules.ProfileCheck;

import source.*;
import source.TextData.*;

import java.io.*;
import java.util.*;

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


public class ProfileCheck extends MiningModule implements ActionListener
{
	String checkedText;

	String titles[];


	int buttonNumber;
	int mining[][], visual[][], size[][];
	ButtonGroup buttonGroup;		
	
	public ProfileCheck()
    {
		setModuleID(38);
		setCategoryID(9);
		setUseMode(2);
		pairingVisualizationID = new int[]{3};
//        defaultSetPanelModuleID = new int[]{38};
  //      defaultSetPanelVisualizationID = new int[]{3};
		setToolType(2);
	}

	void panelDataCreation(int m[], int v[], int s[], int number)
	{
		mining[number] = m;
		visual[number] = v;
		size[number] = s;
	}
	
	public void initializePanel()
	{
		buttonNumber = 3;
		
		operationPanel.setBackground(Color.GREEN);
		operationPanel.setLayout(new GridLayout(buttonNumber,1));
		
		toggleButton = new JToggleButton[buttonNumber];
		mining = new int[buttonNumber][];
		visual = new int[buttonNumber][];
		size = new int[buttonNumber][];

		// set0
		int m0[] = {38,1};
		int v0[] = {3,1};
		int s0[] = {1,1};
		
		// set1
		int m1[] = {38,35};
		int v1[] = {3,24};
		int s1[] = {1,2};
		
		// set2
		int m2[] = {38,26,26};
		int v2[] = {3,3,2};
		int s2[] = {1,2,2};

		/*
		// set3
		int m3[] = {38,5};
		int v3[] = {3,2};
		int s3[] = {1,2};

		// set4
		int m4[] = {38,13};
		int v4[] = {3,12};
		int s4[] = {1,2};
		
		// set5
		int m5[] = {38,2,2};
		int v5[] = {3,3,5};
		int s5[] = {1,2,2};
		
		// set6
		int m6[] = {38,16};
		int v6[] = {3,3};
		int s6[] = {1,2};
		
		// set7
		int m7[] = {38,26,26};
		int v7[] = {3,3,2};
		int s7[] = {1,2,2};		
		*/
		panelDataCreation(m0,v0,s0,0);
		panelDataCreation(m1,v1,s1,1);
		panelDataCreation(m2,v2,s2,2);
/*		panelDataCreation(m3,v3,s3,3);
		panelDataCreation(m4,v4,s4,4);
		panelDataCreation(m5,v5,s5,5);
		panelDataCreation(m6,v6,s6,6);
		panelDataCreation(m7,v7,s7,7);
	*/	

		
		String buttonNames[] = {"Editor","WordInfo","Impolite","Subject","Liver","WordExt","LongSent","Impolite"};
		
		if(isMenuInJapanese())
			for(int i=0;i<buttonNumber;i++)
				buttonNames[i] = inJapanese[23+i];
		
		buttonGroup = new ButtonGroup();			
		
		for(int i=0;i<buttonNumber;i++)
		{
			toggleButton[i] = new JToggleButton(buttonNames[i]);
			toggleButton[i].setBackground(Color.green);
			toggleButton[i].addActionListener(this);
			operationPanel.add(toggleButton[i]);
			buttonGroup.add(toggleButton[i]);			
		}
		int focus = getMainFocusInteger();
		if(focus >= 0 && focus < buttonNumber)
			toggleButton[focus].setSelected(true);	
	}
	
	public void initializeData()
	{

	    String titleLists[] = {"Items","Results","Segments","Sentences","Subjects","Key Sentence","Lighting Rate","Coherence Rate",
							"Word Expression","Long Sentences","Total Score","Text","pts.","Keywords","Main Character","Subject Rate","Overlap Rate",
							"Structure","Expression","Wordy Sentences","Impolite Rate","Kanji Rate"};
		
		if(isMenuInJapanese())
			titles = inJapanese;
		else
			titles = titleLists;	
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 4502:
			case 0:		
				checkedText = check_text();				
				resetData();
				setDataString(checkedText);
				break;
				
			case 1:
				displayOperations(0);
				break;
		}
	}
    public String check_text()
	{
		StringWriter sw1 = new StringWriter();
		BufferedWriter bw1 = new BufferedWriter(sw1);
		
        int keywordCount=0;
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].frequency > 0)
                keywordCount++;
        
		try
		{
			//header
			bw1.write("<HTML><HEAD><TITLE>ANALYSIS</TITLE></HEAD><BODY>");
			bw1.write("<H1>"+titles[11]+" ["+(new File(text.sourceFilename).getName())+"] </H1>");

			
			//BASIC TABLE
			bw1.write("<H1>"+titles[0]+"</H1>");			
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");			
			
			bw1.write("<H3>"+titles[1]+"</H3>");			
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[22]+"</TD>");
			bw1.write("<TD ALIGN=center>"+text.textLength+"</TD>");
			bw1.write("</TR>");				
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[2]+"</TD>");
			bw1.write("<TD ALIGN=center>"+text.segmentNumber+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[3]+"</TD>");
			bw1.write("<TD ALIGN=center>"+(int)(text.sentenceNumber/text.segmentNumber)+"</TD>");
			bw1.write("</TR>");			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[13]+"</TD>");
			bw1.write("<TD ALIGN=center>"+keywordCount+"</TD>");
			bw1.write("</TR>");			
			
			bw1.write("</TABLE>");
			bw1.write("</TABLE>");			
			
			bw1.write("</BODY></HTML>");
			bw1.flush();
		}
		catch(Exception e){
			System.out.println("Error in ProfileCheck");
            e.printStackTrace();            
		}

		return sw1.toString();
	}
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeSelectionButtonLog(e);/////NEW LOG
		
		for(int i=0;i<buttonNumber;i++)
			if(e.getSource() == toggleButton[i])
			{
				setMainFocusInteger(i);				
				panelSetFromModule(mining[i], visual[i], size[i]);
				return;
			}
	
	}	
}


