//
// Mining module for TETDM 
// SentenceEvaluation.java Version 0.30
// This is one of the sample modules.
// You should read the README file.
//

package module.MiningModules.SentenceEvaluation;

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

public class SentenceEvaluation extends MiningModule implements ActionListener
{
    String conditionFullName[];
    String conditionIcon[];
    int conditionNumber;
    int labelAttribute[];
    
    boolean selectedCondition[];
    
    int dataNumber;
    
    boolean condition[][];
    
    boolean andCondition; // false: OR condition, true: AND condition
    boolean segmentMode; // false: sentence, true: segment
    
    int countForSegment[];
    int countForSentence[];
    
	public SentenceEvaluation()
	{
		setModuleID(52);						// Set your module ID after you have got it
		setCategoryID(3);
		pairingVisualizationID = new int[]{26};
		setToolType(2);
        setUseMode(0);
	}

	public void initializePanel()
    {

        
        conditionNumber = 7;
        
        conditionFullName = new String[conditionNumber];
        conditionIcon = new String[conditionNumber];
        labelAttribute = new int[conditionNumber];
        selectedCondition = new boolean[conditionNumber];
        
        for(int i=0;i<conditionNumber;i++)
        {
            conditionFullName[i] = inJapanese[i];
            conditionIcon[i] = inJapanese[conditionNumber+i];
            //labelAttribute[i] = 30-i; /////////////////// Label COLOR
            selectedCondition[i] = false;
        }
        
        condition = new boolean[conditionNumber][];
        
        toggleButton = new JToggleButton[conditionNumber+2];
        
        operationPanel.setLayout(new GridLayout((int)((conditionNumber+2)/5)+1, 5));
        
        for(int i=0;i<conditionNumber;i++)
        {
            toggleButton[i] = new JToggleButton(inJapanese[i]);
            toggleButton[i].setBackground(Color.red);
            toggleButton[i].addActionListener(this);
            operationPanel.add(toggleButton[i]);
        }
        

        
        // AND condition or OR condition
        toggleButton[conditionNumber] = new JToggleButton();
        
        andCondition = false;   ////////////////////true: AND, false: OR
        
        if(andCondition == true)
        {
            if(isMenuInJapanese())
                toggleButton[conditionNumber].setText(inJapanese[conditionNumber*2+1]);
            else
                toggleButton[conditionNumber].setText("AND");
        }
        else
        {
            if(isMenuInJapanese())
                toggleButton[conditionNumber].setText(inJapanese[conditionNumber*2]);
            else
                toggleButton[conditionNumber].setText("OR");
        }
        toggleButton[conditionNumber].setBackground(Color.red);
        toggleButton[conditionNumber].addActionListener(this);
        operationPanel.add(toggleButton[conditionNumber]);
        
        //toggleButton[conditionNumber].setEnabled(false);  ////////////////////
        
        
        
        //SEGMENT or SENTENCE
        toggleButton[conditionNumber+1] = new JToggleButton();
        
        segmentMode = false;    ////////////////////true: SEGMENT, false: SENTENCE

        if(segmentMode == true)
        {
            if(isMenuInJapanese())
                toggleButton[conditionNumber+1].setText(inJapanese[conditionNumber*2+3]);
            else
                toggleButton[conditionNumber+1].setText("SEGMENT");
            toggleButton[conditionNumber+1].setSelected(true);
        }
        else
        {
            if(isMenuInJapanese())
                toggleButton[conditionNumber+1].setText(inJapanese[conditionNumber*2+2]);
            else
                toggleButton[conditionNumber+1].setText("SENTENCE");
        }
        toggleButton[conditionNumber+1].setBackground(Color.red);
        toggleButton[conditionNumber+1].addActionListener(this);
        operationPanel.add(toggleButton[conditionNumber+1]);
        
        //toggleButton[conditionNumber+1].setEnabled(false);  ////////////////////
        
        //ATTENTION
        button = new JButton[1];
        button[0] = new JButton(inJapanese[conditionNumber*2+4]);
        button[0].setBackground(Color.red);
        button[0].addActionListener(this);
        operationPanel.add(button[0]);
    }
	
