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

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.GroupLayout.Alignment;


public class ModeSettingPanel extends JPanel implements ActionListener, ItemListener
{
	JPanel allTopPanel;
	JPanel middlePanel;
	
	JPanel buttonPanel;
    JLabel navigationLabel;
    JToggleButton navigationButton[] = new JToggleButton[2]; //0:Navigation-on,off, 1:Window-on,off
    
    JLabel displayLabel[] = new JLabel[3];  //0:windowSizeX, 1:windowSizeY, 2:fontSize
    JTextField display[] = new JTextField[3];  //0:windowSizeX, 1:windowSizeY, 2:fontSize

	JPanel checkPanel;
	JPanel labelPanel;
	
	
	//Top
	JLabel message;

    //Middle
	JScrollPane scroll[];
	
    JPanel checkBoxPanel1; 		
    JPanel checkBoxPanel2;	
    
	//checkBoxPanel1
    JLabel boxLabel[];    
   	JCheckBox setBoxA[];
	ButtonGroup modeGroup;	
	

	

    ButtonGroup leftGroup;
	
	String posString[] = new String[9];
	String periodString[] = new String[3];
	String sunaString;
	

	//checkBoxPanel2
//   	JCheckBox setBoxB[];	//currently not used, these are prepared for task execution

	
	//Buttom	
	JButton bt10 = new JButton();
	JButton bt20 = new JButton();	
	JButton bt40 = new JButton();
	
	JToggleButton bt80 = new JToggleButton();	
	
	int fontsize = 16;
	
	String menuInJapanese[];
	
	SettingData settingData;
	
	boolean autoSet;
	
	Control control;
	
	PopUpForModeSetting popUp;
	
	ModeSettingPanel(SettingData settingD, Control control, PopUpForModeSetting popUp)
	{
		setLayout(new BorderLayout());
		
		settingData = settingD;
		this.control = control;
		this.popUp = popUp;
		
		menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(settingData.absolutePath + "source" + File.separator + "japaneseText" + File.separator+ "JapaneseMode.txt"));		
		
		//ALL
		allTopPanel = new JPanel();
        allTopPanel.setBackground(Color.blue);
		add(allTopPanel, BorderLayout.NORTH);

		message = new JLabel();
		message.setFont(new Font(settingData.fontName, Font.BOLD, fontsize+2));
		message.setForeground(Color.white);
		
		setTitleNames();
		allTopPanel.add(message);

		
		
		//MIDDLE
		middlePanel = new JPanel();		
//		middlePanel.setLayout(new GridLayout(2,1));
//		middlePanel.setLayout(new GridLayout(1,1));		
		middlePanel.setLayout(new GridLayout(1,1));				
        add(middlePanel);
        scroll = new JScrollPane[2];
        
        //MIDDLE-INSIDE
		
		checkPanel = new JPanel(new GridLayout(4,1));
		labelPanel = new JPanel(new GridLayout(4,1));
		checkPanel.setBackground(new Color(0x7f,0xff,0xd4));
		labelPanel.setBackground(new Color(0x7f,0xff,0xd4));
		
		//game mode button
		bt80.setIcon(new ImageIcon(control.absolutePath+"source"+File.separator+"image"+File.separator+"tet100.png"));
		bt80.setSelectedIcon(new ImageIcon(control.absolutePath+"source"+File.separator+"image"+File.separator+"character100.png"));		
		bt80.addActionListener(this);
		
//		bt80.setEnabled(false);//////
        
        navigationLabel = new JLabel();
        navigationButton[0] = new JToggleButton();
        navigationButton[1] = new JToggleButton();
        
        for(int i=0;i<3;i++)
            displayLabel[i] = new JLabel();
		
		createSetPanel();
		initializeSetBox();		

		//game mode button		
		if(control.sp1.gameMode == true)
		{
			bt80.setText("Game Mode ON");
			autoSet = true;
			bt80.setSelected(true);
			autoSet = false;
		}      
	

		


		//BOTTOM
		buttonPanel = new JPanel(new GridLayout(4,3));
		add(buttonPanel, BorderLayout.SOUTH);	
		buttonPanel.setBackground(Color.GREEN);
        

        
        navigationLabel.setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        navigationButton[0].setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        navigationButton[1].setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        
        navigationLabel.setBackground(Color.YELLOW);
        navigationButton[0].setBackground(Color.YELLOW);
        navigationButton[1].setBackground(Color.YELLOW);
        
