//
// Core Program for TETDM
// CharacterPanel2.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 CharacterPanel2 extends JPanel implements ActionListener
{
    Control control;
    ExpPanel exppanel;
    ShopPanel shoppanel;
    CharacterPanel characterpanel;
    //CharacterData CharacterData;
    
    JPanel allTopPanel;
    JPanel middlePanel;
    JPanel profilePanel;
    
    
    ExpData expdata;
    
    JPanel checkPanel;
    JPanel buttonpanel;
    
    //Gamecharacter gamecharacter;
    //CharacterImage characterimage;
    ItemPanel item;
    //Top
    JLabel message;
    
    //Middle
    JScrollPane scroll[];
    
    JLabel profile[];
    
    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[];
    
    
    //Buttom
    
    JButton bt40 = new JButton();
    
    int cnumber2 = -1;
    
    static JButton charaCos[];
    
    JButton magic = new JButton();
    
    int fontsize = 16;
    int i = 0;
    static int magiccount[] ={0,0,0,0,0,0};
    
    //static int characternumber = 0;
    
    String menuInJapanese[];
    
    SettingData settingData;
    
    boolean autoSet;
    
    PopUpForCharacter2 popUp;
    
    //ImageIcon icon,smallIcon,icon2,smallIcon2,icon3,smallIcon3,icon4,smallIcon4;
    //ImageIcon icon14,smallIcon14;
    Image smallImg;
    //smallImg2,smallImg3,smallImg4;
    //Image smallImg11,smallImg12,smallImg13,smallImg14;
    ImageIcon imagemagic,smallimagemagic;
    
    ImageIcon Icon[];
    ImageIcon smallIcon[];
    //ImageIcon Icon[];
    //ImageIcon smallIcon[];
    
    
    CharacterPanel2(Control control, PopUpForCharacter2 popUp,int cnumber)
    {
        //this();
        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+ "image" + File.separator +   CharacterData.charaname[cnumber] + File.separator + CharacterData.charaname[cnumber]  + ".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();
        //setProfile();
        
        allTopPanel.add(message);
        
        Icon = new ImageIcon[CharacterData.OneCostumeNumber];
        smallIcon = new ImageIcon[CharacterData.OneCostumeNumber];
        
        //MIDDLE
        middlePanel = new JPanel();
        
        //        middlePanel.setLayout(new GridLayout(2,1));
        middlePanel.setLayout(new GridLayout(1,1));
        add(middlePanel);
        scroll = new JScrollPane[2];
        
        
        charaCos = new JButton[4];
        
        profilePanel = new JPanel();
        profilePanel.setLayout(new GridLayout(14,1));
        
        add(profilePanel, BorderLayout.SOUTH);
        if(cnumber == 0) profilePanel.setBackground(new Color(178,178,178));
        if(cnumber == 1) profilePanel.setBackground(new Color(239,193,196));
        if(cnumber == 2) profilePanel.setBackground(new Color(255,241,15));
        if(cnumber == 3) profilePanel.setBackground(new Color(250,250,250));
        if(cnumber == 4) profilePanel.setBackground(new Color(157,216,140));
        if(cnumber == 5) profilePanel.setBackground(new Color(186,183,224));
        
        checkPanel = new JPanel(new GridLayout(1,1));
        buttonpanel = new JPanel(new GridLayout(3,2)); //button
        
        
        if(cnumber == 0) buttonpanel.setBackground(new Color(178,178,178));
        if(cnumber == 1) buttonpanel.setBackground(new Color(239,193,196));
        if(cnumber == 2) buttonpanel.setBackground(new Color(255,241,15));
        if(cnumber == 3) buttonpanel.setBackground(new Color(250,250,250));
        if(cnumber == 4) buttonpanel.setBackground(new Color(157,216,140));
        if(cnumber == 5) buttonpanel.setBackground(new Color(186,183,224));
        
        int iconSize = 60;
        
        String imagePath = control.absolutePath+"source"+File.separator+"GameMode"+File.separator+"image"+File.separator;
        
        imagemagic = new ImageIcon(imagePath+"magic.png");
        
        
        int i2 = 0;
        int i3 = 0;
        for(int i=0;i<CharacterData.OneCostumeNumber;i++){
            Icon[i] = new ImageIcon(imagePath + CharacterData.charaname[cnumber] + File.separator + CharacterData.charaname[cnumber] + "Cos" + i3 + ".png");
            i3++;
        }
        
        
        for(int i = 0;i<CharacterData.OneCostumeNumber;i++){
            smallImg = Icon[i].getImage().getScaledInstance(iconSize,iconSize, Image.SCALE_SMOOTH);
            smallIcon[i] = new ImageIcon(smallImg);
        }
        
        
        smallImg = imagemagic.getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
        smallimagemagic = new ImageIcon(smallImg);
        
        
        
        if(CharacterData.buycharacternumber[cnumber] == 1) charaCos[0] = new JButton("",smallIcon[0]);
        if(CharacterData.cosnumber[cnumber*3] == 1) charaCos[1] = new JButton("",smallIcon[1]);
        else charaCos[1] = new JButton("");
        
        if(CharacterData.cosnumber[cnumber*3 + 1] == 1) charaCos[2] = new JButton("",smallIcon[2]);
        else charaCos[2] = new JButton("");
        
        if(CharacterData.cosnumber[cnumber*3 + 2] == 1) charaCos[3] = new JButton("",smallIcon[3]);
        else charaCos[3] = new JButton("");
        
        magic = new JButton("",smallimagemagic);
        
        
        if(CharacterData.buycharacternumber[cnumber] == 1) charaCos[0].setText("   "  + menuInJapanese[20] );
        else if(CharacterData.buycharacternumber[cnumber] == 0) charaCos[0].setText(menuInJapanese[1]);
        
        if(CharacterData.nowcharacternumber == (cnumber+1)*10) charaCos[0].setText("   "  + menuInJapanese[2] );
        
        
        if(CharacterData.buycharacternumber[cnumber] == 1 && CharacterData.cosnumber[cnumber*3] == 1) charaCos[1].setText("   "  +menuInJapanese[21]);
        else if(CharacterData.cosnumber[cnumber*3] == 0) charaCos[1].setText(menuInJapanese[1]);
        
        if(CharacterData.nowcharacternumber == (cnumber+1)*10 + 1) charaCos[1].setText("   "  + menuInJapanese[2]);
        //if(CharacterData.characternumber == 21 && cnumber == 1) charaCos[1].setText("   "  + menuInJapanese[2]);
        
        
        if(CharacterData.buycharacternumber[cnumber] == 1 && CharacterData.cosnumber[cnumber*3 + 1] == 1) charaCos[2].setText("   "  +menuInJapanese[22]);
        else if(CharacterData.cosnumber[cnumber*3 + 1] == 0) charaCos[2].setText(menuInJapanese[1]);
        
        if(CharacterData.nowcharacternumber == (cnumber+1)*10 + 2) charaCos[2].setText("   "  + menuInJapanese[2]);
        //if(CharacterData.characternumber == 22 && cnumber == 1) charaCos[2].setText("   "  + menuInJapanese[2]);
        
        if(CharacterData.buycharacternumber[cnumber] == 1 && CharacterData.cosnumber[cnumber*3 + 2] == 1) charaCos[3].setText("   "  +menuInJapanese[23]);
        else if(CharacterData.cosnumber[cnumber*3 + 2] == 0) charaCos[3].setText(menuInJapanese[1]);
        if(CharacterData.nowcharacternumber == (cnumber+1)*10 + 3) charaCos[3].setText("   "  + menuInJapanese[2]);
        //if(CharacterData.characternumber == 23 && cnumber == 2) charaCos[3].setText("   "  + menuInJapanese[2]);
        
        charaCos[0].addActionListener(this);
        charaCos[1].addActionListener(this);
        charaCos[2].addActionListener(this);
        charaCos[3].addActionListener(this);
        
        charaCos[0].setBackground(new Color(188,200,219));
        charaCos[1].setBackground(new Color(188,200,219));
        charaCos[2].setBackground(new Color(188,200,219));
        charaCos[3].setBackground(new Color(188,200,219));
        
        
        magic.addActionListener(this);
        magic.setBackground(Color.white);
        
        createSetPanel();
        createprofilePanel();
        //initializeSetBox();
        
        //profilePanel.add(magic);
        magic.setText(menuInJapanese[28]);
        
        
        bt40.addActionListener(this);
        bt40.setBackground(Color.GREEN);
        
        profilePanel.add(bt40);
        bt40.setText(menuInJapanese[3]);
        
        cnumber2 = cnumber;
    }
    
    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++)
        {
            //buttonpanel.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]);
        }
        
        buttonpanel.add(charaCos[0]);
        buttonpanel.add(charaCos[1]);
        buttonpanel.add(charaCos[2]);
        buttonpanel.add(charaCos[3]);
        buttonpanel.add(magic);
        
        
        checkBoxPanel1.add(buttonpanel);
        //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]);
        
        
        
    }
    
    public void createprofilePanel()
    {
        profile = new JLabel[13];
        for(int i=0;i<13;i++)
        {
            profile[i] = new JLabel();
            profile[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize));
        }
        
        
        for(int i=0;i<13;i++){
            profilePanel.add(profile[i]);
            setProfile();
            
        }
        
    }
    
    
    public void setTitleNames()
    {
        if(settingData.inJapanese)
            message.setText(menuInJapanese[0]);
        else
            message.setText("Keyword SETTINGS");
    }
    
    
    public void setProfile()
    {
        for(int i = 0;i<13;i++){
            profile[i].setText(menuInJapanese[6+i]);
        }
    }
    
    //JLabel label = new JLabel("");
    //int option = JOptionPane.showConfirmDialog(this,label,"",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
    
    
    public void saveCostumeForTutorial()
    {
        int charNumber = CharacterData.nowcharacternumber/10 - 1;
        int cosNumber = CharacterData.nowcharacternumber%10;
        
        String costumeFile = control.absolutePath + "source" + File.separator + "Tutorial" + File.separator + "serif" + File.separator + "coslog.txt";
        
        try
        {
            BufferedWriter bw1 = new BufferedWriter(new FileWriter(costumeFile));
            
            bw1.write(String.valueOf(charNumber));
            bw1.newLine();
            bw1.write(String.valueOf(cosNumber));
            bw1.newLine();
            
            bw1.close();
        }
        catch(Exception e){
            System.out.println("File Writing ERROR coslog.txt");
        }
    }
    
    
    //Action listener
    public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;
        
        
        // Game Mode
        
        if(e.getSource() == charaCos[0]  && CharacterData.buycharacternumber[cnumber2] == 1)
        {
            
            //Select.popForInterpretation = new PopUpForInterpretation(imagePath, mining[selected_module],text.control,this, panelID,windowTitle);
            
            charaCos[0].setText("   " + menuInJapanese[2] );
            
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3] == 1)
                charaCos[1].setText("  " + menuInJapanese[21]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 +1] == 1)
                charaCos[2].setText("  " + menuInJapanese[22]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 2] == 1)
                charaCos[3].setText("   " + menuInJapanese[23] );
            
            CharacterData.nowcharacternumber = (cnumber2+1)*10;
            
            if(control.expPanel != null)
                control.expPanel.repaint();
            
            saveCostumeForTutorial();
            
        }
        
        if(e.getSource() == charaCos[1]  && CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3] == 1)
        {
            
            
            charaCos[1].setText("   " + menuInJapanese[2] );
            charaCos[0].setText("   " + menuInJapanese[20]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 1] == 1) charaCos[2].setText("   " + menuInJapanese[22]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 2] == 1) charaCos[3].setText("   " + menuInJapanese[23] );
            
            CharacterData.nowcharacternumber = (cnumber2+1)*10 + 1;
            
            
            
            if(control.expPanel != null) control.expPanel.repaint();
            
            saveCostumeForTutorial();
            
        }
        
        if(e.getSource() == charaCos[2]  && CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 1] == 1)
        {
            
            charaCos[2].setText("   " + menuInJapanese[2] );
            charaCos[0].setText("   " + menuInJapanese[20]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3] == 1) charaCos[1].setText("   " + menuInJapanese[21]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 2] == 1)charaCos[3].setText("   " + menuInJapanese[23] );
            
            CharacterData.nowcharacternumber = (cnumber2+1)*10 + 2;
            
            
            
            
            if(control.expPanel != null) control.expPanel.repaint();
            //expdata.save();
            
            saveCostumeForTutorial();
        }
        
        if(e.getSource() == charaCos[3]  && CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 +2] == 1)
        {
            
            charaCos[3].setText("   " + menuInJapanese[2] );
            charaCos[0].setText("   " + menuInJapanese[20]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3] == 1) charaCos[1].setText("   " + menuInJapanese[21]);
            if(CharacterData.buycharacternumber[cnumber2] == 1 && CharacterData.cosnumber[cnumber2*3 + 1] == 1) charaCos[2].setText("   " + menuInJapanese[22]);
            
            CharacterData.nowcharacternumber = (cnumber2+1)*10 + 3;
            
            
            if(control.expPanel != null) control.expPanel.repaint();
            
            saveCostumeForTutorial();
        }
        
        
        
        if(e.getSource() == magic)
        {
            if(control.ex1.stamina2 >=5){
                
                if(CharacterData.nowcharacternumber ==  (cnumber2+1)*10 || CharacterData.nowcharacternumber ==  (cnumber2+1)*10 + 1 || CharacterData.nowcharacternumber == (cnumber2+1)*10 + 2 || CharacterData.nowcharacternumber == (cnumber2+1)*10 + 3)
                {
                    
                    if(cnumber2 == 0 || cnumber2 == 1 || cnumber2 == 2 || cnumber2 == 3){
                        control.ex1.takeStamina(5);
                        magic.setText(menuInJapanese[27]);
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10) magiccount[cnumber2] = 10;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 1) magiccount[cnumber2] = 11;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 2) magiccount[cnumber2] = 12;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 3) magiccount[cnumber2] = 15;
                    }
                    else if(cnumber2 == 4){
                        control.ex1.takeStamina(5);
                        magic.setText(menuInJapanese[27]);
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10) magiccount[cnumber2] = 2;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 1) magiccount[cnumber2] = 3;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 2) magiccount[cnumber2] = 4;
                        if(CharacterData.nowcharacternumber == (cnumber2+1)*10 + 3) magiccount[cnumber2] = 5;
                    }
                    else if(cnumber2 >= 5){
                        magic.setText(menuInJapanese[30]);
                    }
                    
                    if(control.expPanel != null) control.expPanel.repaint();
                }
                else magic.setText(menuInJapanese[25]);
            }
            else magic.setText(menuInJapanese[26]);
            
            //expdata.save();
        }
        
        if(e.getSource() == bt40)
        {
            closeWindow();
            
            if(control.expPanel != null)
                control.expPanel.repaint();
            //expdata.save();
        }
    }
    
    public void closeWindow()
    {
        for(int i = 0;i<CharacterData.CharacterNumber;i++){
            characterpanel.Chara[i].setSelected(false);
        }
        characterpanel.closePopForCharacter2();
    }
    
    
}

