//
// Mining module for TETDM 
// FirstAppearance Version 4.00 2018.12.19
// Copyright(C) 2018-
// 

// You should read the README file.
//

package module.MiningModules.FirstAppearance;

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

import java.io.*;

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

import java.util.*;

public class FirstAppearance extends MiningModule implements ActionListener
{
	int wordHighlightNumber[];
    int border,originalBorder,maxBorder;
    
    JLabel tf1;
    
    int wordPositionInText[];
	
	public FirstAppearance()
	{
		setModuleID(56);						// Set your module ID after you have got it
		setCategoryID(2);
		setUseMode(2);
		pairingVisualizationID = new int[]{2};
	}

	public void initializePanel()
	{
        String buttonName[] = {"-10","-1","","+1","+10"};
        button = new JButton[5];
        
        tf1 = new JLabel();
        tf1.setFont(new Font(text.getFontName(), Font.BOLD, getFontSize()));
        operationPanel.add(tf1);
        
        for(int i=0;i<5;i++)
        {
            button[i] = new JButton(buttonName[i]);
            button[i].setFont(new Font(text.getFontName(), Font.BOLD, getFontSize()));
            button[i].addActionListener(this);
            operationPanel.add(button[i]);
        }
	}
    
    public void createText()
    {
        if(isMenuInJapanese())
            tf1.setText(inJapanese[0]+" "+border+" "+inJapanese[1]);
        else
            tf1.setText("FREQ "+border+" +");
    }

    public void initializeData()
    {
		wordHighlightNumber = new int[text.wordNumber];
        int frequency[] = new int[text.keywordNumber];
        int sortedIndex[];
        for(int i=0;i<text.keywordNumber;i++)
            frequency[i] = text.keyword[i].frequency;
        sortedIndex = Qsort.getSortedIndex(frequency);
        
        border = originalBorder = maxBorder = 0;
        if(text.keywordNumber >= 10)
        {
            originalBorder = text.keyword[sortedIndex[9]].frequency;
            border = originalBorder;
            maxBorder = text.keyword[sortedIndex[0]].frequency;
        }
        button[2].setText(""+originalBorder);
        createText();
    }
	
    public void miningOperations(int optionNumber)
	{
	    switch(optionNumber)
		{
            case -1:
                displayOperations(300);
                displayOperations(12);
                break;
                
			case 0:
                setDataBoolean(0, true);// for TextDisplayColor
                initializeWordColor();
                ColorByPartOfSpeech();
                ColorByPartOfSpeech2();
                setDataIntegerArray(1,wordHighlightNumber);
				break;
		}
	}

    void initializeWordColor()
    {
        wordPositionInText = new int[text.sentenceNumber];
        if(text.sentenceNumber > 0)//
            wordPositionInText[0] = 0;
        for(int i=1;i<text.sentenceNumber;i++)
            wordPositionInText[i] = wordPositionInText[i-1] + text.sentence[i-1].wordNumber;
        
        for(int i=0;i<text.wordNumber;i++)
            wordHighlightNumber[i] = 10;
    }
    
    void ColorByPartOfSpeech()
    {
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].frequency >= border)
                for(int j=0;j<text.sentence[text.keyword[i].appearingSentence[0]].wordNumber;j++)
                    if(text.sentence[text.keyword[i].appearingSentence[0]].wordIDList[j] == i)
                    {
                        wordHighlightNumber[wordPositionInText[text.keyword[i].appearingSentence[0]]+j] = 1;
                        break;
                    }
    }

    void ColorByPartOfSpeech2()
    {
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].frequency >= border)
                for(int j=0;j<text.sentence[text.keyword[i].lastAppearingSentence].wordNumber;j++)
                    if(text.sentence[text.keyword[i].lastAppearingSentence].wordIDList[j] == i)
                    {
                        wordHighlightNumber[wordPositionInText[text.keyword[i].lastAppearingSentence]+j] = 20;
                        break;
                    }
    }
	
	
	
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);/////NEW LOG
		
        if(e.getSource() == button[0])
        {
            if(border > 1)
            {
                border-=10;
                if(border < 1)
                    border = 1;
                createText();
                miningOperations(0);
                displayOperations(12);
            }
        }
        if(e.getSource() == button[1])
        {
            if(border > 1)
            {
                border--;
                createText();
                miningOperations(0);
                displayOperations(12);
            }
        }
        if(e.getSource() == button[2])
        {
            border = originalBorder;
            createText();
            miningOperations(0);
            displayOperations(12);
        }
        if(e.getSource() == button[3])
        {
            if(border < maxBorder)
            {
                border++;
                createText();
                miningOperations(0);
                displayOperations(12);
            }
        }
        if(e.getSource() == button[4])
        {
            if(border < maxBorder)
            {
                border+=10;
                if(border > maxBorder)
                    border = maxBorder;
                createText();
                miningOperations(0);
                displayOperations(12);
            }
        }
	}	
}
