package source;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.awt.BorderLayout;


public class WordToolWindow extends JPanel implements TreeSelectionListener, ActionListener
{
    JScrollPane scrollPane[] = new JScrollPane[3];
    
    JPanel buttonPanel;
    JToggleButton toggleButton[] = new JToggleButton[3];//Button GROUP
    ButtonGroup buttonGroup = new ButtonGroup();
    int lastSelected = 0;

    JTree toolTree[] = new JTree[3];

    String menuInJapanese[];

    SettingData settingData;
    Control control;
    ModuleData moduleData;

    int m[], v[], t[], s[];
    
    DefaultMutableTreeNode wordToolNode[], sentenceToolNode[], segmentToolNode[];
    
    int wordMiningIDs[] =  {13,23,10,3,25, 35,48,3,9000,21, 2,26,14,5,47, 49,4,9998};
    int sentenceMiningIDs[] = {9,5,16,31,30,12,37};
    int segmentMiningIDs[] = {36,34,22, 6,24, 19,34,6,12, 11,29};
    
//    NavigationControl navigationControl;

    boolean autoSet;

    WordToolWindow(Control control)
    {
        this.control = control;

        setLayout(new BorderLayout());

        settingData = control.sp1;
        moduleData = control.moduleData;
        menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(settingData.absolutePath + "source" + File.separator + "japaneseText" + File.separator + "JapaneseToolSelect.txt"));


        buttonPanel = new JPanel(new GridLayout(1,3));
        buttonPanel.setBackground(Color.YELLOW);
        add(buttonPanel, BorderLayout.SOUTH);
        
        for(int i=0;i<3;i++)
        {
            toggleButton[i] = new JToggleButton(menuInJapanese[i]); //0,1,2
            toggleButton[i].setFont(new Font(settingData.fontName, Font.BOLD, 12));
            toggleButton[i].addActionListener(this);
            buttonPanel.add(toggleButton[i]);
            buttonGroup.add(toggleButton[i]);
        }

        ///WORD TREE [0]

//depth 1
        DefaultMutableTreeNode wordroot = new DefaultMutableTreeNode(menuInJapanese[3]);
//depth 2
        DefaultMutableTreeNode subwordroot1 = new DefaultMutableTreeNode(menuInJapanese[4]);
        DefaultMutableTreeNode subwordroot2 = new DefaultMutableTreeNode(menuInJapanese[5]);
        DefaultMutableTreeNode subwordroot3 = new DefaultMutableTreeNode(menuInJapanese[6]);
        wordroot.add(subwordroot1);
        wordroot.add(subwordroot2);
        wordroot.add(subwordroot3);
//depth 3
        DefaultMutableTreeNode subsubwordroot11 = new DefaultMutableTreeNode(menuInJapanese[7]);
        DefaultMutableTreeNode subsubwordroot12 = new DefaultMutableTreeNode(menuInJapanese[8]);
        subwordroot1.add(subsubwordroot11);
        subwordroot1.add(subsubwordroot12);

        DefaultMutableTreeNode subsubwordroot21 = new DefaultMutableTreeNode(menuInJapanese[9]);
        DefaultMutableTreeNode subsubwordroot22 = new DefaultMutableTreeNode(menuInJapanese[10]);
        DefaultMutableTreeNode subsubwordroot23 = new DefaultMutableTreeNode(menuInJapanese[11]);
        subwordroot2.add(subsubwordroot21);
        subwordroot2.add(subsubwordroot22);
        subwordroot2.add(subsubwordroot23);

//depth 4
        DefaultMutableTreeNode subsubsubwordroot111 = new DefaultMutableTreeNode(menuInJapanese[12]); //////////////// VISUALIZATION
//        subsubwordroot11.add(subsubsubwordroot111);
        addVisualizationToolNode(subsubwordroot11, subsubsubwordroot111, 1017);