	public void initializeData()
    {
        countForSegment = new int[text.segmentNumber];
        countForSentence = new int[text.sentenceNumber];
    }
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
                ///////////ORIGINAL///////////
                int focusID;
                if(segmentMode == true)
                    focusID = 4501;
                else
                    focusID = 4502;

                condition[0] = getNewDataBooleanArray(11, focusID, 2); //Panoramic Informative******
                condition[1] = getNewDataBooleanArray(5, focusID, 0); //Subject
                condition[2] = getNewDataBooleanArray(16, focusID, 0); //Long
    //            condition[3] = getNewDataBooleanArray(30, focusID, 0); //Similar
                condition[3] = getNewDataBooleanArray(31, focusID, 0); //Twice
                condition[4] = getNewDataBooleanArray(2, focusID, 0); //PaperCheck
//                condition[5] = getNewDataBooleanArray(26, focusID, 0); //Impolite
//                condition[6] = getNewDataBooleanArray(9, focusID, 0); //AnnotationMining

                if(segmentMode == true)
                {
                    condition[5] = new boolean[text.segmentNumber];
                    for(int i=0;i<text.segmentNumber;i++)
                        if(text.segment[i].posiNega > 0)
                            condition[5][i] = true;
                        else
                            condition[5][i] = false;
                }
                else
                {
                    condition[5] = new boolean[text.sentenceNumber];
                    for(int i=0;i<text.sentenceNumber;i++)
                        if(text.sentence[i].posiNega > 0)
                            condition[5][i] = true;
                        else
                            condition[5][i] = false;
                }
                
                if(segmentMode == true)
                {
                    condition[6] = new boolean[text.segmentNumber];
                    for(int i=0;i<text.segmentNumber;i++)
                        if(text.segment[i].posiNega < 0)
                            condition[6][i] = true;
                        else
                            condition[6][i] = false;
                }
                else
                {
                    condition[6] = new boolean[text.sentenceNumber];
                    for(int i=0;i<text.sentenceNumber;i++)
                        if(text.sentence[i].posiNega < 0)
                            condition[6][i] = true;
                        else
                            condition[6][i] = false;
                }
                
                
                
                ///////////ORIGINAL///////////
                
                resetData();
                setDataStringArray(0,conditionIcon);
                setDataBooleanArray2(0,condition);
//                setDataIntegerArray(0,labelAttribute);//////////////////
				break;
                
