//
// Core Program for TETDM 
// ShopPanel.java Version 1.00
// Copyright(C) 2011-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;
import java.util.Calendar;


public class ShopPanel extends JPanel implements ActionListener
{
	ExpPanel exppanel;
	CharacterPanel characterpanel;
	ItemPanel itempanel;
	
	
	CharacterData characterdata;
	//DoubleCheckPanel doublecheckpanel;
	
	JPanel allTopPanel;
	JPanel middlePanel;
	JPanel centerPanel;
	
	//JPanel buttonPanel;

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

    //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 bt40 = new JButton();
	
	static JButton buymp = new JButton();
	 //JButton buymp = new JButton();
	 
	
	JButton Chara[];
	
	JButton cos[] = new JButton[CharacterData.CostumeNumber];
	
	int fontsize = 16;
	
	String menuInJapanese[];
	
	SettingData settingData;
	
	boolean autoSet;
	Control control;
	PopUpForShop popUp;
	
	
	ImageIcon Icon[];
	ImageIcon smallIcon[];
	static ImageIcon cosIcon[];
	static ImageIcon cossmallIcon[];
	
	//CharacterData characterdata;
	
	Image smallImg;	
	Calendar cal = Calendar.getInstance();
	int year = cal.get(Calendar.YEAR);
	int month = cal.get(Calendar.MONTH) + 1;
	int day = cal.get(Calendar.DATE);
	int hour = cal.get(Calendar.HOUR);
	int minute = cal.get(Calendar.MINUTE);
	int second = cal.get(Calendar.SECOND);

	ShopPanel(Control control, PopUpForShop popUp)
	{
		setLayout(new BorderLayout());
		
		settingData = control.sp1;
		this.control = control;
		this.popUp = popUp;
		
		//charactername = new CharacterData[5];
		
		Chara = new JButton[CharacterData.CharacterNumber];
		
		
		//CharacterData.buycharacternumber = new int[CharacterData.CharacterNumber];
		
		
		//for(int i=0;i<characterdata.CharacterNumber;i++){
		//if(characterdata.buycharacternumber[i] == 0) characterdata.buycharacternumber[i] = 0;
		//}
	
		
		
		menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "GameMode" + File.separator+ "Shop.txt"));		
		
		for(int i = 0;i<CharacterData.CostumeNumber;i++){
		cos[i] = new JButton();
		}
		//CharacterData.cosnumber = new int[18]; 
		
		//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);
		
		//Center
		centerPanel = new JPanel();
        	centerPanel.setBackground(new Color(221,132,22));
		//add(centerPanel, BorderLayout.WEST);

		message2 = new JLabel();
		message2.setFont(new Font(settingData.fontName, Font.BOLD, fontsize+2));
		message2.setForeground(Color.white);
		//message2.setBounds(10,10,10,10);
		

		centerPanel.add(message2);
		
		
		//MIDDLE
		middlePanel = new JPanel();		
