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

package source.GameMode;

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 CharacterPanel extends JPanel implements ActionListener
{
	Control control;
	ExpPanel exppanel;
	ShopPanel shoppanel;
	JPanel allTopPanel;
	JPanel middlePanel;
	ExpData expdata;
	
	JPanel buttonPanel;

	JPanel checkPanel;
	JPanel labelPanel;
	
	//Gamecharacter gamecharacter;
	//CharacterImage characterimage;
	ItemPanel item;
	//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;
	

	static JToggleButton Chara[];
	//static JButton Chara[0]miko = new JButton();
	
	JButton bt40 = new JButton();
	
	int fontsize = 16;
	int i = 0;
	
	
	String menuInJapanese[];
	
	SettingData settingData;
	
	boolean autoSet;

	PopUpForCharacter popUp;
	

	Image smallImg;
	static ImageIcon Icon[];
	static ImageIcon smallIcon[];
	
	static PopUpForCharacter2 popForCharacter2;

	public static void closePopForCharacter2()
    {
        if(popForCharacter2 != null)
        {
            popForCharacter2.dispose();
            popForCharacter2 = null;
        }
    }
	
	 CharacterPanel(Control control, PopUpForCharacter popUp)
	{
		setLayout(new BorderLayout());
		
		settingData = control.sp1;
		this.control = control;
		this.popUp = popUp;
		
		menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "GameMode" + File.separator+ "Character.txt"));		
		
		//ALL
		allTopPanel = new JPanel();
        	allTopPanel.setBackground(Color.black);
		add(allTopPanel, BorderLayout.NORTH); //topmessage

		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));		
        add(middlePanel);
        scroll = new JScrollPane[2];
        
		
		
		checkPanel = new JPanel(new GridLayout(1,1));
		if(CharacterData.CharacterNumber % 2 == 0) labelPanel = new JPanel(new GridLayout(CharacterData.CharacterNumber/2,2)); //button
		if(CharacterData.CharacterNumber % 2 == 1) labelPanel = new JPanel(new GridLayout(1 + CharacterData.CharacterNumber/2,2));
		checkPanel.setBackground(new Color(0x7f,0xff,0xd4));
		checkPanel.setBackground(Color.blue);
		labelPanel.setBackground(new Color(0x7f,0xff,0xd4));
		labelPanel.setBackground(new Color(209,232,41));
		
		int iconSize = 70;
		
		String imagePath = control.absolutePath+"source"+File.separator+"GameMode"+File.separator+"image"+File.separator;
		
		Icon = new ImageIcon[CharacterData.CharacterNumber];
		smallIcon = new ImageIcon[CharacterData.CharacterNumber];
		
		Chara = new JToggleButton[CharacterData.CharacterNumber];
		
		for(int i = 0; i< CharacterData.CharacterNumber ;i++){
		Icon[i] = new ImageIcon(imagePath + CharacterData.charaname[i] + File.separator + CharacterData.charaname[i] + "Cos0.png");
		}
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
		smallImg = Icon[i].getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
		smallIcon[i] = new ImageIcon(smallImg);
		}

		
		for(int i = 0;i < CharacterData.CharacterNumber;i++){
		if(CharacterData.buycharacternumber[i] == 1) Chara[i] =  new JToggleButton("",smallIcon[i]);
		else if(CharacterData.buycharacternumber[i] == 0) Chara[i] =  new JToggleButton();
		}
		
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
		if(CharacterData.buycharacternumber[i] == 1) Chara[i].setText("   "  + menuInJapanese[4+i]);
		else if(CharacterData.buycharacternumber[i] == 0) Chara[i].setText(menuInJapanese[1]);
		}
		
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
		Chara[i].setBackground(Color.white);
		Chara[i].addActionListener(this);
		}
		

		createSetPanel();
		//initializeSetBox();		

		
			//bt80.setTextSize(32.0);
			//autoSet = true;
			//bt80.setSelected(true);
			//autoSet = false;
			

	  
	
		//BOTTOM
		buttonPanel = new JPanel(new GridLayout(1,1));
		add(buttonPanel, BorderLayout.SOUTH);	
		buttonPanel.setBackground(Color.green);		

		
		bt40.addActionListener(this);
		bt40.setBackground(Color.GREEN);
		buttonPanel.add(bt40);
		bt40.setText(menuInJapanese[3]);
	}
    
	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));
        }
		modeGroup = new ButtonGroup();		
        
        checkBoxPanel1 = new JPanel(new BorderLayout());
		checkBoxPanel1.setBackground(new Color(0x7f,0xff,0xd4));//green
//		checkBoxPanel1.setLayout(new GridLayout(4,2,5,-2));		
		
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
        	labelPanel.add(Chara[i]);
		}
		
		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("Character");
	}
	

	
	public void setNames()
	{
     
	}
	

   
	


	//Action listener
	public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;		
		
		// Game Mode
	
	for(int i = 0;i<CharacterData.CharacterNumber;i++){
	
	if(e.getSource() == Chara[i] && CharacterData.buycharacternumber[i] == 1)
	{
	if(control.expPanel != null) control.expPanel.repaint();
	
	if(Chara[i].isSelected()){
	for(int j=0;j<CharacterData.CharacterNumber;j++){
	if(j != i) Chara[j].setSelected(false);
	}
	closePopForCharacter2();
	popForCharacter2= new PopUpForCharacter2(150,70, 450, 700, control,i);
	}	 
	else
	{
	for(int j=0;j<CharacterData.CharacterNumber;j++){
	if(j != i) Chara[j].setSelected(false);
	}
	closePopForCharacter2();
	}
	}
	
	}
	
	
	if(e.getSource() == bt40)
	{
	closeWindow();
	closePopForCharacter2();

	if(control.expPanel != null) control.expPanel.repaint();
	}
	
	
	
	}
	
	public void closeWindow()
	{		
	exppanel.character.setSelected(false);
        exppanel.closePopForCharacter();
	//control.closePopForModeSetting();
	}
	
}
