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

package source.Tutorial;

import source.*;
import source.TextData.*;
import source.Utility.*;
import source.GameMode.*;

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 StoryPanel extends JPanel implements ActionListener
{
    //ExpPanel exppanel;
    
    JPanel allTopPanel;
    JPanel middlePanel;
    
    JPanel checkPanel;
    JPanel labelPanel;
    
    //Top
    JLabel message;
    
    //Middle
    JScrollPane scroll[];
    
    JPanel checkBoxPanel;
    

    
    //Buttom
    JButton button[];   
    int buttonNumber[] = {0,3,5,3,5,3,3,3}; //for StoryPanel
    boolean autoSet = false;
    
    int fontsize = 16;
    
    
    String menuInJapanese[];

    ImageIcon Icon[];
    ImageIcon smallIcon[];
    Image smallImg;
    int iconSize;
    
    Control control;
    int number;
    
    boolean clearFlag[] = new boolean[13];
    String imagePath;
    

    

    

    

    
    public void clearCheck()
    {

  //   int checkNumber[] =  {0,  1,8, 1,   1,2,1,  1,10, 1,  1,17, 1};

        int checkNumber[] = new int[13];
        String clearCheckerOperation[];
        String clearCheckerTool[];
        String clearCheckerMining[];
        String clearChecker2[];
        
        System.out.println("---Clear Check in StoryPanel---");
        
        for(int i=0;i<13;i++)
        {
            if(i == 0)
                checkNumber[i] = 0;
            else if(i%3 == 2)
                checkNumber[i] = StoryControl.subMissionNumber[i];
            else
                checkNumber[i] = 1;
        }
        

        clearCheckerOperation = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "Tutorial" + File.separator + "serif" + File.separator+ "ClearCheckOperation.txt"));
        
        clearCheckerTool = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "Tutorial" + File.separator + "serif" + File.separator+ "ClearCheckTool.txt"));
        
        clearCheckerMining = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "Tutorial" + File.separator + "serif" + File.separator+ "ClearCheckMining.txt"));
        
        for(int chapterNumber=1;chapterNumber<=12;chapterNumber++)
        {
            if(chapterNumber%3 == 1)//1,4,7,10
            {
                if(chapterNumber == 1)
                {
                    if(Integer.parseInt(clearCheckerOperation[0]) >= StoryControl.subMissionNumber[chapterNumber])
                        clearFlag[chapterNumber] = true;
                    else
                        clearFlag[chapterNumber] = false;
                }
                else
                {
                    if(Integer.parseInt(clearCheckerOperation[(int)(chapterNumber/3)]) == StoryControl.subMissionNumber[chapterNumber])
                        clearFlag[chapterNumber] = true;
                    else
                        clearFlag[chapterNumber] = false;
                }
            }
         
            if(chapterNumber%3 == 2)//2,5,8,11
            {
                clearChecker2 = clearCheckerTool[(int)(chapterNumber/3)].split(",");
                clearFlag[chapterNumber] = true;
                for(int j=0;j<checkNumber[chapterNumber];j++)
                    if(j == clearChecker2.length)
                    {
                        clearFlag[chapterNumber] = false;
                        break;
                    }
                    else if(clearChecker2[j].equals("0"))
                    {
                        clearFlag[chapterNumber] = false;
                        break;
                    }
            }
 
            if(chapterNumber%3 == 0)//3,6,9,12
            {
                if(Integer.parseInt(clearCheckerMining[(int)(chapterNumber/3)-1]) >= StoryControl.subMissionNumber[chapterNumber])
                    clearFlag[chapterNumber] = true;
                else
                    clearFlag[chapterNumber] = false;
            }
        }
        