        wordToolNode = new DefaultMutableTreeNode[wordMiningIDs.length + 1];
        for(int i=0;i<wordMiningIDs.length;i++)
            wordToolNode[i] = new DefaultMutableTreeNode(menuInJapanese[31]+moduleData.getModuleNameInJapanese(wordMiningIDs[i]));
        
        addToolNode(subsubwordroot11, wordToolNode, wordMiningIDs, 0);
        addToolNode(subsubwordroot11, wordToolNode, wordMiningIDs, 1);
        addToolNode(subsubwordroot11, wordToolNode, wordMiningIDs, 2);
        
        addToolNode(subsubwordroot12, wordToolNode, wordMiningIDs, 3);
        addToolNode(subsubwordroot12, wordToolNode, wordMiningIDs, 4);
        
        addToolNode(subsubwordroot21, wordToolNode, wordMiningIDs, 5);
        addToolNode(subsubwordroot21, wordToolNode, wordMiningIDs, 6);
        addToolNode(subsubwordroot21, wordToolNode, wordMiningIDs, 7);
        
        addToolNode(subsubwordroot22, wordToolNode, wordMiningIDs, 8);
        addToolNode(subsubwordroot22, wordToolNode, wordMiningIDs, 9);
        
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 10);
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 11);
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 12);
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 13);
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 14);
        addToolNode(subsubwordroot23, wordToolNode, wordMiningIDs, 15);
        
        addToolNode(subwordroot3, wordToolNode, wordMiningIDs, 16);
        addToolNode(subwordroot3, wordToolNode, wordMiningIDs, 17);

        /*
        subsubwordroot11.add(wordToolNode[0]);
        subsubwordroot11.add(wordToolNode[1]);
        subsubwordroot11.add(wordToolNode[2]);
        
        subsubwordroot12.add(wordToolNode[3]);
        subsubwordroot12.add(wordToolNode[4]);
        
        subsubwordroot21.add(wordToolNode[5]);
        subsubwordroot21.add(wordToolNode[6]);
        subsubwordroot21.add(wordToolNode[7]);
        
        subsubwordroot22.add(wordToolNode[8]);
        subsubwordroot22.add(wordToolNode[9]);
        
        subsubwordroot23.add(wordToolNode[10]);
        subsubwordroot23.add(wordToolNode[11]);
        subsubwordroot23.add(wordToolNode[12]);
        subsubwordroot23.add(wordToolNode[13]);
        subsubwordroot23.add(wordToolNode[14]);
        subsubwordroot23.add(wordToolNode[15]);
        
        subwordroot3.add(wordToolNode[16]);
        subwordroot3.add(wordToolNode[17]);
*/
        
        ///SENTENCE TREE [1]
        
        //depth 1
        DefaultMutableTreeNode sentenceroot = new DefaultMutableTreeNode(menuInJapanese[15]);
        
        sentenceToolNode = new DefaultMutableTreeNode[sentenceMiningIDs.length];
        for(int i=0;i<sentenceMiningIDs.length;i++)
        {
            sentenceToolNode[i] = new DefaultMutableTreeNode(menuInJapanese[31]+moduleData.getModuleNameInJapanese(sentenceMiningIDs[i]));
            addToolNode(sentenceroot, sentenceToolNode, sentenceMiningIDs, i);
//            sentenceroot.add(sentenceToolNode[i]);
        }

        
        ///SEGMENT TREE [2]
        
        //depth 1
        DefaultMutableTreeNode segmentroot = new DefaultMutableTreeNode(menuInJapanese[16]);
        
        //depth 2
        DefaultMutableTreeNode subsegmentroot1 = new DefaultMutableTreeNode(menuInJapanese[17]);
        DefaultMutableTreeNode subsegmentroot2 = new DefaultMutableTreeNode(menuInJapanese[18]);
        DefaultMutableTreeNode subsegmentroot3 = new DefaultMutableTreeNode(menuInJapanese[19]);
        segmentroot.add(subsegmentroot1);
        segmentroot.add(subsegmentroot2);
        segmentroot.add(subsegmentroot3);
        
        //depth 3
        DefaultMutableTreeNode subsubsegmentroot11 = new DefaultMutableTreeNode(menuInJapanese[20]);
        DefaultMutableTreeNode subsubsegmentroot12 = new DefaultMutableTreeNode(menuInJapanese[21]);
        subsegmentroot1.add(subsubsegmentroot11);
        subsegmentroot1.add(subsubsegmentroot12);
        
        DefaultMutableTreeNode subsubsubsegmentroot122 = new DefaultMutableTreeNode(menuInJapanese[22]);
        DefaultMutableTreeNode subsubsubsegmentroot123 = new DefaultMutableTreeNode(menuInJapanese[23]);
        DefaultMutableTreeNode subsubsubsegmentroot124 = new DefaultMutableTreeNode(menuInJapanese[24]);
        
        segmentToolNode = new DefaultMutableTreeNode[segmentMiningIDs.length];
        for(int i=0;i<segmentMiningIDs.length;i++)
            segmentToolNode[i] = new DefaultMutableTreeNode(menuInJapanese[31]+moduleData.getModuleNameInJapanese(segmentMiningIDs[i]));

