//
// Core Program for TETDM 
// MissionPanel.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;
import java.util.Calendar;


public class MissionPanel extends JPanel implements ActionListener
{
	ExpPanel exppanel;
	ExpData expdata;
	DataPanel datapanel;
	
	JPanel allTopPanel;
	JPanel middlePanel;
	JPanel centerPanel;
	
	JPanel buttonPanel;
    
    JToggleButton btxx; //tutorial
    JToggleButton btxxx; //tutorial


	JPanel checkPanel;
	JPanel labelPanel;
	PopUpForKnowledgeCreation knowledge;
	PopUpForInterpretation interpretation;
	

	//Top
	JLabel message;
	JLabel message2;

    //Middle
	JScrollPane scroll[];
	
    JPanel checkBoxPanel1; 		
    JPanel checkBoxPanel2;	
    
	//checkBoxPanel1
    JLabel boxLabel[];
    JLabel mis[];  
   	//JCheckBox setBoxA[];
	ButtonGroup modeGroup;	
	int sizeX;
    ButtonGroup leftGroup;
	
	String posString[] = new String[9];
	String periodString[] = new String[3];
	String sunaString;
	
	 String tutorial1[];
	 String tutorial2[];
	String tutorial3[];
	String tutorial4[];
	int tutorialnumber1[];
	 int totaltutorialnumber1 = 0;
	 int tutorialnumber2[];
	 int totaltutorialnumber2 = 0;
	 int tutorialnumber3[];
	 int totaltutorialnumber3 = 0;
	 int tutorialnumber4[];
	 int totaltutorialnumber4 = 0;
	int noroop=0;
	int miningtoolnumber = 0;
	int visualtoolnumber = 0;
	int miningoperationtoolnumber = 0;
	int visualoperationtoolnumber = 0;
	
	//checkBoxPanel2
   	//JCheckBox setBoxB[];	//currently not used, these are prepared for task execution

	int n1,n2,n3,n4 = 0;
	
	int fontsize = 16;
	int i = 0;
	static int MISSIONNUMBER = 21;
	
	static int missionnumber[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	
	int score = 0;
	
	String menuInJapanese[];
	String TypingScore[];
	
	
	SettingData settingData;
	
	boolean autoSet;
	Control control;
	PopUpForMission popUp;
	
	ImageIcon icon,smallIcon,icon2,smallIcon2;
	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);
	