        navigationButton[0].addActionListener(this);
        navigationButton[1].addActionListener(this);
        
        buttonPanel.add(navigationLabel);
        buttonPanel.add(navigationButton[0]);
        buttonPanel.add(navigationButton[1]);
        
        //game mode button
        if(control.sp1.navigation == true)
        {
            autoSet = true;
            navigationButton[0].setSelected(true);
            autoSet = false;
        }
        
        
        
        
        for(int i=0;i<3;i++)
        {
            displayLabel[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
            display[i] = new JTextField();
            display[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize));
        }
        

        display[0].setText(""+settingData.windowSizeX);
        display[1].setText(""+settingData.windowSizeY);
        display[2].setText(""+settingData.fontSize);
        
        for(int i=0;i<3;i++)
            buttonPanel.add(displayLabel[i]);
        
        for(int i=0;i<3;i++)
            buttonPanel.add(display[i]);

        bt10.setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        bt20.setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        bt40.setFont(new Font(settingData.fontName, Font.BOLD, fontsize-4));
        
		bt10.setBackground(Color.GREEN);
		bt20.setBackground(Color.GREEN);
		bt40.setBackground(Color.GREEN);		
		
		bt10.addActionListener(this);
		bt20.addActionListener(this);
		bt40.addActionListener(this);
		
		buttonPanel.add(bt10);
		buttonPanel.add(bt20);		
		buttonPanel.add(bt40);
		
		

	}
    
	public void createSetPanel()
	{
        //CheckBox
        boxLabel = new JLabel[4];
        setBoxA = new JCheckBox[4];
//        setBoxB = new JCheckBox[5];


        for(int i=0;i<4;i++)
        {
            boxLabel[i] = new JLabel();
            boxLabel[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize));
			boxLabel[i].setBackground(new Color(0x7f,0xff,0xd4));//green label
        }
		modeGroup = new ButtonGroup();		
        
        checkBoxPanel1 = new JPanel(new BorderLayout());
		checkBoxPanel1.setBackground(new Color(0x7f,0xff,0xd4));//green panel
//		checkBoxPanel1.setLayout(new GridLayout(4,2,5,-2));		

        
		for(int i=0;i<4;i++)
		{
            labelPanel.add(boxLabel[i]);
            
			setBoxA[i] = new JCheckBox();
			setBoxA[i].setBackground(new Color(0x7f,0xff,0xd4));//green checkbox
			setBoxA[i].addItemListener(this);
			checkPanel.add(setBoxA[i]);
			
			modeGroup.add(setBoxA[i]);
		}
		
//		labelPanel.add(bt80);
		