//        subsubsegmentroot11.add(segmentToolNode[0]);//
  //      subsubsegmentroot11.add(segmentToolNode[1]);//
        
        addToolNode(subsubsegmentroot11, segmentToolNode, segmentMiningIDs, 0);
        addToolNode(subsubsegmentroot11, segmentToolNode, segmentMiningIDs, 1);
        
//        subsubsegmentroot12.add(segmentToolNode[2]);//
        addToolNode(subsubsegmentroot12, segmentToolNode, segmentMiningIDs, 2);
//        subsubsegmentroot12.add(subsubsubsegmentroot122);//
  //      subsubsegmentroot12.add(subsubsubsegmentroot123);//
    //    subsubsegmentroot12.add(subsubsubsegmentroot124);//
        addVisualizationToolNode(subsubsegmentroot12, subsubsubsegmentroot122, 1013);
        addVisualizationToolNode(subsubsegmentroot12, subsubsubsegmentroot123, 1015);
        addVisualizationToolNode(subsubsegmentroot12, subsubsubsegmentroot124, 1005);
//        subsubsegmentroot12.add(segmentToolNode[3]);//
  //      subsubsegmentroot12.add(segmentToolNode[4]);//
        addToolNode(subsubsegmentroot12, segmentToolNode, segmentMiningIDs, 3);
        addToolNode(subsubsegmentroot12, segmentToolNode, segmentMiningIDs, 4);
        
//        subsegmentroot2.add(segmentToolNode[5]);//
  //      subsegmentroot2.add(segmentToolNode[6]);//
    //    subsegmentroot2.add(segmentToolNode[7]);//
      //  subsegmentroot2.add(segmentToolNode[8]);//
        addToolNode(subsegmentroot2, segmentToolNode, segmentMiningIDs, 5);
        addToolNode(subsegmentroot2, segmentToolNode, segmentMiningIDs, 6);
        addToolNode(subsegmentroot2, segmentToolNode, segmentMiningIDs, 7);
        addToolNode(subsegmentroot2, segmentToolNode, segmentMiningIDs, 8);
        
