//
// Mining module for TETDM 
// ReadEnglish.java Version 0.36
// Copyright(C) 2011-2016 WATARU SUNAYAMA All RIGHTS RESERVED.
// This program is provided under the license.
// You Should read the README file.
//


package module.MiningModules.ReadEnglish;

import source.*;
import source.TextData.*;

import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;


public class ReadEnglish extends MiningModule implements ActionListener, Runnable
{
    int buttonNumber;

    int attribute[];
	
	String savedText="";
    
    int readingLineNumber;
    
//	Thread th;					//For thread execution
    boolean goon = false;
	
	public ReadEnglish()
	{
		setModuleID(11111);
		setCategoryID(8);
		setUseMode(1);
		pairingVisualizationID = new int[]{2};
		setToolType(2);
	}

	public void initializePanel()
	{
		operationPanel.setBackground(Color.RED);

        buttonNumber = 2;
        toggleButton = new JToggleButton[buttonNumber];
        button = new JButton[1];

        operationPanel.setLayout(new GridLayout(1,3));
        
	    for(int i=0;i<buttonNumber;i++)
		{
		    toggleButton[i] = new JToggleButton();
			toggleButton[i].setBackground(Color.red);
		    toggleButton[i].addActionListener(this);

		    operationPanel.add(toggleButton[i]);
		}
        
        button[0] = new JButton();
        button[0].setBackground(Color.red);
        button[0].addActionListener(this);
        
        operationPanel.add(button[0]);
        
        if(text.control.sp1.isWindows == true)
            toggleButton[0].setEnabled(false);
	}
	
	public void initializeData()
    {		
		String buttonNamesInEnglish[] = {"READ","Period"};

		if(isMenuInJapanese())
		{
			for(int i=0;i<buttonNumber;i++)
				toggleButton[i].setText(inJapanese[i]);
            button[0].setText(inJapanese[buttonNumber]);
		}
		else
		{
			for(int i=0;i<buttonNumber;i++)
				toggleButton[i].setText(buttonNamesInEnglish[i]);
            button[0].setText("Treat as English");
		}
		savedText = text.originalText;
        
        attribute = new int[text.sentenceNumber];
        for(int i=0;i<text.sentenceNumber;i++)
            attribute[i] = 0;
        
        readingLineNumber = 0;
    }
	
    
	public void miningOperations(int optionNumber)
	{
		switch(optionNumber)
		{
			case 0:
                resetData();
                setDataBoolean(0, true);// for TextDisplayColor
                //CAUTION FOR USING PARTIAL TEXT                
				setDataString(text.originalText);
				setDataIntegerArray(0,attribute);
				break;
		}
	}
	
    public void resetDisplay()
    {
        for(int j=0;j<text.sentenceNumber;j++)
            attribute[j] = 0;
        miningOperations(0);
        displayOperations(0);
    }
    
	//Action listener
	public void actionPerformed(ActionEvent e)
	{
		writeButtonLog(e);/////NEW LOG
		
		if(e.getSource() == toggleButton[0])
        {
            if(toggleButton[0].isSelected())
            {
                goon = true;
                Thread th = new Thread(this);
                th.start();
            }
            else
            {
                goon = false;
                resetDisplay();
            }
		}
        
        //CAUTION FOR USING PARTIAL TEXT
        if(e.getSource() == toggleButton[1])
        {
            String tounText="";
            if(toggleButton[1].isSelected())
            {
                for(int i=0;i<text.control.sp1.touns;i++)
                    tounText += (text.control.sp1.kind_touns[i]+" ");
                tounText += ".";
                
                text.control.sp1.kind_touns = tounText.split(" ");
            }
            else
            {
                for(int i=0;i<text.control.sp1.touns;i++)
                    if(!text.control.sp1.kind_touns[i].equals("."))
                        tounText += (text.control.sp1.kind_touns[i]+" ");
            }
            
            //CAUTION FOR USING PARTIAL TEXT
            text.control.sp1.kind_touns = tounText.split(" ");
            text.control.sp1.touns = text.control.sp1.kind_touns.length;

            text.control.textDataReset(text.originalText);/////
            text.control.resetSelectpanelData(text.control.text);
        }
        
        if(e.getSource() == button[0])
        {
            //CAUTION FOR USING PARTIAL TEXT
            text.control.sp1.isEnglish = true;
            text.control.createProcessingPop();
            text.control.textDataLoadingAgain();/////
            text.control.resetSelectpanelData(text.control.text);
        }
    }
    

    public void run()
    {
        resetMainSub();
        for(int i=0;i<text.sentenceNumber;i++)
        {
            //                System.out.println("i="+i+" Go on = "+goon);
            if(goon == true)
            {
                if(readingLineNumber == text.sentenceNumber)
                {
                    readingLineNumber = 0;  //END OF THE DOCUMENT
                    resetDisplay();
                    goon = false;
                    return;
                }
                setMainFocusSentence(i,inJapanese[3],"Reading",true);
                
                setDataInteger(0,readingLineNumber);
                displayOperations(30);

                EXECUTE.sayEnglishWords(text.sentence[readingLineNumber++].sentenceText);
            }
            else
            {
                resetDisplay();
                resetMainSub();
                return;
            }
        }
        goon = false;
        resetDisplay();
    }

}