//        for(int i=1;i<=12;i++)
  //          System.out.println(i+":"+clearFlag[i]);
    }
    
    public void initializeIconButton(boolean clearCheck, int i)
    {
        if(clearCheck)
            Icon[i] = new ImageIcon(imagePath +"clear.png");
        else
            Icon[i] = new ImageIcon(imagePath +"notclear.png");
        
        smallImg = Icon[i].getImage().getScaledInstance(iconSize, iconSize, Image.SCALE_SMOOTH);
        smallIcon[i] = new ImageIcon(smallImg);
        
        button[i] = new JButton("   "  + menuInJapanese[i+1], smallIcon[i]);
        button[i].setBackground(Color.white);
        button[i].addActionListener(this);
    }
    
    StoryPanel(Control control, int number)
    {
        setLayout(new BorderLayout());
        this.control = control;
        this.number = number;
        
        button = new JButton[buttonNumber[number]];


        if(StoryControl.countSubmission == 0)   //Check ONCE
        {
            StoryControl.control = control;            
            StoryControl.countSubMissionNumber();
        }

        
        menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "Tutorial" + File.separator+ "Story"+number+".txt"));
        
        //ALL
        allTopPanel = new JPanel();
        allTopPanel.setBackground(Color.blue);
        add(allTopPanel, BorderLayout.NORTH);
        
        message = new JLabel();
        message.setFont(new Font(control.sp1.fontName, Font.BOLD, fontsize+2));
        message.setForeground(Color.white);
        
        setTitleNames();
        allTopPanel.add(message);
        
        //MIDDLE
        middlePanel = new JPanel();
        middlePanel.setLayout(new GridLayout(1,1));
        add(middlePanel);
        scroll = new JScrollPane[2];
        
        //MIDDLE-INSIDE
        checkPanel = new JPanel(new GridLayout(buttonNumber[number],1));
        checkPanel.setBackground(Color.blue);
        
        labelPanel = new JPanel(new GridLayout(buttonNumber[number],1));
        labelPanel.setBackground(Color.green);
        
        if(number == 2 || number == 4)
            iconSize = 50;
        else
            iconSize = 70;

        imagePath = control.absolutePath+"source"+File.separator+"Tutorial"+File.separator+"image"+File.separator;

            clearCheck();
        
