//
// Core Program for TETDM 
// SetModulePanel.java Version 1.00
// Copyright(C) 2013-2018 TETDM CORE MEMBER ALL RIGHTS RESERVED.
// This program is provided under the license.

// FROM MENU BUTTON MULTI SET MODULES
//

package source;

//import source.*;
import source.TextData.*;
import source.Utility.*;

import java.io.File;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.LineBorder;
import javax.swing.GroupLayout.Alignment;


public class SetModulePanel extends JPanel implements ActionListener, ComponentListener
{
	JPanel informationSelectPanel;
	JSplitPane splitPane;	
	JScrollPane userLayer, developerLayer;
	
	JToggleButton displayInformation;
	JRadioButton forUsers,forDevelopers;
	ButtonGroup bgroup;	
    
	String useJapanese[];
    
	SetModulePanel2 drawPanel;
	
    int width;
    int informationWidth = 300;
	int fontSize = 13;
	
	Control control;
	
	SetModulePanel(Control control, int option)
	{
		this.control = control;

		setLayout(new BorderLayout());
		addComponentListener(this);
        width = getWidth();

		useJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "japaneseText" + File.separator + "useJapanese.txt"));
			
        System.out.println("CHECK POINT-1");
		
		splitPane = new JSplitPane();
		splitPane.setOpaque(true);
   		setDivider();

		//Button
		informationSelectPanel = new JPanel();
		informationSelectPanel.setLayout(new GridLayout(1,3));
       	informationSelectPanel.setBackground(new Color(0xcc,0xff,0xff));
		add("North",informationSelectPanel);

		if(control.sp1.inJapanese)
			displayInformation = new JToggleButton(useJapanese[5],true);
		else
			displayInformation = new JToggleButton("Information Display",true);
		displayInformation.setSelected(false);
		displayInformation.setBackground(new Color(0xcc,0xff,0xff));
		displayInformation.addActionListener(this);
		displayInformation.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
		informationSelectPanel.add(displayInformation);
		
        System.out.println("CHECK POINT-2");
        
		//setModulePanel2
		drawPanel = new SetModulePanel2(control, option, displayInformation);
		userLayer = new JScrollPane(drawPanel.area);
		developerLayer = new JScrollPane(drawPanel.d_area);		
		
        System.out.println("CHECK POINT-3");

		bgroup = new ButtonGroup();
		
		if(control.sp1.inJapanese)
			forUsers = new JRadioButton(useJapanese[18],true);
		else
			forUsers = new JRadioButton("For Users",true);
		forUsers.setBackground(new Color(0xcc,0xff,0xff));
		forUsers.addActionListener(this);
		forUsers.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
		bgroup.add(forUsers);
		informationSelectPanel.add(forUsers);
		
		if(control.sp1.inJapanese)		
			forDevelopers = new JRadioButton(useJapanese[19],false);
		else
			forDevelopers = new JRadioButton("For Developers",false);
		forDevelopers.setBackground(new Color(0xcc,0xff,0xff));
		forDevelopers.addActionListener(this);
		forDevelopers.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
		bgroup.add(forDevelopers);
		informationSelectPanel.add(forDevelopers);
		
		
		if(displayInformation.isSelected())
		{
			splitPane.setLeftComponent(drawPanel);
			splitPane.setRightComponent(userLayer);
			
			add(splitPane);
		}
		else
			add(drawPanel);
	}
	
	public void setMenu()
	{
		if(control.sp1.inJapanese)
		{
			displayInformation.setText(useJapanese[5]);
			forUsers.setText(useJapanese[18]);
			forDevelopers.setText(useJapanese[19]);
		}
		else
		{
			displayInformation.setText("Information Display");
			forUsers.setText("For Users");
			forDevelopers.setText("For Developers");			
		}
		
		drawPanel.setMenu();
		
	}

    public void closeInformationWindow()
    {
        if(drawPanel != null)
            drawPanel.closeInformationWindow();
    }
    
    public void buttonExp(ActionEvent e)
	{
		int number = -1;
		
		if(e.getSource() == displayInformation)
			number = 0;
			
		
		if(number >= 0)
			addExpBySetModuleButton(number);			
		
	}
    	
		public void addExpBySetModuleButton(int number)
	{
		control.ex1.addExpBySetModuleButton(number);
		if(control.expPanel != null)		
			control.expPanel.repaint();		
	}	
	
	public String getButtonName(ActionEvent e)
	{
		return control.getButtonName(e);
	}  
	  
    
	public void actionPerformed(ActionEvent e)
	{	
		///////////////// EXP ///////////////
		buttonExp(e);
//		String buttonName = getButtonName(e);
	
		if(e.getSource() == displayInformation)		
		{
			if(displayInformation.isSelected())
			{
//				forUsers.setEnabled(true);
//				forDevelopers.setEnabled(true);

				remove(drawPanel);
				splitPane.setLeftComponent(drawPanel);
				if(forUsers.isSelected())
					splitPane.setRightComponent(userLayer);
				else
					splitPane.setRightComponent(developerLayer);
                setDivider();                
				add(splitPane);               
				revalidate();
                repaint();
			}
			else 
			{
//				forUsers.setEnabled(false);
//				forDevelopers.setEnabled(false);
					
				remove(splitPane);
				add(drawPanel);				
				revalidate();
			}
		}
		
		if(e.getSource() == forUsers)		
			if(forUsers.isSelected())
            {
				drawPanel.forUser = true;
				
				if(displayInformation.isSelected())
				{
					splitPane.setRightComponent(userLayer);
					setDivider();
				}

				drawPanel.repaint();
            }
		
		if(e.getSource() == forDevelopers)		
			if(forDevelopers.isSelected())
            {
				drawPanel.forUser = false;
				
				if(displayInformation.isSelected())
				{
					splitPane.setRightComponent(developerLayer);
					setDivider();
				}

				drawPanel.repaint();				
            }
	}
	
    public void setDivider()
    {
        width = getWidth();
        if(width < informationWidth * 2)
            splitPane.setDividerLocation(width);
        else
            splitPane.setDividerLocation(width - informationWidth);
    }
    
	//ComponentListener
	public void componentHidden(ComponentEvent e){}
	public void componentMoved(ComponentEvent e){}
	public void componentShown(ComponentEvent e){}
	public void componentResized(ComponentEvent e)
	{
        setDivider();
	}
}