            case 1:
                if(segmentMode == true)
                    reviseSelectedConditionOfSegment();
                else
                    reviseSelectedConditionOfSentence();
                setDataBooleanArray(0,selectedCondition);
                displayOperations(0);
                break;
                
		}
	}
    
    public void createAttention()
    {
        int maxValue;
        int sortedIndex[];
        
        if(segmentMode)
        {
            initializeAttentionSegments();
            sortedIndex = new int[text.segmentNumber];
            for(int i=0;i<text.segmentNumber;i++)
            {
                countForSegment[i] = 0;
                for(int j=0;j<conditionNumber;j++)
                    if(condition[j][i])
                        countForSegment[i]++;
            }
            Qsort.initializeIndex(sortedIndex, sortedIndex.length);
            Qsort.quicksort(countForSegment, sortedIndex, text.segmentNumber);
            
            maxValue = countForSegment[0];
            for(int i=0;i<text.segmentNumber && countForSegment[i] == maxValue;i++)
            {
                attentionSegments[sortedIndex[i]] = true;
                System.out.println("ATTENTION SEG:"+sortedIndex[i]);
            }
            setAttentionSegments();
        }
        else
        {
            initializeAttentionSentences();
            sortedIndex = new int[text.sentenceNumber];
            for(int i=0;i<text.sentenceNumber;i++)
            {
                countForSentence[i] = 0;
                for(int j=0;j<conditionNumber;j++)
                    if(condition[j][i])
                        countForSentence[i]++;
            }
            Qsort.initializeIndex(sortedIndex, sortedIndex.length);
            Qsort.quicksort(countForSentence, sortedIndex, text.sentenceNumber);
            
            maxValue = countForSentence[0];
            for(int i=0;i<text.sentenceNumber && countForSentence[i] == maxValue;i++)
            {
                attentionSentences[sortedIndex[i]] = true;
                System.out.println("ATTENTION SEN:"+sortedIndex[i]);
            }
            setAttentionSentences();
        }

    }

    public void reviseSelectedConditionOfSegment()
    {
        for(int i=0;i<conditionNumber;i++)
            selectedCondition[i] = toggleButton[i].isSelected();
        
        initializeFocusSegments();
        
        String conditionText = "";
        
        if(andCondition == true)
            conditionText = "AND: ";
        else
            conditionText = "OR: ";
        
        for(int i=0;i<conditionNumber;i++)
            if(selectedCondition[i] == true)
                conditionText += (inJapanese[i]+" ");
        
        if(andCondition == false)
        {
            for(int i=0;i<conditionNumber;i++)
                if(selectedCondition[i] == true)
                {
                    for(int j=0;j<text.segmentNumber;j++)
                        if(condition[i][j] == true)
                            focusSegments[j] = true;
                }
        }
        else
        {
            int count1, count2;
            for(int j=0;j<text.segmentNumber;j++)
            {
                count1 = count2 = 0;
                for(int i=0;i<conditionNumber;i++)
                    if(selectedCondition[i] == true)
                    {
                        count1++;
                        if(condition[i][j] == true)
                            count2++;
                    }
                if(count1 > 0 && count1 == count2)
                    focusSegments[j] = true;
            }
        }
        
        setFocusSegments(conditionText,"selected");
        repaintOthersByFocus();
    }
    
    public void reviseSelectedConditionOfSentence()
    {
        for(int i=0;i<conditionNumber;i++)
            selectedCondition[i] = toggleButton[i].isSelected();
        
        initializeFocusSentences();
        
        String conditionText = "";
        
        if(andCondition == true)
            conditionText = "AND: ";
        else
            conditionText = "OR: ";
        
        for(int i=0;i<conditionNumber;i++)
            if(selectedCondition[i] == true)
                conditionText += (inJapanese[i]+" ");
        
        if(andCondition == false)
        {
            for(int i=0;i<conditionNumber;i++)
                if(selectedCondition[i] == true)
                {
                    for(int j=0;j<text.sentenceNumber;j++)
                        if(condition[i][j] == true)
                            focusSentences[j] = true;
                }
        }
        else
        {
            int count1, count2;
            for(int j=0;j<text.sentenceNumber;j++)
            {
                count1 = count2 = 0;
                for(int i=0;i<conditionNumber;i++)
                    if(selectedCondition[i] == true)
                    {
                        count1++;
                        if(condition[i][j] == true)
                            count2++;
                    }
                if(count1 > 0 && count1 == count2)
                    focusSentences[j] = true;
            }
        }
        
        setFocusSentences(conditionText,"selected");
        repaintOthersByFocus();
    }
    
    public void actionPerformed(ActionEvent e)
    {
        writeButtonLog(e);
        
        for(int i=0;i<conditionNumber;i++)
            if(e.getSource() == toggleButton[i])
                miningOperations(1);
        
        if(e.getSource() == toggleButton[conditionNumber])
        {
            andCondition = toggleButton[conditionNumber].isSelected();
            if(andCondition)
            {
                if(isMenuInJapanese())
                    toggleButton[conditionNumber].setText(inJapanese[conditionNumber*2+1]);
                else
                    toggleButton[conditionNumber].setText("AND");
            }
            else
            {
                if(isMenuInJapanese())
                    toggleButton[conditionNumber].setText(inJapanese[conditionNumber*2]);
                else
                    toggleButton[conditionNumber].setText("AND");
            }
            setDataBoolean(0,andCondition);
            miningOperations(1);
        }
        
        if(e.getSource() == toggleButton[conditionNumber+1])
        {
            segmentMode = toggleButton[conditionNumber+1].isSelected();
            if(segmentMode)
            {
                if(isMenuInJapanese())
                    toggleButton[conditionNumber+1].setText(inJapanese[conditionNumber*2+3]);
                else
                    toggleButton[conditionNumber+1].setText("SEGMENT");
            }
            else
            {
                if(isMenuInJapanese())
                    toggleButton[conditionNumber+1].setText(inJapanese[conditionNumber*2+2]);
                else
                    toggleButton[conditionNumber+1].setText("SENTENCE");
            }
            miningOperations(0);
            miningOperations(1);
        }
        
        if(e.getSource() == button[0])
        {
            createAttention();
            displayOperations(37);
        }
    }
}