//        for(int i=1;i<13;i++)
  //         System.out.println("clearFlag["+i+"]="+clearFlag[i]);
        
        Icon = new ImageIcon[buttonNumber[number]-1];
        smallIcon = new ImageIcon[buttonNumber[number]-1];
        
        if(number == 1)
        {
            boolean clearCheck;
            
            clearCheck=true;
            for(int i=1;i<13;i++)
            {
                if(i%3==0)
                    continue;
                if(clearFlag[i] == false)
                {
                    clearCheck = false;
                    break;
                }
            }
            initializeIconButton(clearCheck, 0);    //operation 1,2,4,5,7,8,10,11
            
            clearCheck=true;
            for(int i=0;i<4;i++)
                if(clearFlag[i*3+3] == false)
                {
                    clearCheck = false;
                    break;
                }
            initializeIconButton(clearCheck, 1);    //mining 3,6,9,12
        }
        

        if(number == 2)
        {
            for(int i=0;i<4;i++)    //operation 1,2 or 4,5 or 7,8 or 10,11
                initializeIconButton(clearFlag[i*3+1] && clearFlag[i*3+2],i);
        }
        
        
        if(number == 3 || number == 5 || number == 6 || number == 7)
        {
            int c1[] = {0,0,0,1,0,4,7,10};
            int c2[] = {0,0,0,2,0,5,8,11};
            
            /*1,4,7,10*/
            initializeIconButton(clearFlag[c1[number]],0);
            
            /*2,5,8,11*/
            initializeIconButton(clearFlag[c2[number]],1);
        }
        
        if(number == 4) /*3,6,9,12*/
        {
            for(int i=0;i<4;i++)
                initializeIconButton(clearFlag[(i+1)*3],i);
        }
        
        /**/

        button[buttonNumber[number]-1] = new JButton(menuInJapanese[buttonNumber[number]]);
        button[buttonNumber[number]-1].setBackground(Color.white);
        button[buttonNumber[number]-1].addActionListener(this);
        
        for(int i=0;i<buttonNumber[number];i++)
            labelPanel.add(button[i]);
        
        checkBoxPanel = new JPanel(new BorderLayout());
        checkBoxPanel.setBackground(Color.green);
        checkBoxPanel.add(labelPanel);
        checkBoxPanel.add(checkPanel, BorderLayout.WEST);
        
        scroll[0] = new JScrollPane(checkBoxPanel);
        middlePanel.add(scroll[0]);

        
        if(number == 2 || number == 4)
        {
            if(control.sp1.mode == 0)//Super Light
            {
                button[1].setEnabled(false);
                button[2].setEnabled(false);
                button[3].setEnabled(false);
            }
            
            if(control.sp1.mode == 1)//Light
            {
                button[2].setEnabled(false);
                button[3].setEnabled(false);
            }
            
            if(control.sp1.mode == 2 && number == 2)//Normal
            {
                button[3].setEnabled(false);
            }
        }
    }

    public void setTitleNames()
    {
        if(control.sp1.inJapanese)
            message.setText(menuInJapanese[0]);
        else
            switch(number)
            {
                case 1:
                    message.setText("Character Assist");
                    break;
                case 2:
                case 4:
                    message.setText("Mode Select");
                    break;
                case 3:
                    message.setText("TETDM Operation (Super Light)");
                    break;
                case 5:
                    message.setText("TETDM Operation (Light)");
                    break;
                case 6:
                    message.setText("TETDM Operation (Normal)");
                    break;
                case 7:
                    message.setText("TETDM Operation (Advanced)");
                    break;
            }
    }
    

    
    
    public void componentWindowClose(Component c)
    {
        Window w = SwingUtilities.getWindowAncestor(c);
        w.dispose();
    }
        
    //Action listener
    public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;
        
        if(number == 1) //Select Operation or Mining
        {
            if(e.getSource() == button[0])  //Operation
            {
                StoryControl.PopPanel(control, 2); // -> Story2
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1])  //Mining
            {
                StoryControl.PopPanel(control, 4); // -> Story4
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2] ) //CLOSE
            {
                control.closePopForStory(); //END OF TUTORIAL
                control.characterTutorial = 0;
                control.setCharacterTutorialButton();
            }
        }
        if(number == 2) //Mode Select for Operation
        {
            if(e.getSource() == button[0] )
            {
                
                StoryControl.PopPanel(control, 3); // -> Story3
                StoryControl.SelectMode=1;
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.PopPanel(control, 5); // -> Story5
                StoryControl.SelectMode=4;
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2] )
            {
                StoryControl.PopPanel(control, 6); // -> Story6
                StoryControl.SelectMode=7;
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[3] )
            {
                StoryControl.PopPanel(control, 7); // -> Story7
                StoryControl.SelectMode=10;
                
                componentWindowClose((Component)e.getSource());
            }
            
            if(e.getSource() == button[4])
            {
                control.openPopForStory();  // -> Story1
                componentWindowClose((Component)e.getSource());
            }
        }
        if(number == 3) //Operation for Super Light -> 1, 2
        {
            if(e.getSource() == button[0] )
            {
                StoryControl.SelectMode = 1;
                StoryControl.PopPanelForChapter(1,control);  // -> Chapter1
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.SelectMode = 2;
                StoryControl.PopPanelForChapter(2,control);  // -> Chapter2
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2])
            {
                StoryControl.PopPanel(control, 2);    // -> Story2
                componentWindowClose((Component)e.getSource());
            }
        }
        if(number == 4) //Mode Select for Mining -> 3, 6, 9, 12
        {
            if(e.getSource() == button[0] )
            {
                StoryControl.PopPanelForChapter(3,control);  // -> Chapter3
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.PopPanelForChapter(6,control);  // -> Chapter6
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2] )
            {
                StoryControl.PopPanelForChapter(9,control);  // -> Chapter9
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[3] )
            {
                StoryControl.PopPanelForChapter(12,control);  // -> Chapter12
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[4])
            {
                control.openPopForStory();  // -> Story1
                componentWindowClose((Component)e.getSource());
            }
        }
        if(number == 5) //Operation for Light -> 4, 5
        {
            if(e.getSource() == button[0] )
            {
                StoryControl.SelectMode = 4;
                StoryControl.PopPanelForChapter(4,control);  // -> Chapter4
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.SelectMode = 5;
                StoryControl.PopPanelForChapter(5,control);  // -> Chapter5
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2])
            {
                StoryControl.PopPanel(control, 2);    // -> Story2
                componentWindowClose((Component)e.getSource());
            }
        }
        if(number == 6) //Operation for Normal -> 7, 8
        {
            if(e.getSource() == button[0] )
            {
                StoryControl.SelectMode = 7;
                StoryControl.PopPanelForChapter(7,control);  // -> Chapter7
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.SelectMode = 8;
                StoryControl.PopPanelForChapter(8,control);  // -> Chapter8
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2])
            {
                StoryControl.PopPanel(control, 2);    // -> Story2
                componentWindowClose((Component)e.getSource());
            }
        }
        if(number == 7) //Operation for Advanced -> 10, 11
        {
            if(e.getSource() == button[0] )
            {
                StoryControl.SelectMode = 10;
                StoryControl.PopPanelForChapter(10,control);  // -> Chapter10
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[1] )
            {
                StoryControl.SelectMode = 11;
                StoryControl.PopPanelForChapter(11,control);  // -> Chapter11
                
                componentWindowClose((Component)e.getSource());
            }
            if(e.getSource() == button[2])
            {
                StoryControl.PopPanel(control, 2);    // -> Story2
                componentWindowClose((Component)e.getSource());
            }
        }
    }
}
