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

// You should read the README file.
//

package module.MiningModules.AppearanceDistribution;

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 AppearanceDistribution extends MiningModule implements ActionListener
{
    String displayWordList[];
	int wordHighlightNumber[];
    int dataNumber;
    
    int length[];
    double density[];
    double sortedDensity[];
    int attribute[];
    
    int sortedIndexDensity[];
    
    int border,originalBorder,maxBorder;
    
    boolean sorted;
    
    JLabel tf1;
    

	
	public AppearanceDistribution()
	{
		setModuleID(57);						// Set your module ID after you have got it
		setCategoryID(2);
		setUseMode(2);
		pairingVisualizationID = new int[]{2};
	}

	public void initializePanel()
	{
        button = new JButton[5];
        String buttonName[] = {"-10","-1","","+1","+10"};
        
        toggleButton = new JToggleButton[1];
        String toggleButtonName[] = {"sort"};
        
        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]);
        }
        
        if(isMenuInJapanese())
            toggleButton[0] = new JToggleButton(inJapanese[2]);
        else
            toggleButton[0] = new JToggleButton(toggleButtonName[0]);
        toggleButton[0].setFont(new Font(text.getFontName(), Font.BOLD, getFontSize()));
        toggleButton[0].addActionListener(this);
        operationPanel.add(toggleButton[0]);
        
        sorted = false;
	}
   
    public void createText()
    {
        if(isMenuInJapanese())
            tf1.setText(inJapanese[0]+" "+border+" "+inJapanese[1]);
        else
            tf1.setText("FREQ "+border+" +");
    }

    public void initializeData()
    {
        length = new int[text.keywordNumber];
        density = new double[text.keywordNumber];
        sortedDensity = new double[text.keywordNumber];
        attribute = new int[text.keywordNumber];
        
        int frequency[] = new int[text.keywordNumber];
        int sortedIndex[];
        for(int i=0;i<text.keywordNumber;i++)
        {
            frequency[i] = text.keyword[i].frequency;
            if(frequency[i] > 0)
            {
                length[i] = text.keyword[i].lastAppearingSegment - text.keyword[i].appearingSegment[0] + 1;
                sortedDensity[i] = density[i] = (double)text.keyword[i].segmentFrequency / length[i];
                int score = (int)(density[i]/0.1);
                if(score > 8)
                    score = 8;
                attribute[i] = 1+(8-score);
            }
        }
        sortedIndex = Qsort.getSortedIndex(frequency);
        sortedIndexDensity = Qsort.getSortedIndex(sortedDensity);
        
        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();
        countData();
    }
    
    public void miningOperations(int optionNumber)
	{
	    switch(optionNumber)
		{
            case -1:
                displayOperations(201);
                break;
                
			case 0:
                setDataBoolean(0, true);// for TextDisplayColor
                if(sorted)
                    initializeWordColorByDensityOrder();
                else
                    initializeWordColor();
                setDataStringArray(1,displayWordList);
                setDataIntegerArray(2,wordHighlightNumber);
				break;
		}
	}
    
    void countData()
    {
        int count=0;
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].frequency >= border)
                count++;
        
        dataNumber = count;
        
        int totalWord = dataNumber * (text.segmentNumber+2);
        displayWordList = new String[totalWord];
        wordHighlightNumber = new int[totalWord];
    }

    void initializeWordColor()
    {
        int count=0;
        for(int i=0;i<text.keywordNumber;i++)
            if(text.keyword[i].frequency >= border)
            {
                for(int j=0;j<text.segmentNumber;j++)
                {
                    displayWordList[count] = String.valueOf(text.keyword[i].appearingSegmentTable[j]);
                    
                    if(j >= text.keyword[i].appearingSegment[0] && j <= text.keyword[i].lastAppearingSegment)
                        wordHighlightNumber[count] = attribute[i];
                    else
                        wordHighlightNumber[count] = 10;

                    count++;
                }
                
                displayWordList[count] = " "+String.format("%.2f",density[i])+" "+text.keyword[i].word;
                wordHighlightNumber[count] = 10;
                count++;
                
                displayWordList[count] = System.getProperty("line.separator");
                wordHighlightNumber[count] = 10;
                count++;
            }
    }
    
    void initializeWordColorByDensityOrder()
    {
        int count=0, sortedID;
        for(int i=0;i<text.keywordNumber;i++)
        {
            sortedID = sortedIndexDensity[i];
            if(text.keyword[sortedID].frequency >= border)
            {
                for(int j=0;j<text.segmentNumber;j++)
                {
                    displayWordList[count] = String.valueOf(text.keyword[sortedID].appearingSegmentTable[j]);
                    
                    if(j >= text.keyword[sortedID].appearingSegment[0] && j <= text.keyword[sortedID].lastAppearingSegment)
                        wordHighlightNumber[count] = attribute[sortedID];
                    else
                        wordHighlightNumber[count] = 10;
                    
                    count++;
                }
                
                displayWordList[count] = " "+String.format("%.2f",density[sortedID])+" "+text.keyword[sortedID].word;
                wordHighlightNumber[count] = 10;
                count++;
                
                displayWordList[count] = System.getProperty("line.separator");
                wordHighlightNumber[count] = 10;
                count++;
            }
        }
    }

    void reDisplay()
    {
        createText();
        countData();
        miningOperations(0);
        miningOperations(-1);
    }
	
	//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;
                reDisplay();
            }
        }
        if(e.getSource() == button[1])
        {
            if(border > 1)
            {
                border--;
                reDisplay();
            }
        }
        if(e.getSource() == button[2])
        {
            border = originalBorder;
            reDisplay();
        }
        if(e.getSource() == button[3])
        {
            if(border < maxBorder)
            {
                border++;
                reDisplay();
            }
        }
        if(e.getSource() == button[4])
        {
            if(border < maxBorder)
            {
                border+=10;
                if(border > maxBorder)
                    border = maxBorder;
                reDisplay();
            }
        }
        if(e.getSource() == toggleButton[0])
        {
            sorted = toggleButton[0].isSelected();
            miningOperations(0);
            miningOperations(-1);
        }
        
	}	
}
