//
// 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.WordRelationInformation;

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 ShrinkPanel extends JPanel implements ActionListener, ChangeListener
{
    MiningModule mining;
    
    JComboBox itemCombo;
    JSlider valueSlider;
    JLabel valueLabel;
    JComboBox ijouCombo;
    
    double rowData[][];
    double dataRangeMin[];
    double dataRangeMax[];
    
    int columnNumber;
    int dataNumber;
    
    String[] koumoku = {"ITEM"};
    String[] ijou = {"+","-"};
    String japaneseWords[];
    
    //ShrinkPanelForTable
    public ShrinkPanel(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 +"ShrinkJapanese.txt"));
        
        setLayout(new GridLayout(1,4));
        setBackground(new Color(255, 255, 0));
        setOpaque(true);
        
        if(inJapanese)
            koumoku[0] = japaneseWords[0];
            
        itemCombo = new JComboBox(koumoku);
        itemCombo.addActionListener(this);
        add(itemCombo);
        
        for(int i=first;i<last;i++)
            itemCombo.addItem(columnNames[i]);
        
        valueSlider = new JSlider(0, 100, 50);
        valueSlider.addChangeListener(this);
        add(valueSlider);
        
        valueLabel = new JLabel();
        valueLabel.setText("0");
        add(valueLabel);
        
        if(inJapanese)
        {
            ijou[0] = japaneseWords[1];
            ijou[1] = japaneseWords[2];
        }
        
        ijouCombo = new JComboBox(ijou);
        ijouCombo.addActionListener(this);
        add(ijouCombo);
    }
    
    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();
    }
    
    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;
    }
    
    public void createHighLight()
    {
        int selected = itemCombo.getSelectedIndex() - 1;
        int ijou = ijouCombo.getSelectedIndex();
        
        if(selected == -1)
            return;
        
        double max = dataRangeMax[selected];
        double min = dataRangeMin[selected];
        int border = (int)(min + (max-min)*valueSlider.getValue()/100.0);
        
        valueLabel.setText(" "+border);
        
        int highlightColumnList[] = new int[1];
        int highlightColumnlowerBorder[] = new int[1];
        int highlightColumnupperBorder[] = new int[1];
        
        highlightColumnList[0] = selected+1;
        if(ijou == 0)
        {
            highlightColumnlowerBorder[0] = (int)border;
            highlightColumnupperBorder[0] = (int)max+1;
        }
        else
        {
            highlightColumnlowerBorder[0] = (int)min-1;
            highlightColumnupperBorder[0] = (int)border;
        }
        
        mining.setDataIntegerArray(0,highlightColumnList);
        mining.setDataIntegerArray(1,highlightColumnlowerBorder);
        mining.setDataIntegerArray(2,highlightColumnupperBorder);
        
        System.out.println("HighLight-----"+highlightColumnList[0]+","+highlightColumnlowerBorder[0]+","+highlightColumnupperBorder[0]);
        
        mining.displayOperations(25);
    }
    
    public void stateChanged(ChangeEvent e)
    {
        createHighLight();
    }
    
    //Action listener
    public void actionPerformed(ActionEvent e)
    {
        mining.writeButtonLog(e);
        
        if(e.getSource() == ijouCombo)
            createHighLight();
        
        if(e.getSource() == itemCombo)
            createHighLight();
    }
}