	MissionPanel(Control control, PopUpForMission 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+ "Mission.txt"));		
		TypingScore = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "module" + File.separator + "VisualizationModules" + File.separator + "TypingColor" + File.separator + "score.txt"));
		
		score = Integer.parseInt(TypingScore[0]);
		
		tutorial1 = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operateBeginner-1_clear.txt"));
		tutorial2 = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operatebeginner-2_clear.txt"));	
		tutorial3 = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operateDocumentPolish_clear.txt"));
		tutorial4 = FILEIO.TextFileAllReadCodeArray(new File(control.absolutePath + "source" + File.separator + "operateForDeveloper" + File.separator+ "developerbeginner_clear.txt"));
		
		
		try{
		  BufferedReader fin = new BufferedReader(new FileReader(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operateBeginner-1_clear.txt"));
       		 String aLine;
      		while(null!=(aLine = fin.readLine())){
         	 n1++;
        	}
       		 fin.close();
     		}
  		catch(Exception e){
			System.out.println("Text Reading ERROR speechtext");
		}
		
		try{
		  BufferedReader fin = new BufferedReader(new FileReader(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operateBeginner-2_clear.txt"));
       		 String aLine;
      		while(null!=(aLine = fin.readLine())){
         	 n2++;
        	}
       		 fin.close();
     		}
  		catch(Exception e){
			System.out.println("Text Reading ERROR speechtext");
		}
		
		try{
		  BufferedReader fin = new BufferedReader(new FileReader(control.absolutePath + "source" + File.separator + "operateForUser" + File.separator+ "operateDocumentPolish_clear.txt"));
       		 String aLine;
      		while(null!=(aLine = fin.readLine())){
         	 n3++;
        	}
       		 fin.close();
     		}
  		catch(Exception e){
			System.out.println("Text Reading ERROR speechtext");
		}
		
		try{
		  BufferedReader fin = new BufferedReader(new FileReader(control.absolutePath + "source" + File.separator + "operateForDeveloper" + File.separator+ "developerbeginner_clear.txt"));
       		 String aLine;
      		while(null!=(aLine = fin.readLine())){
         	 n4++;
        	}
       		 fin.close();
     		}
  		catch(Exception e){
			System.out.println("Text Reading ERROR speechtext");
		}
		
		
		tutorialnumber1 = new int[n1];
		tutorialnumber2 = new int[n2];
		tutorialnumber3 = new int[n3];
		tutorialnumber4 = new int[n4];
		
		
		for(int t=0;t<n1;t++){
		tutorialnumber1[t] = Integer.parseInt(tutorial1[t]);
		totaltutorialnumber1 = totaltutorialnumber1 + tutorialnumber1[t];
		}
		
		for(int t=0;t<n2;t++){
		tutorialnumber2[t] = Integer.parseInt(tutorial2[t]);
		totaltutorialnumber2 = totaltutorialnumber2 + tutorialnumber2[t];
		}
		
		for(int t=0;t<n3;t++){
		tutorialnumber3[t] = Integer.parseInt(tutorial3[t]);
		totaltutorialnumber3 = totaltutorialnumber3 + tutorialnumber3[t];
		}
		
		for(int t=0;t<n4;t++){
		tutorialnumber4[t] = Integer.parseInt(tutorial4[t]);
		totaltutorialnumber4 = totaltutorialnumber4 + tutorialnumber4[t];
		}
		
		
		//ALL
		allTopPanel = new JPanel();
        	allTopPanel.setBackground(Color.red);
		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(new GridLayout(MISSIONNUMBER,1));
        	centerPanel.setBackground(new Color(186,183,224));
		add(centerPanel);

	
        
        //MIDDLE-INSIDE
		
		checkPanel = new JPanel(new GridLayout(5,1));
		labelPanel = new JPanel(new GridLayout(4,1));
		checkPanel.setBackground(new Color(0x7f,0xff,0xd4));
		checkPanel.setBackground(Color.blue);
		labelPanel.setBackground(new Color(0x7f,0xff,0xd4));
		labelPanel.setBackground(new Color(121,132,22));
		

		createCenterPanel();
		//initializeSetBox();		



		//BOTTOM
		buttonPanel = new JPanel(new GridLayout(1,2));
		add(buttonPanel, BorderLayout.SOUTH);	
		buttonPanel.setBackground(Color.green);
        buttonPanel.setOpaque(true);
        
        btxx = new JToggleButton();//SUPPORT
        btxx.setFont(new Font(settingData.fontName, Font.PLAIN, settingData.fontSize));//user
        btxx.addActionListener(this);
        
        btxxx = new JToggleButton();//SUPPORT
        btxxx.setFont(new Font(settingData.fontName, Font.PLAIN, settingData.fontSize));//developer
        btxxx.addActionListener(this);

        if(settingData.inJapanese)
        {
            btxx.setText(menuInJapanese[28]);    //SUPPORT
            btxxx.setText(menuInJapanese[29]);    //SUPPORT
        }
        else
        {
            btxx.setText("MISSION FOR USER");    //SUPPORT
            btxxx.setText("MISSION FOR DEVELOPER");    //SUPPORT
        }

        buttonPanel.add(btxx);
        buttonPanel.add(btxxx);
        
        if(settingData.mode == 0 || settingData.mode == 1)
        {
            btxx.setEnabled(false);
            btxxx.setEnabled(false);
        }
        else if(settingData.mode == 2)
        {
            btxxx.setEnabled(false);
        }

	}
	
	public void createCenterPanel(){
	mis = new JLabel[MISSIONNUMBER];
    	 for(int i=0;i<MISSIONNUMBER;i++)
        {
            mis[i] = new JLabel();
            mis[i].setFont(new Font(settingData.fontName, Font.BOLD, fontsize+6));
        }
	
	
	
	for(int i=0;i<MISSIONNUMBER;i++){
	centerPanel.add(mis[i]);
	}  
	
	setTitleNames3();
	}
	
	public void setTitleNames()
	{
		if(settingData.inJapanese){
			
			message.setText(menuInJapanese[0]);
			}
		else
			message.setText("MISSION");
	}
	
	public void Missionwrite(int number){
	try
	{
	//File file = new File(control.absolutePath +  "source" + File.separator + "GameMode" + File.separator+ "Mission.txt");
	File file = new File(control.absolutePath + "LogData.txt");
	FileWriter filewriter = new FileWriter(file, true);
	filewriter.write("mission" + number + ":" + year + month + day + ":" + hour + minute + second  + "  /total" + ExpData.totaltimeint  + "\n");
	filewriter.close();
	}catch(IOException ae){
     	System.out.println(ae);
 	}
	}
	
	
	public void setTitleNames3()
	{
	
		if(settingData.inJapanese){
			
			mis[0].setText(menuInJapanese[1] + menuInJapanese[21] + (100 -control.ex1.rank));
			if(control.ex1.rank >= 100){ 
			mis[0].setText(menuInJapanese[1]);
			mis[0].setForeground(Color.red);
			
			if(missionnumber[0] == 0){
			control.ex1.addCoin(5000);
			missionnumber[0] = 1;
			Missionwrite(1);
			control.ex1.save();
			}
			
			
			
			}
	
			mis[1].setText(menuInJapanese[2] + menuInJapanese[21] + (500 -control.ex1.rank));
			if(control.ex1.rank >= 500){ 
			mis[1].setText(menuInJapanese[2]);
			mis[1].setForeground(Color.red);
			if(missionnumber[1] == 0){
			control.ex1.addCoin(10000);
			missionnumber[1] = 1;
			Missionwrite(2);
			control.ex1.save();
			}
			}
			
			
			mis[2].setText(menuInJapanese[3]);
			if(totaltutorialnumber1 == n1 && totaltutorialnumber2 == n2){
		
			mis[2].setForeground(Color.red);
			if(missionnumber[2] == 0){
			control.ex1.addCoin(5000);
			missionnumber[2] = 1;
			Missionwrite(3);
			}
			}
			
			mis[3].setText(menuInJapanese[4]);
			if(totaltutorialnumber3 == n3){
		
			mis[3].setForeground(Color.red);
			if(missionnumber[3] == 0){
			control.ex1.addCoin(10000);
			missionnumber[3] = 1;
			Missionwrite(4);
			}
			}
			
			
			
			mis[4].setText(menuInJapanese[5] + menuInJapanese[21] + (5-expdata.knowledgenumber) + menuInJapanese[23]);
			if(expdata.knowledgenumber>=5){
			mis[4].setText(menuInJapanese[5]);
			mis[4].setForeground(Color.red);
			if(missionnumber[4] == 0){
			control.ex1.addCoin(5000);
			control.ex1.addExp(50);
			missionnumber[4] = 1;
			Missionwrite(5);
			}
			}
			
			mis[5].setText(menuInJapanese[6] + menuInJapanese[21] + (10 - expdata.knowledgenumber) + menuInJapanese[23]);
			if(expdata.knowledgenumber>=10){
			mis[5].setText(menuInJapanese[6]);
			mis[5].setForeground(Color.red);
			if(missionnumber[5] == 0){
			control.ex1.addCoin(10000);
			control.ex1.addExp(100);
			missionnumber[5] = 1;
			Missionwrite(6);
			}
			}
			
			mis[6].setText(menuInJapanese[7] + menuInJapanese[21] + (50 - expdata.startingnumber) + menuInJapanese[23]);
			if(expdata.startingnumber >= 50){
			mis[6].setText(menuInJapanese[7]);
			mis[6].setForeground(Color.red);
			if(missionnumber[6] == 0){
			control.ex1.addCoin(5000);
			missionnumber[6] = 1;
			Missionwrite(7);
			}
			}
			
			
			mis[7].setText(menuInJapanese[8] + menuInJapanese[21] + (10 - expdata.loginnumber) + menuInJapanese[24]);
			if(expdata.loginnumber >= 10){
			mis[7].setText(menuInJapanese[8]);
			mis[7].setForeground(Color.red);
			if(missionnumber[7] == 0){
			control.ex1.addCoin(5000);
			missionnumber[7] = 1;
			Missionwrite(8);
			}
			}
			
			if(missionnumber[8] == 0){
			mis[8].setText(menuInJapanese[9] + menuInJapanese[21] + (5 - expdata.renzokuloginnumber) + menuInJapanese[24]);
			}
			
			if(expdata.renzokuloginnumber >= 5 || missionnumber[8] == 1){
			mis[8].setText(menuInJapanese[9]);
			mis[8].setForeground(Color.red);
			if(missionnumber[8] == 0){
			control.ex1.addCoin(5000);
			missionnumber[8] = 1;
			Missionwrite(9);
			}
			}
			
			mis[9].setText(menuInJapanese[10] + menuInJapanese[21] + (36000 - expdata.totaltimeint) + menuInJapanese[22]);
			if(expdata.totaltimeint >= 36000){
			mis[9].setText(menuInJapanese[10]);
			mis[9].setForeground(Color.red);
			if(missionnumber[9] == 0){
			control.ex1.addCoin(5000);
			missionnumber[9] = 1;
			Missionwrite(10);
			
			}
			}
			mis[10].setText(menuInJapanese[11] + menuInJapanese[21] + (50 - expdata.inputnumber) + menuInJapanese[23]);
			if(expdata.inputnumber >= 50){
			mis[10].setText(menuInJapanese[11]);
			mis[10].setForeground(Color.red);
			if(missionnumber[10] == 0){
			control.ex1.addCoin(5000);
			control.ex1.addExp(50);
			missionnumber[10] = 1;
			Missionwrite(11);
			}
			}
			mis[11].setText(menuInJapanese[12] + menuInJapanese[21] + (100 - expdata.inputnumber) + menuInJapanese[23]);
			if(expdata.inputnumber >= 100){
			mis[11].setText(menuInJapanese[12]);
			mis[11].setForeground(Color.red);
			if(missionnumber[11] == 0){
			control.ex1.addCoin(10000);
			control.ex1.addExp(100);
			missionnumber[11] = 1;
			Missionwrite(12);
			
			}
			}
			
			mis[12].setText(menuInJapanese[13]);
			if(score >= 200){
			mis[12].setText(menuInJapanese[13]);
			mis[12].setForeground(Color.red);
			if(missionnumber[12] == 0){
			control.ex1.addCoin(100);
			missionnumber[12] = 1;
			Missionwrite(13);
			}
			}
			
			
			for(int i = 0;i<expdata.miningModuleNumber;i++){
			if(expdata.miningModuleSelectionCount[i] >= 1) miningtoolnumber++;
			}
			
			for(int j = 0;j<expdata.visualizationModuleNumber;j++){
			if(expdata.visualizationModuleSelectionCount[j]>= 1) visualtoolnumber++;
			}
			
			
			mis[13].setText(menuInJapanese[14] + menuInJapanese[21] + (20 - miningtoolnumber) + menuInJapanese[25]);
			if(miningtoolnumber >= 20){
			mis[13].setText(menuInJapanese[14]);
			mis[13].setForeground(Color.red);
			if(missionnumber[13] == 0){
			control.ex1.addCoin(1000);
			control.ex1.addExp(50);
			missionnumber[13] = 1;
			Missionwrite(14);
			}
			}
			
			mis[14].setText(menuInJapanese[15]  + menuInJapanese[21] + (20 - visualtoolnumber) + menuInJapanese[25]);
			if(visualtoolnumber >= 20){
			mis[14].setText(menuInJapanese[15]);
			mis[14].setForeground(Color.red);
			if(missionnumber[14] == 0){
			control.ex1.addCoin(1000);
			control.ex1.addExp(50);
			missionnumber[14] = 1;
			Missionwrite(15);
			}
			}
			
			
			ExpData.miningmodule5 = 0;
			for(int i=0;i<ExpData.miningModuleNumber;i++){
			ExpData.miningmodule5 += ExpData.miningModuleSelectionCount[i];
			}
			
			ExpData.visualmodule5 = 0;
			for(int i=0;i<ExpData.visualizationModuleNumber;i++){
			ExpData.visualmodule5 += ExpData.visualizationModuleSelectionCount[i];
			}
	
			
	
			mis[15].setText(menuInJapanese[16]  + menuInJapanese[21] + (1000 - ExpData.miningmodule5) + menuInJapanese[23]);
			if(ExpData.miningmodule5 >= 1000){
			mis[15].setText(menuInJapanese[16]);
			mis[15].setForeground(Color.red);
			if(missionnumber[15] == 0){
			control.ex1.addCoin(5000);
			control.ex1.addExp(100);
			missionnumber[15] = 1;
			Missionwrite(16);
			}
			}
			
			for(int i = 0;i<expdata.miningModuleNumber;i++){
			if(expdata.miningModuleOperationCount[i] >= 1) miningoperationtoolnumber++;
			}
			
			for(int j = 0;j<expdata.visualizationModuleNumber;j++){
			if(expdata.visualizationModuleOperationCount[j]>= 1) visualoperationtoolnumber++;
			}
			
			mis[16].setText(menuInJapanese[17] + menuInJapanese[21] + (10 - miningoperationtoolnumber) + menuInJapanese[25]);
			if(miningoperationtoolnumber >= 10){
			mis[16].setText(menuInJapanese[17]);
			mis[16].setForeground(Color.red);
			if(missionnumber[16] == 0){
			control.ex1.addCoin(1000);
			control.ex1.addExp(100);
			missionnumber[16] = 1;
			Missionwrite(17);
			}
			}
			
			ExpData.miningmoduleoperation5 = 0;
			for(int i=0;i<ExpData.miningModuleNumber;i++){
			ExpData.miningmoduleoperation5 += ExpData.miningModuleOperationCount[i];
			}
	
			ExpData.visualmoduleoperation5 = 0;
			for(int i=0;i<ExpData.visualizationModuleNumber;i++){
			ExpData.visualmoduleoperation5 += ExpData.visualizationModuleOperationCount[i];
			}
			
			mis[17].setText(menuInJapanese[18] + menuInJapanese[21] + (500 - expdata.miningmoduleoperation5 + expdata.visualmoduleoperation5) + menuInJapanese[23]);
			if(expdata.miningmoduleoperation5 + expdata.visualmoduleoperation5 >= 500){
			mis[17].setText(menuInJapanese[18]);
			mis[17].setForeground(Color.red);
			if(missionnumber[17] == 0){
			control.ex1.addCoin(5000);
			control.ex1.addExp(100);
			missionnumber[17] = 1;
			Missionwrite(18);
			}
			}
			
			mis[18].setText(menuInJapanese[19] + menuInJapanese[21] + (100 - TitlePanel.skillrank));
			if(TitlePanel.skillrank >= 100){
			mis[18].setText(menuInJapanese[19]);
			mis[18].setForeground(Color.red);
			if(missionnumber[18] == 0){
			control.ex1.addCoin(5000);
			missionnumber[18] = 1;
			Missionwrite(19);
			}
			}
			
			mis[19].setText(menuInJapanese[20] + menuInJapanese[21] + (500 - TitlePanel.skillrank));
			if(TitlePanel.skillrank >= 500){
			mis[19].setText(menuInJapanese[20]);
			mis[19].setForeground(Color.red);
			if(missionnumber[19] == 0){
			control.ex1.addCoin(10000);
			missionnumber[19] = 1;
			Missionwrite(20);
			}
			}
			
			mis[20].setText(menuInJapanese[27] + menuInJapanese[21] + (100 - ExpData.collectbutton) + menuInJapanese[23]);
			if(ExpData.collectbutton>= 100){
			mis[20].setText(menuInJapanese[27]);
			mis[20].setForeground(Color.red);
			if(missionnumber[20] == 0){
			control.ex1.addCoin(5000);
			control.ex1.addExp(100);
			missionnumber[20] = 1;
			Missionwrite(21);
			}
			}
			
		}
	}



	//Action listener
	public void actionPerformed(ActionEvent e)
    {
        if(autoSet == true)
            return;
        
        if(e.getSource() == btxx)//SUPPORT
        {
            control.btxx.doClick();
        }
        
        if(e.getSource() == btxxx)//SUPPORT
        {
            control.btxxx.doClick();
        }
	}
	
	public void closeWindow()
	{		
	exppanel.mission.setSelected(false);
        //control.closePopForTitle();
	exppanel.closePopForShop();
	}
	
}