//		middlePanel.setLayout(new GridLayout(2,1));
		middlePanel.setLayout(new GridLayout(1,1));		
        add(middlePanel);
        scroll = new JScrollPane[2];
        
        //MIDDLE-INSIDE
		
		
		checkPanel = new JPanel(new GridLayout(5,1));
		//labelPanel = new JPanel(new GridLayout(13,2));
		labelPanel = new JPanel(new GridLayout((CharacterData.CharacterNumber*2)+1,2));
		checkPanel.setBackground(new Color(0x7f,0xff,0xd4));
		checkPanel.setBackground(Color.blue);
		labelPanel.setBackground(new Color(0x7f,0xff,0xd4));
		labelPanel.setBackground(new Color(221,132,22));
		
		int iconSize = 70;
		
		String imagePath = control.absolutePath+"source"+File.separator+"GameMode"+File.separator+"image"+File.separator;
		
		

		Icon = new ImageIcon[CharacterData.CharacterNumber + 1];
		smallIcon = new ImageIcon[CharacterData.CharacterNumber + 1];
		
		cosIcon = new ImageIcon[CharacterData.CostumeNumber];
		cossmallIcon = new ImageIcon[CharacterData.CostumeNumber];
		
		
		Icon[0] = new ImageIcon(imagePath +"mp.png");
		
		for(int i = 1 ; i< CharacterData.CharacterNumber + 1;i++){
		Icon[i] = new ImageIcon(imagePath + CharacterData.charaname[i-1] + File.separator + CharacterData.charaname[i-1] + "kage.png");
		}
	
		int number = 0;
		int number2 = 1;
		for(int i = 1; i<= CharacterData.CostumeNumber ;i++){
		cosIcon[i-1] = new ImageIcon(imagePath+ CharacterData.charaname[number] + File.separator + CharacterData.charaname[number] + "Cos" + number2 + "shop.png");
		if(i%3 == 0) number++;
		
		number2++;
		if(i%3 == 0) number2 = 1;
		}
		
		
		for(int i = 0; i<CharacterData.CharacterNumber + 1;i++){
		smallImg = Icon[i].getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
		smallIcon[i] = new ImageIcon(smallImg);
		}
		
	
		
		for(int i = 0;i<CharacterData.CostumeNumber;i++){
		smallImg = cosIcon[i].getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
		cossmallIcon[i] = new ImageIcon(smallImg);
		}
		
		
		buymp = new JButton("",smallIcon[0]);
		
		for(int i=0;i<CharacterData.CharacterNumber;i++){
		Chara[i] = new JButton("",smallIcon[i+1]);
		}
		
		
		
		for(int i= 0; i<CharacterData.CostumeNumber;i++){
		cos[i] = new JButton("",cossmallIcon[i]);
		}
		
		
		//game mode button
		//bt80.setIcon(new ImageIcon(imagePath+"item.png"));
		//bt80.setSelectedIcon(new ImageIcon(imagePath+"character100.png"));		
		//buymp.setBackground(new Color(0x7f,0xff,0xd4));
		buymp.setBackground(Color.white);
		buymp.setText("   "  + menuInJapanese[3] + menuInJapanese[14]);
	
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
		Chara[i].setBackground(new Color(221,222,211));
		if(CharacterData.buycharacternumber[i] == 0){
		if(i==0 || i==1) Chara[i].setText("   "  + menuInJapanese[3] + menuInJapanese[7]);
		if(i==2) Chara[i].setText("   "  + menuInJapanese[3] + menuInJapanese[8]);
		if(i==3 || i==4)  Chara[i].setText("   "  + menuInJapanese[3] + menuInJapanese[9]);
		if(i>=5) Chara[i].setText("   "  + menuInJapanese[3] + menuInJapanese[15]);
		}
		if(CharacterData.buycharacternumber[i] == 1) Chara[i].setText("   "  + menuInJapanese[5]);
		}
		
		
		for(int i = 0;i<CharacterData.CostumeNumber;i++){
		cos[i].setBackground(new Color(210,191,121));
		if(i % 3 == 0 && CharacterData.cosnumber[i] == 0) cos[i].setText("   "  +   menuInJapanese[3] + menuInJapanese[7]);
		if(i % 3 == 1 && CharacterData.cosnumber[i] == 0) cos[i].setText("   "  +   menuInJapanese[3] + menuInJapanese[8]);
		if(i % 3 == 2 && CharacterData.cosnumber[i] == 0) cos[i].setText("   "  +   menuInJapanese[3] + menuInJapanese[9]);
		if(CharacterData.cosnumber[i] == 1) cos[i].setText("   "  + menuInJapanese[5]);
		}
		
		
		
		buymp.addActionListener(this);
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
		Chara[i].addActionListener(this);
		}
		
		
		for(int i=0;i<CharacterData.CostumeNumber;i++){
		cos[i].addActionListener(this);
		}
		
		createSetPanel();
		//initializeSetBox();
	

	}
    
	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<4;i++)
		//{
            //labelPanel.add(boxLabel[i]);
            
			//setBoxA[i] = new JCheckBox();
			//setBoxA[i].setBackground(new Color(0x7f,0xff,0xd4));//green
			//setBoxA[i].addItemListener(this);
			//checkPanel.add(setBoxA[i]);
			
			//modeGroup.add(setBoxA[i]);
		//}
		
		labelPanel.add(buymp);
		
		for(int i = 0;i<CharacterData.CharacterNumber;i++){
        	labelPanel.add(Chara[i]);
		}
		
	
		for(int i=0;i<CharacterData.CostumeNumber;i++){
		labelPanel.add(cos[i]);
		}
		
		checkBoxPanel1.add(labelPanel);
		checkBoxPanel1.add(checkPanel, BorderLayout.SOUTH);		
		
		
        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));
        
        
        scroll[1] = new JScrollPane(checkBoxPanel2);
 //       middlePanel.add(scroll[1]);

      
	}

	public void setTitleNames()
	{
		if(settingData.inJapanese){
			
			message.setText(menuInJapanese[0]);
			}
		else
			message.setText("Shop");
	}
	
	
	public void Buywrite(String cname){
	try
	{
	//File file = new File(control.absolutePath +  "source" + File.separator + "GameMode" + File.separator+ "Shop.txt");
	File file = new File(control.absolutePath +  "LogData.txt");
	//File file = new File(control.absolutePath + "tetdm.exp");
	FileWriter filewriter = new FileWriter(file, true);
	filewriter.write(cname + ":" + year + month + day + ":" + hour +  minute + second + "  /total" + ExpData.totaltimeint + "\n");
	filewriter.close();
	}catch(IOException ae){
     	System.out.println(ae);
 	}
	}
	
	public void Buycoswrite(int cnumber){
	try
	{
	//File file = new File(control.absolutePath +  "source" + File.separator + "GameMode" + File.separator+ "Shop.txt");
	File file = new File(control.absolutePath +  "LogData.txt");
	//File file = new File(control.absolutePath + "tetdm.exp");
	FileWriter filewriter = new FileWriter(file, true);
	filewriter.write("cosnumber" + cnumber + ":" + year + month + day + ":" +  hour  + minute + second +  "  /total" + ExpData.totaltimeint  + "\n");
	filewriter.close();
	}catch(IOException ae){
     	System.out.println(ae);
 	}
	}

	

	//Action listener
	public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;		
		
		// Game Mode
	JLabel label = new JLabel(menuInJapanese[24]);
	
	
	if(e.getSource() == buymp )
	{
    	int option = JOptionPane.showConfirmDialog(this,label,menuInJapanese[25],JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
	if (option == JOptionPane.YES_OPTION){
	if(control.ex1.coin >= 100){
	control.ex1.addCoin(-100);
	control.ex1.addMp(1);
	itempanel.mp.setText("   "  + menuInJapanese[13] + itempanel.mpnumber);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else buymp.setText("   "  +  menuInJapanese[6] );
	}
	}
	
	for(int i=0;i<CharacterData.CharacterNumber;i++){
	if(e.getSource() == Chara[i] && CharacterData.buycharacternumber[i] == 0 ){
	
	int option = JOptionPane.showConfirmDialog(this,label,menuInJapanese[25],JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
	if (option == JOptionPane.YES_OPTION){
	
	if(i==0 || i ==1){
	if(control.ex1.coin >= 1000){
	control.ex1.addCoin(-1000);
	Chara[i].setText("   "  + menuInJapanese[5]);
	CharacterData.buycharacternumber[i] = 1;
	Buywrite(CharacterData.charaname[i]);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else Chara[i].setText("   "  + menuInJapanese[6]); 
	}
	
	if(i==2){
	if(control.ex1.coin >= 5000){
	control.ex1.addCoin(-5000);
	Chara[i].setText("   "  + menuInJapanese[5]);
	CharacterData.buycharacternumber[i] = 1;
	Buywrite(CharacterData.charaname[i]);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else Chara[i].setText("   "  + menuInJapanese[6]); 
	}
	
	if(i==3 || i==4){
	if(control.ex1.coin >= 10000){
	control.ex1.addCoin(-10000);
	Chara[i].setText("   "  + menuInJapanese[5]);
	CharacterData.buycharacternumber[i] = 1;
	Buywrite(CharacterData.charaname[i]);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else Chara[i].setText("   "  + menuInJapanese[6]); 
	}
	
	
	if(i>=5){
	if(control.ex1.coin >= 20000){
	control.ex1.addCoin(-20000);
	Chara[i].setText("   "  + menuInJapanese[5]);
	CharacterData.buycharacternumber[i] = 1;
	Buywrite(CharacterData.charaname[i]);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else Chara[i].setText("   "  + menuInJapanese[6]); 
	}
	
	if(control.expPanel != null) control.expPanel.repaint();
	control.ex1.saveExpFile();
	}
	}
	}
	
	
	for(int i = 0;i< CharacterData.CostumeNumber;i++){
	if(e.getSource() == cos[i] && CharacterData.cosnumber[i] == 0 ){
	
	int option = JOptionPane.showConfirmDialog(this,label,menuInJapanese[25],JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
	if (option == JOptionPane.YES_OPTION){
	
	if(i%3 == 0){
	if(control.ex1.coin >= 1000){
	control.ex1.addCoin(-1000);
	cos[i].setText("   "  + menuInJapanese[5]);
	CharacterData.cosnumber[i] = 1;
	Buycoswrite(i);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else cos[i].setText("   "  + menuInJapanese[6]); 
	if(control.expPanel != null) control.expPanel.repaint();
	}
	
	
	if(i%3 == 1){
	if(control.ex1.coin >= 5000){
	control.ex1.addCoin(-5000);
	cos[i].setText("   "  + menuInJapanese[5]);
	CharacterData.cosnumber[i] = 1;
	Buycoswrite(i);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else cos[i].setText("   "  + menuInJapanese[6]); 
	if(control.expPanel != null) control.expPanel.repaint();
	}
	
	
	if(i%3 == 2){
	if(control.ex1.coin >= 10000){
	control.ex1.addCoin(-10000);
	cos[i].setText("   "  + menuInJapanese[5]);
	CharacterData.cosnumber[i] = 1;
	Buycoswrite(i);
	if(control.expPanel != null) control.expPanel.repaint();
	}
	else cos[i].setText("   "  + menuInJapanese[6]); 
	if(control.expPanel != null) control.expPanel.repaint();
	}
	if(control.expPanel != null) control.ex1.saveExpFile();	
	}
	}
	}
	}
	
	public void closeWindow()
	{		
	exppanel.shop.setSelected(false);
	exppanel.closePopForShop();
	
	}
	
	

}
