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



package module.MiningModules.PaperCheck;

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.*;

import java.util.regex.*;

public class PaperCheck extends MiningModule implements ActionListener
{
	String noises[];	//Noise/Stop words
	int noiseNumber;
	
	int score;
	
	String checkedText;

	public PaperCheck()
	{
		setModuleID(2);
		setCategoryID(4);
		setUseMode(0);
		pairingVisualizationID = new int[]{3,5};
        
        displayOperationPanel = new boolean[]{true,false};
	}
	
	public void initializePanel()
	{
        button = new JButton[1];
        
		if(isMenuInJapanese())
            button[0] = new JButton(inJapanese[0]);
        else
            button[0] = new JButton("checkresult");		
		
		operationPanel.setBackground(Color.RED);
		
		button[0].setBackground(Color.RED);
		button[0].addActionListener(this);
		operationPanel.add(button[0]);
	}	
	
	public void initializeData()
	{
		noises = fileReadArray(myModulePath+"checklist");
		noiseNumber = noises.length;
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
//                countWords();
                
				setDataString(10000,myModulePath+"checklist");	//For FileDisplay

				checkedText = checkText();
                resetData();
				setDataString(checkedText); //For TextDisplayHtml
				setDataInteger(1,(100-score)/5);//For TextInfo***
				displayOperations(0);
				break;
				
			case 1:
				initializeData();
				
				checkedText = checkText();
                resetData();                
				setDataString(checkedText); //For TextDisplayHtml
				setDataInteger(1,(100-score)/5);//For TextInfo***
				displayOperations(0);
				break;
		}
	}
    
    /*
    public void countWords()
    {
        String dictionary[] = noises;
        int count=0;
        
        for(int k=0; k<dictionary.length; k++)
        {
            Pattern pattern = Pattern.compile(dictionary[k]);
            Matcher matcher = pattern.matcher(text.originalText);
            
            while(matcher.find())
                count++;
        }
        
        System.out.println("System Count = "+count);
    }
*/
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);/////NEW LOG
		
//		insideOfActionPerformed(e);
		
		if(e.getSource() == button[0])
		{
			displayOtherModule(5,991);
			miningOperations(1);			
			executeOtherModule(19,2);
			executeOtherModule(19,1);
		}
	}
	
	public String checkText()
	{
        initializeFocusSentences();
        
		StringWriter sw1 = new StringWriter();
		BufferedWriter bw1 = new BufferedWriter(sw1);
		
		score = 100;

		try
		{
			//header
			bw1.write("<HTML><HEAD><TITLE>ANALYSIS</TITLE></HEAD><BODY>");
			bw1.write("<H1>STATUS OF TEXT</H1>");

			// each sentence evaluation
			bw1.write("<TABLE BORDER=2 BORDERCOLOR=black>");

			bw1.write("<TR>");
			bw1.write("<TD ALIGN=right>No.</TD>");
			bw1.write("<TD ALIGN=right>Comment</TD>");
			bw1.write("</TR>");

            int count = 0;
            
            for(int i=0;i<text.sentenceNumber;i++)
			{
				bw1.write("<TR>");
				bw1.write("<TD ALIGN=right>"+(i+1)+"</TD>");
				bw1.write("<TD>");


                String sent = text.sentence[i].sentenceText.replaceAll(System.getProperty("line.separator"),"");
                boolean highLightCheck[] = new boolean[sent.length()];
                for(int k=0;k<highLightCheck.length;k++)
                    highLightCheck[k] = false;
                

                
                for(int x=0;x<noiseNumber;x++)
                {
                    Pattern pattern = Pattern.compile(noises[x]);
                    Matcher matcher = pattern.matcher(sent);
                    
                    while(matcher.find())
                    {
                        String matchstr = matcher.group();
                        int startPoint = matcher.start();
                        int endPoint = startPoint + matchstr.length();
                        
                        for(int k=startPoint;k<endPoint;k++)
                        {
                            highLightCheck[k] = true;
                            focusSentences[i] = true;
                        }
                    }
                }
                
                boolean last = false;
                int lastEnd = 0;
                for(int k=0;k<highLightCheck.length;k++)
                {
                    if(highLightCheck[k] != last)
                    {
                        if(highLightCheck[k] == true)
                            bw1.write(sent.substring(lastEnd, k));
                        else
                        {
                            bw1.write("<span style=background-color:lime;>"+sent.substring(lastEnd, k)+"</span>");
                            count++;
                            score -= 5;
                        }
                        last = highLightCheck[k];
                        lastEnd = k;
                    }
                }
                if(lastEnd < highLightCheck.length)
                    if(last == false)
                        bw1.write(sent.substring(lastEnd, highLightCheck.length));
                    else
                    {
                        bw1.write("<span style=background-color:lime;>"+sent.substring(lastEnd, highLightCheck.length)+"</span>");
                        count++;
                        score -= 5;
                    }


				bw1.write("</TD>");
				bw1.write("</TR>");
            }
			
			bw1.write("<TR><TD></TD><TD>");
			bw1.write("YOUR SCORE = "+score+"</TD></TR>");
			
			bw1.write("</TABLE>");
			bw1.write("</BODY></HTML>");
			
			bw1.flush();
            
            setFocusSentences(inJapanese[1],"Fuzzy");
		}
		catch(Exception e){
			System.out.println("Error in PaperCheck");
		}
		return sw1.toString();
	}
}


