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

package module.MiningModules.WordInformation;

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 OsusumePanel extends JPanel implements ActionListener
{
    MiningModule mining;
    
    JComboBox osusumeCombo;
    
    double rowData[][];
    double dataRangeMin[];
    double dataRangeMax[];
    
    final int maxFrequency = 100;
    int frequencyData[][];
    
    int columnNumber;
    int dataNumber;
    
    String[] osusume = {"CONDITION","MAX","MIN","EXCP-MAXMIN","MIN FREQ","MAX FREQ"};
    String japaneseWords[];
    
    //ShrinkPanelForTable
    public OsusumePanel(String columnNames[], int first, int last, MiningModule mining, boolean inJapanese, String absolutePath)
    {
        this.mining = mining;
        columnNumber = last - first;
        
        japaneseWords = FILEIO.TextFileAllReadCodeArray(new File(absolutePath+"module"+File.separator +"MiningModules"+File.separator +"WordInformation"+File.separator +"OsusumeJapanese.txt"));
        
        setLayout(new GridLayout(1,1));
        setBackground(new Color(255, 192, 203));
        setOpaque(true);
        
        if(inJapanese)
            for(int i=0;i<6;i++)
                osusume[i] = japaneseWords[i];
        
        osusumeCombo = new JComboBox(osusume);
        osusumeCombo.addActionListener(this);
        add(osusumeCombo);
    }
    
    public void initializeData(double data[][], int first)
    {
        dataNumber = data[0].length;
        
        rowData = new double[columnNumber][dataNumber];
        dataRangeMin = new double[columnNumber];
        dataRangeMax = new double[columnNumber];
        
        for(int i=0;i<columnNumber;i++)
            for(int j=0;j<dataNumber;j++)
                rowData[i][j] = data[first + i][j];
        
        if(columnNumber == 0 || dataNumber == 0)
            return;

        calculateMaxMin();
        calculateFrequency();
    }
    
    void calculateMaxMin()
    {
        for(int i=0;i<columnNumber;i++)
        {
            dataRangeMax[i] = findMax(rowData[i]);
            dataRangeMin[i] = findMin(rowData[i]);
        }
    }
    
    double findMax(double a[])
    {
        double max = a[0];
        for(int i=1;i<a.length;i++)
            if(a[i] > max)
                max = a[i];
        return max;
    }
    
    double findMin(double a[])
    {
        double min = a[0];
        for(int i=1;i<a.length;i++)
            if(a[i] < min)
                min = a[i];
        return min;
    }
    
    int findMinID(int a[])
    {
        int min = dataNumber;
        int minID = -1;
        
        for(int i=0;i<a.length;i++)
            if(a[i] < min && a[i] != 0)
            {
                min = a[i];
                minID = i;
            }
        return minID;
    }
    
    int findMaxID(int a[])
    {
        int max = a[0];
        int maxID = 0;
        
        for(int i=1;i<a.length;i++)
            if(a[i] > max)
            {
                max = a[i];
                maxID = i;
            }
        return maxID;
    }

    void calculateFrequency()
    {
        frequencyData = new int[columnNumber][maxFrequency+1];
        
        for(int i=0;i<columnNumber;i++)
            for(int j=0;j<maxFrequency;j++)
                frequencyData[i][j] = 0;
        
        for(int i=0;i<columnNumber;i++)
            for(int j=0;j<dataNumber;j++)
            {
                int singleData = (int)rowData[i][j];
                if(singleData < 0 || singleData > maxFrequency)
                    continue;
                
                frequencyData[i][singleData]++;
            }
    }
    
    public void easyHighLight()
    {
        int optionNumber = osusumeCombo.getSelectedIndex() - 1;
        
        if(optionNumber == -1)
            return;
        
        int highlightColumnList[] = new int[1];
        int highlightColumnlowerBorder[] = new int[1];
        int highlightColumnupperBorder[] = new int[1];
        
        if(optionNumber == 0)
        {
            highlightColumnList = new int[columnNumber];
            highlightColumnlowerBorder = new int[columnNumber];
            highlightColumnupperBorder = new int[columnNumber];
            
            for(int i=0;i<columnNumber;i++)
            {
                highlightColumnList[i] = i+1;
                highlightColumnlowerBorder[i] = (int)dataRangeMax[i];
                highlightColumnupperBorder[i] = (int)dataRangeMax[i];
            }
        }
        else if(optionNumber == 1)
        {
            highlightColumnList = new int[columnNumber];
            highlightColumnlowerBorder = new int[columnNumber];
            highlightColumnupperBorder = new int[columnNumber];
            
            for(int i=0;i<columnNumber;i++)
            {
                highlightColumnList[i] = i+1;
                highlightColumnlowerBorder[i] = (int)dataRangeMin[i];
                highlightColumnupperBorder[i] = (int)dataRangeMin[i];
            }
        }
        else if(optionNumber == 2)
        {
            highlightColumnList = new int[columnNumber];
            highlightColumnlowerBorder = new int[columnNumber];
            highlightColumnupperBorder = new int[columnNumber];
            
            for(int i=0;i<columnNumber;i++)
            {
                highlightColumnList[i] = i+1;
                highlightColumnlowerBorder[i] = (int)dataRangeMin[i]+1;
                highlightColumnupperBorder[i] = (int)dataRangeMax[i]-1;
            }
        }
        else if(optionNumber == 3)
        {
            highlightColumnList = new int[columnNumber];
            highlightColumnlowerBorder = new int[columnNumber];
            highlightColumnupperBorder = new int[columnNumber];
            
            int border;
            
            for(int i=0;i<columnNumber;i++)
            {
                border = findMinID(frequencyData[i]);
                highlightColumnList[i] = i+1;
                highlightColumnlowerBorder[i] = border;
                highlightColumnupperBorder[i] = border;
            }
        }
        else if(optionNumber == 4)
        {
            highlightColumnList = new int[columnNumber];
            highlightColumnlowerBorder = new int[columnNumber];
            highlightColumnupperBorder = new int[columnNumber];
            
            int border;
            
            for(int i=0;i<columnNumber;i++)
            {
                border = findMaxID(frequencyData[i]);
                highlightColumnList[i] = i+1;
                highlightColumnlowerBorder[i] = border;
                highlightColumnupperBorder[i] = border;
            }
        }
        
        mining.setDataIntegerArray(0,highlightColumnList);
        mining.setDataIntegerArray(1,highlightColumnlowerBorder);
        mining.setDataIntegerArray(2,highlightColumnupperBorder);
        
        mining.displayOperations(25);
    }

    //Action listener
    public void actionPerformed(ActionEvent e)
    {
        mining.writeButtonLog(e);
        
        if(e.getSource() == osusumeCombo)
             easyHighLight();
    }
}