//		checkBoxPanel1.add(bt80, BorderLayout.SOUTH);		GAME MODE
        
		checkBoxPanel1.add(labelPanel);
		checkBoxPanel1.add(checkPanel, BorderLayout.WEST);		
		
		
        scroll[0] = new JScrollPane(checkBoxPanel1);
        middlePanel.add(scroll[0]);
        


        /*
        //BOX continue
        checkBoxPanel2 = new JPanel();
		checkBoxPanel2.setBackground(Color.pink);
		checkBoxPanel2.setLayout(new GridLayout(5,1,5,-2));
        
		for(int i=0;i<5;i++)
		{
			setBoxB[i] = new JCheckBox();
			setBoxB[i].addItemListener(this);
			setBoxB[i].setBackground(Color.white);
			checkBoxPanel2.add(setBoxB[i]);
		}
        
        scroll[1] = new JScrollPane(checkBoxPanel2);
 //       middlePanel.add(scroll[1]);
*/
        
        //Names
		setNames();        
	}

	public void setTitleNames()
	{
		if(settingData.inJapanese)
			message.setText(menuInJapanese[0]);
		else
			message.setText("Mode SETTINGS");
	}
	

	
	public void setNames()
	{
        String boxLabelWords[] = {"[Super Light Mode]","[Light Mode]","[Public Mode]","[Advanced Mode]"};
        
        String setWordsB[] = {"Exclude HIRAGANA words", "Exclude KATAKANA words", "Exclude One character words","Exclude HANKAKU words","Menu in Japanese (Reconstruct panels)"};
		
		if(settingData.inJapanese)
		{
            for(int i=0;i<4;i++)
                boxLabel[i].setText(menuInJapanese[4+i]);

			/*
			for(int i=0;i<4;i++)
				setBoxB[i].setText(menuInJapanese[8+i]);
            setBoxB[4].setText(menuInJapanese[34]);
*/
            navigationLabel.setText(menuInJapanese[51]);
            navigationButton[0].setText(menuInJapanese[52]);
            navigationButton[1].setText(menuInJapanese[53]);
            
            displayLabel[0].setText(menuInJapanese[12]);    /////
            displayLabel[1].setText(menuInJapanese[13]);    /////
            displayLabel[2].setText(menuInJapanese[14]);    /////
            
			bt10.setText(menuInJapanese[1]);
			bt20.setText(menuInJapanese[43]);
			bt40.setText(menuInJapanese[2]);
			bt80.setText("Game Mode OFF");			
		}
		else
		{
            for(int i=0;i<4;i++)
                boxLabel[i].setText(boxLabelWords[i]);
            
//			for(int i=0;i<5;i++)
//				setBoxB[i].setText(setWordsB[i]);

            navigationLabel.setText("Navigation");
            navigationButton[0].setText("ON");
            navigationButton[1].setText("WINDOW");
            
            displayLabel[0].setText(" Width (INIT. 0)");    /////
            displayLabel[1].setText(" Height (INIT. 0)");    /////
            displayLabel[2].setText(" FontSize (INIT. 0)");    /////
            
			bt10.setText("Save Settings");
			bt20.setText("All Initialization");			
			bt40.setText("Quit Settings");
			bt80.setText("Game Mode OFF");
		}
        bt40.setForeground(Color.red);
	}
	
	public void initializeSetBox()
	{
        autoSet = true;
        
		setBoxA[settingData.mode].setSelected(true);		
/*
		for(int i=0;i<5;i++)
			setBoxB[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize));

		setBoxB[0].setSelected(settingData.excludeHiragana);
		setBoxB[1].setSelected(settingData.excludeKatakana);
		setBoxB[2].setSelected(settingData.excludeOne);
		setBoxB[3].setSelected(settingData.excludeHankaku);
		setBoxB[4].setSelected(settingData.inJapanese);
*/		
        autoSet = false;
	}
	

	///////////////// EXP ///////////////
	public void buttonExp(ActionEvent e)
	{
		int number = -1;
		
		if(e.getSource() == bt10)
			number = 0;
		if(e.getSource() == bt20)
			number = 1;
		if(e.getSource() == bt40)
			number = 2;
		if(e.getSource() == bt80)
			number = 3;		
		
		if(number >= 0)
			addExpByModeButton(number);			
		
	}
	
	public void addExpByModeButton(int number)
	{
		control.ex1.addExpByModeButton(number);
		if(control.expPanel != null)		
			control.expPanel.repaint();		
	}
	
	public String getButtonName(ActionEvent e)
	{
		return control.getButtonName(e);
	}


	/*
	public class ReConfirmation extends JFrame
	{		
		ReConfirmation()
		{
			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			setBounds(10, 10, 300, 200);
			setTitle("ReConfirmation");
			setVisible(true);			
			
			JOptionPane op = new JOptionPane("REALLY OK?",JOptionPane.WARNING_MESSAGE,JOptionPane.YES_NO_OPTION);
			JPanel p = new JPanel();
			p.add(op);   //JOptionPane
			getContentPane().add(p, BorderLayout.CENTER);
		}
	}	
*/
    
    public void quitTetdm()
    {
        control.mainf.dispose(); //destroy frame
        System.exit(0);
    }

	//Action listener
	public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;			
		
    	///////////////// EXP ///////////////
		buttonExp(e);
		
