//
// Mining module for TETDM 
// TextInfoApplication.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.TextInfoApplication;

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 TextInfoApplication extends MiningModule implements ActionListener
{
	String checkedText;

	String titles[];

//	JToggleButton toggleButton[];
	int buttonNumber;
	int mining[][], visual[][], size[][];	
	ButtonGroup buttonGroup;
	
	public TextInfoApplication()
    {
		setModuleID(40);
		setCategoryID(9);
		setUseMode(0);
		pairingVisualizationID = 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 = 11;
		
		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[] = {44,40,19,1};
		int v0[] = {0,3,3,1};
		int s0[] = {1,4,8,8};
        
        // set7
        int m1[] = {44,40,35,53,54,25};
        int v1[] = {0,3,24,24,2,21};
        int s1[] = {1,4,12,4,14,12};
        
        // set sentence
        int m2[] = {44,40,52,55};
        int v2[] = {0,3,26,2};
        int s2[] = {1,4,10,8};
		
		// set1
		int m3[] = {44,40,11,11};
		int v3[] = {0,3,1,8};
		int s3[] = {1,4,8,8};
		
		// set2
		int m4[] = {44,40,12,12,12};
		int v4[] = {0,3,2,4,1102};
		int s4[] = {1,2,6,6,6};
        
        // set4
        int m5[] = {44,40,13};
        int v5[] = {0,3,12};
        int s5[] = {1,7,13};

        
		// set3
		int m6[] = {44,40,5};
		int v6[] = {0,3,2};
		int s6[] = {1,7,13};

        // set6
        int m7[] = {44,40,16};
        int v7[] = {0,3,3};
        int s7[] = {1,7,13};
        /*
        // set5
        int m8[] = {44,40,30};
        int v8[] = {0,3,2};
        int s8[] = {1,7,13};
        */
        // set7
        int m8[] = {44,40,31};
        int v8[] = {0,3,2};
        int s8[] = {1,7,13};
        
		// set5
		int m9[] = {44,40,2,2};
		int v9[] = {0,3,3,5};
		int s9[] = {1,4,8,8};

        /*
		// set7
		int m10[] = {44,40,26,26};
		int v10[] = {0,3,3,2};
		int s10[] = {1,4,8,8};
        
        // set7
        int m11[] = {44,40,9};
        int v11[] = {0,3,2};
        int s11[] = {1,7,13};
        */
        
        // posinega
        int m12[] = {44,40,59};
        int v12[] = {0,3,2};
        int s12[] = {1,7,13};
        
		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);
		panelDataCreation(m8,v8,s8,8);
        panelDataCreation(m9,v9,s9,9);
   //     panelDataCreation(m10,v10,s10,10);
     //   panelDataCreation(m11,v11,s11,11);
        panelDataCreation(m12,v12,s12,10);
	
		
		String buttonNames[] = {"Editor","WordInfo","SentenceInfo","Sumamry","Light","Liver","Subject","LongSent","Wordy","FuzzyWord",
//            "ImpoliteWord","Annotation",
            "PosiNega"};
		
		if(isMenuInJapanese())
			for(int i=0;i<buttonNumber;i++)
				buttonNames[i] = inJapanese[25+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","Letters","Posi","Nega"};
		
		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++;
        
        int posiKey = 0;
        int negaKey = 0;
        int posiSen = 0;
        int negaSen = 0;
        int posiSeg = 0;
        int negaSeg = 0;
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].posiNega > 0)
                posiKey++;
            else if(text.keyword[i].posiNega < 0)
                negaKey++;
        for(int i=0;i<text.sentenceNumber;i++)
            if(text.sentence[i].posiNega > 0)
                posiSen++;
            else if(text.sentence[i].posiNega < 0)
                negaSen++;
        for(int i=0;i<text.segmentNumber;i++)
            if(text.segment[i].posiNega > 0)
                posiSeg++;
            else if(text.segment[i].posiNega < 0)
                negaSeg++;
		
		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("<TD ALIGN=center>"+titles[23]+"</TD>");
            bw1.write("<TD ALIGN=center>"+titles[24]+"</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("<TD ALIGN=center>"+posiSeg+"</TD>");
            bw1.write("<TD ALIGN=center>"+negaSeg+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[3]+"</TD>");
			bw1.write("<TD ALIGN=center>"+text.sentenceNumber+"</TD>");
            bw1.write("<TD ALIGN=center>"+posiSen+"</TD>");
            bw1.write("<TD ALIGN=center>"+negaSen+"</TD>");
			bw1.write("</TR>");			
			
			bw1.write("<TR>");
			bw1.write("<TD ALIGN=center>"+titles[13]+"</TD>");
			bw1.write("<TD ALIGN=center>"+keywordCount+"</TD>");
            bw1.write("<TD ALIGN=center>"+posiKey+"</TD>");
            bw1.write("<TD ALIGN=center>"+negaKey+"</TD>");
			bw1.write("</TR>");
			
			bw1.write("</TABLE>");				
			bw1.write("</TABLE>");			
			
			bw1.write("</BODY></HTML>");
			bw1.flush();
		}
		catch(Exception e){
			System.out.println("Error in TextInfoApplication");
            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]);
                
                System.out.println("CALEED ACTION PERFORMED ---------"+i);
                
                text.control.navigationControl.setSoundNumber(511+i);
                
                StoryControl.nextSerif(10004001+i);
				return;
			}
	
	}
}


