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

package module.MiningModules.SetSegment;

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 SetSegment extends MiningModule implements ActionListener
{
    JPanel toolSetPanel;
    JPanel segmentSelectPanel;
    
//    JPanel numberAdjustPanel;
  //  JLabel numberAdjust;
    //JButton minusButton, plusButton;

    JToggleButton segmentButton[];
    ButtonGroup segmentButtonGroup;
    
    int maxSegmentButtonNumber;
    int segmentButtonNumber;

	public SetSegment()
    {
		setModuleID(44);
		setCategoryID(1);
		setUseMode(0);
		pairingVisualizationID = new int[]{0};
		setToolType(1);
        cancelPartialDocument = true;
	}

    public void initializePanel()
    {
        maxSegmentButtonNumber = 101;   //segmentNumber + 1
		operationPanel.setBackground(Color.PINK);
        
 /*
        numberAdjustPanel = new JPanel(new GridLayout(1,3));
        numberAdjustPanel.setBackground(Color.RED);

        minusButton = new JButton("-");
        minusButton.setBackground(Color.RED);
        minusButton.addActionListener(this);
        numberAdjustPanel.add(minusButton);
        
        numberAdjust = new JLabel(""+(maxSegmentButtonNumber-1));
        numberAdjust.setBackground(Color.RED);
        numberAdjustPanel.add(numberAdjust);

        plusButton = new JButton("+");
        plusButton.setBackground(Color.RED);
        plusButton.addActionListener(this);
        numberAdjustPanel.add(plusButton);
  */
    }
    
	public void initializeData()
	{
        segmentButtonNumber = text.segmentNumber+1;
        if(segmentButtonNumber > maxSegmentButtonNumber)
            segmentButtonNumber = maxSegmentButtonNumber;

        
        if(operationPanel != null)
        {
            remove(operationPanel);
//            operationPanel = new JPanel(new GridLayout(segmentButtonNumber+1,1));
            operationPanel = new JPanel(new GridLayout(segmentButtonNumber,1));
            operationPanel.setBackground(Color.PINK);
        }

        segmentButton = new JToggleButton[segmentButtonNumber];
        segmentButtonGroup = new ButtonGroup();
        
        if(isMenuInJapanese())
            segmentButton[0] = new JToggleButton(inJapanese[0]);
        else
            segmentButton[0] = new JToggleButton("ALL");
        segmentButton[0].setBackground(Color.PINK);
        segmentButton[0].addActionListener(this);
        operationPanel.add(segmentButton[0]);
        segmentButtonGroup.add(segmentButton[0]);
        
        boolean setfocus = text.focus.isSetFocusSegments();
        boolean focusSeg[] = getFocusSegments();
        
        for(int i=1;i<segmentButtonNumber;i++)
        {
            if(text.textID == 0)
                segmentButton[i] = new JToggleButton(""+i);
            else
                segmentButton[i] = new JToggleButton(""+(text.originalSegmentNumbers[i-1]+1));

            segmentButton[i].setBackground(Color.PINK);
            segmentButton[i].addActionListener(this);
            operationPanel.add(segmentButton[i]);
            segmentButtonGroup.add(segmentButton[i]);
   
//            if(!setfocus || focusSeg[i])
  //              segmentButton[i].setEnabled(false);
        }
    
//        operationPanel.add(numberAdjustPanel);
        
        add(operationPanel);
        operationPanel.revalidate();
	}
	
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
				break;
		}
	}
	/*
    public void copyToOriginalFocusData()
    {
        if(text.textID == 0)
            return;
        
        TextData originalTextData = text.control.text;
        boolean originalFocusSegments[] = new boolean[originalTextData.segmentNumber];
        boolean focusSegments[] = getFocusSegments();
        
        for(int i=0;i<originalTextData.segmentNumber;i++)
            originalFocusSegments[i] = false;
        
        for(int i=0;i<text.segmentNumber;i++)
            if(focusSegments[i])
                originalFocusSegments[text.originalSegmentNumbers[i]] = true;
        
        originalTextData.focus.setFocusSegments(originalFocusSegments,"");
    }
    */
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeSelectionButtonLog(e);/////NEW LOG
		
        for(int i=0;i<segmentButtonNumber;i++)
            if(e.getSource() == segmentButton[i])
            {
                shrinkReset();
                
                if(i == 0)
                {
                    text.control.panelsReConstructionfromSetSegment(-1, 0);
                }
                else
                {
                    boolean focusSegments[] = new boolean[text.segmentNumber];
                    for(int j=0;j<text.segmentNumber;j++)
                        focusSegments[j] = false;
                    focusSegments[i-1] = true;
                    setFocusSegments(focusSegments,inJapanese[1],"");
                    
                    shrinkExecute();
                }
            }
        /*
            if(e.getSource() == minusButton)
            {
                if(maxSegmentButtonNumber > 1)
                    maxSegmentButtonNumber--;
                numberAdjust.setText(""+(maxSegmentButtonNumber-1));
                
                if(segmentButtonNumber > maxSegmentButtonNumber)
                    initializeData();
            }
        
            if(e.getSource() == plusButton)
            {
                maxSegmentButtonNumber++;
                numberAdjust.setText(""+(maxSegmentButtonNumber-1));
                
                if(text.segmentNumber > segmentButtonNumber-1)
                    initializeData();
            }
         */
	}
}