//        subsegmentroot3.add(segmentToolNode[9]);//
  //      subsegmentroot3.add(segmentToolNode[10]);//
        addToolNode(subsegmentroot3, segmentToolNode, segmentMiningIDs, 9);
        addToolNode(subsegmentroot3, segmentToolNode, segmentMiningIDs, 10);
        
        
        ////////////
        
        toolTree[0] = new JTree(wordroot);
        toolTree[1] = new JTree(sentenceroot);
        toolTree[2] = new JTree(segmentroot);
        
        for(int i=0;i<3;i++)
        {
            toolTree[i].addTreeSelectionListener(this);
            expandAll(toolTree[i]);
        }

        setTree(lastSelected);
        
        autoSet = true;
        toggleButton[lastSelected].setSelected(true);
        autoSet = false;
    }
    
    
    public boolean addToolNode(DefaultMutableTreeNode parentNode, DefaultMutableTreeNode toolNode[], int miningIDs[], int number)
    {
        if(control.moduleData.findModule(miningIDs[number]) == -1)
            return false;
        
        parentNode.add(toolNode[number]);
        return true;
    }
    
    public boolean addVisualizationToolNode(DefaultMutableTreeNode parentNode, DefaultMutableTreeNode toolNode, int visualizationID)
    {
        if(control.moduleData.findVisualizationModule(visualizationID) == -1)
            return false;
        
        parentNode.add(toolNode);
        return true;
    }
    
    
    private void expandAll(JTree tree)
    {
        int row = 0;
        while (row < tree.getRowCount())
        {
            tree.expandRow(row);
            row++;
        }
    }

    public void setModule(int moduleID)
    {
        int moduleNumber = moduleData.findModule(moduleID);
        int length = moduleData.mining[moduleNumber].pairingVisualizationID.length;
        
        m = new int[length];
        v = new int[length];
        t = new int[length];
        s = new int[length];
        
        for(int i=0;i<length;i++)
        {
            m[i] = moduleID;
            v[i] = moduleData.mining[moduleNumber].pairingVisualizationID[i];
            t[i] = 0;
            s[i] = 1;
        }
        
        control.panelSet(m,v,t,s);
    }
    
    public void setVisualizationModule(int moduleID)
    {
        m = new int[1];
        v = new int[1];
        t = new int[1];
        s = new int[1];
        
        m[0] = 0;
        v[0] = moduleID;
        t[0] = 0;
        s[0] = 1;

        control.panelSet(m,v,t,s);
    }

    public void valueChanged(TreeSelectionEvent e)
    {
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)toolTree[lastSelected].getLastSelectedPathComponent();

        if(node != null)
        {
            String data = (String)node.getUserObject();

            for(int i=0;i<wordMiningIDs.length;i++)
                if(data.equals(menuInJapanese[31]+moduleData.getModuleNameInJapanese(wordMiningIDs[i])))
                {
                    setModule(wordMiningIDs[i]);
                    control.navigationControl.setSoundNumber(15000+i);
                    return;
                }
            
            for(int i=0;i<sentenceMiningIDs.length;i++)
                if(data.equals(menuInJapanese[31]+moduleData.getModuleNameInJapanese(sentenceMiningIDs[i])))
                {
                    setModule(sentenceMiningIDs[i]);
                    control.navigationControl.setSoundNumber(10000+i);
                    return;
                }
            
            for(int i=0;i<segmentMiningIDs.length;i++)
                if(data.equals(menuInJapanese[31]+moduleData.getModuleNameInJapanese(segmentMiningIDs[i])))
                {
                    setModule(segmentMiningIDs[i]);
                    control.navigationControl.setSoundNumber(20000+i);
                    return;
                }

            if(data.equals(menuInJapanese[12]))
            {
                setVisualizationModule(1017);
                control.navigationControl.setSoundNumber(15019);
            }
            
            if(data.equals(menuInJapanese[22]))
            {
                setVisualizationModule(1013);
                control.navigationControl.setSoundNumber(20011);
            }
 
            if(data.equals(menuInJapanese[23]))
            {
                setVisualizationModule(1015);
                control.navigationControl.setSoundNumber(20012);
            }
            
            if(data.equals(menuInJapanese[24]))
            {
                setVisualizationModule(1005);
                control.navigationControl.setSoundNumber(20013);
            }
        }
    }
    
    public void setTree(int number)
    {
        scrollPane[number] = new JScrollPane(toolTree[number]);
        add(scrollPane[number]);
        revalidate();
        lastSelected = number;
    }

    public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;
        
        for(int i=0;i<3;i++)
            if(e.getSource() == toggleButton[i])
            {
                remove(scrollPane[lastSelected]);
                setTree(i);
            }
    }

    public static void closeWindow()
    {
        //setSelected(false);
    }
}