//		String buttonName = getButtonName(e);
		
		//writeActionLog(buttonName);
		//writeActionLog("MODE",buttonName);


        // NAVIGATION
        if(e.getSource() == navigationButton[0])
        {
            if(navigationButton[0].isSelected())
                control.setNavigation(true);
            else
                control.setNavigation(false);
        }
        
        if(e.getSource() == navigationButton[1])
        {
            if(navigationButton[1].isSelected())
            {
                navigationButton[0].setSelected(true);
                control.setNavigationWindow(true);
            }
            else
            {
                control.setNavigationWindow(false);
            }
        }
        
		// Game Mode
		if(e.getSource() == bt80)
		{            
			if(bt80.isSelected())
			{
				bt80.setText("Game Mode ON");
				control.setGameMode(true);
			}
			else
			{
				bt80.setText("Game Mode OFF");				
				control.setGameMode(false);
			}
		}			
		
		if(e.getSource() == bt10)//Save Settings
		{
			int option;
			if(settingData.inJapanese)
				option = JOptionPane.showConfirmDialog(this, menuInJapanese[44],  menuInJapanese[45], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
			else
				option = JOptionPane.showConfirmDialog(this, "SAVE MODE and CURRENT PANELS OK?", "OK", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
			
			if(option != JOptionPane.YES_OPTION)
				return;
            
            try
            {
                int value1,value2,value3;
                
                value1 = Integer.parseInt(display[0].getText());
                if(value1 < 640 && value1 != 0)
                    value1 = 640;
                if(value1 > control.mainf.windowSizeX)
                    value1 = control.mainf.windowSizeX;
                
                value2 = Integer.parseInt(display[1].getText());
                if(value2 < 480 && value2 != 0)
                    value2 = 480;
                if(value2 > control.mainf.windowSizeY)
                    value2 = control.mainf.windowSizeY;
                
                value3 = Integer.parseInt(display[2].getText());
                if(value3 < 4 && value3 != 0)
                    value3 = 4;
                if(value3 > 50)
                    value3 = 50;

                display[0].setText(""+value1);
                display[1].setText(""+value2);
                display[2].setText(""+value3);
                
//                settingData.windowSizeX = Integer.parseInt(display[0].getText());
//                settingData.windowSizeY = Integer.parseInt(display[1].getText());
//                settingData.fontSize = Integer.parseInt(display[2].getText());
                
                if(value1 == 0 || value2 == 0 || value3 == 0)
                {
                    if(settingData.inJapanese)
                        option = JOptionPane.showConfirmDialog(this, menuInJapanese[48],  menuInJapanese[49], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                    else
                        option = JOptionPane.showConfirmDialog(this, "SAVE SIZE and QUIT TETDM OK?", "OK", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
                    
                    if(option != JOptionPane.YES_OPTION)
                    {
                        display[0].setText(""+settingData.windowSizeX);
                        display[1].setText(""+settingData.windowSizeY);
                        display[2].setText(""+settingData.fontSize);
                        
                        return;
                    }
                    else
                    {
                        settingData.windowSizeX = value1;
                        settingData.windowSizeY = value2;
                        settingData.fontSize = value3;
                        settingData.saveActivationFile();
                        quitTetdm();
                    }
                }
                
                settingData.windowSizeX = value1;
                settingData.windowSizeY = value2;
                settingData.fontSize = value3;
                
                
            }  catch(NumberFormatException nfex)  {
                display[0].setText(""+settingData.windowSizeX);
                display[1].setText(""+settingData.windowSizeY);
                display[2].setText(""+settingData.fontSize);
                
                return;
            }
			
			settingData.saveActivationFile();
			if(settingData.inJapanese)
				message.setText(menuInJapanese[3]);
			else
				message.setText("SETTINGS ARE SAVED");
		}
		
		if(e.getSource() == bt20)//Delete Settings
		{		
			int option;
			if(settingData.inJapanese)			
				option = JOptionPane.showConfirmDialog(this, menuInJapanese[46], menuInJapanese[47], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
			else
				option = JOptionPane.showConfirmDialog(this, "REALLY OK?", "OK", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);							
			if(option != JOptionPane.YES_OPTION)
				return;
            
            if(settingData.inJapanese)
                option = JOptionPane.showConfirmDialog(this, menuInJapanese[48],  menuInJapanese[49], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            else
                option = JOptionPane.showConfirmDialog(this, "SAVE SIZE and QUIT TETDM OK?", "OK", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            
            if(option != JOptionPane.YES_OPTION)
                return;
			
			control.sp1.mode = 3; //for support
			
			//support
			if(control.popForSupport == null)
				control.popForSupport = new PopUpForSupport(0,0,0,0, control, "ForUser", "operate","");
			control.popForSupport.initializeClearData();
			control.closePopForSupport();
			
			if(control.popForSupportDeveloper == null)
				control.popForSupportDeveloper = new PopUpForSupport(0,0,0,0, control, "ForDeveloper", "developer","");			
			control.popForSupportDeveloper.initializeClearData();
			control.closePopForSupportDeveloper();
				
			//.conf file
			//tetdm.conf.original -> tetdm.conf, activation.conf.original -> activation.conf			
			control.sp1.readOriginalSettingFile();
			control.sp1.saveSettingFile();
			control.sp1.saveActivationFile();
			
			setMode(0);
			
			//GameMode expData
			control.ex1.readOriginalExpFile();
			
			//manPanelInformation, Capture*
			FILEIO.deleteFile(control.absolutePath + "capture"+File.separator+"panelInformation");	
			for(int i=0;i<control.selectionAssistData.selectionNumber;i++)
				FILEIO.deleteFile(control.absolutePath + "capture"+File.separator+"Capture"+i+".png");				
			control.selectionAssistData.selectionNumber = 0;		
			
			//panelInformation, manCapture*
			FILEIO.deleteFile(control.absolutePath + "capture"+File.separator+"manPanelInformation");	
			for(int i=0;i<control.selectionAssistData.manSelectionNumber;i++)
				FILEIO.deleteFile(control.absolutePath + "capture"+File.separator+"manCapture"+i+".png");				
			control.selectionAssistData.manSelectionNumber = 0;			
			
			//input text
			//  text/*2
			
			//interpretation
			FILEIO.deleteFile(control.absolutePath + "interpretation"+File.separator+"INTERPRETATION");
			for(int i=1;i<=control.interpretationNumber;i++)
				FILEIO.deleteFile(control.absolutePath + "interpretation"+File.separator+"situation"+i+".png");				
			control.interpretationNumber = 0;
			
            FILEIO.deleteFile(control.absolutePath+"interpretation"+File.separator+"sceneData");
            
			//delete file
            FILEIO.deleteFile(control.absolutePath+"interpretation"+File.separator+"MERGELOG");
            ACTIONLOG.deleteAllLogFiles();
			FILEIO.deleteFile(control.absolutePath + "REVISEDTEXT");
            
            //delete tutorial log
            StoryControl.setSerifPath(control);
            StoryControl.DeleteLog(control);
            
            quitTetdm();
		}		

		// Quit Settings
		if(e.getSource() == bt40)
		{            
			closeWindow();
		}	
	}
	
	public void closeWindow()
	{		
		control.bt00.setSelected(false);
        control.closePopForModeSetting();
	}
	
	public void setMode()
	{
		switch(settingData.mode)
		{
			case 0:
				break;
			
			case 1:
				break;
				
			case 2:
				break;
				
			case 3:				
				control.text.initializePartialText();
				break;				
		}
	}

	
	public void setMode(int n)
	{
		settingData.mode = n;
		settingData.setMode();
		setMode();
		
		control.closeAllWindows();
		
		control.reSizeWindow();
		control.resetModuleData();
		
		control.pop.popPanel.remove(control.buttonPanel);
		control.initializeButtonPanel();
		control.pop.popPanel.add(control.buttonPanel,BorderLayout.SOUTH);
		control.pop.popPanel.revalidate();				
		
		control.panelsReConstruction(1);
		
		control.selectionAssistData.countNumber();
//		control.openCombinationWindow();
		
		System.out.println("Mode Changed to "+settingData.mode);
		
		closeWindow();		
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		String tempText;
		String currentText[];
		boolean find;
		
        if(autoSet == true)
            return;
		
		for(int i=0;i<setBoxA.length;i++)
			if(e.getSource() == setBoxA[i] && setBoxA[i].isSelected())
			{
				control.focus.clearAllFocus();
				control.text.focus.clearAllFocus();
				setMode(i);
			}
	}
}
