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

package source;

//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.border.LineBorder;
import javax.swing.GroupLayout.Alignment;

public class MakeSupportTaskData
{		
	int list_num;	
	int task_num=0,thema_num=0;
	String input_task[],input_String[],input_thema[],input_clear[];
	String file_add = "";
	
    String questionDir;
    String questionFilename;
    
	Control control;

	Color base_color = new Color(0x00,0xff,0xff);
	Color click_color = new Color(0x41,0x69,0xE1);
	Color clear_color = Color.pink;
	Color thema_color = new Color(0xf0,0xff,0xff);
	
	class Items
	{
		int x,y,width,hight;
//		Color color,frame_color;
		
		int posx,posy;
		String goal;
		int no,num;
		int id;
		int level;
		int exp;
		boolean clear;
		
		int click_num;
		Color color;
		
		int open_condition[];   ///[task_num]task_number,-1
		int open;//can click_task_number
		boolean canopen;
		
		int max_page,max_fig,max_que;
		int fig_num;
		int page;
		boolean read;
		
		int page_fig [];	
		String fig_name [];
		
		String p_str[][];
		int line_max;
		String que_ans[][];
		Image image;
		
		String pictype;
		
		String result_txt;
		int result_len;
		String meaning_txt;
		int meaning_len;
		int importance;
		String Assigntext_name;
		String AssignMiningname;
		String AssignVisualizationname;
	}
	
	Items theme[],task[];
	
	void set_item_data(int judge)
	{
		Color color[] = new Color[4];
		color[0] = new Color(0xff,0xfa,0xcd);
		color[1] = new Color(0xff,0xf0,0xf5);
		color[2] = new Color(0xf0,0xff,0xf0);
		color[3] = new Color(0xf0,0xf8,0xff);
		
		String str="";
//		int count[] = new int [input_thema.length];	
//		for(int i=0;i<input_thema.length;i++) count[i] = 0;
		
		if(judge == 0){		
			for(int i=0;i<input_task.length;i++)
			{
				str = input_task[i];
				String [] str2 = str.split(",",0);
				task[i].color = base_color;			
				task[i].no = Integer.parseInt(str2[0]);
				task[i].goal = str2[1];
				task[i].exp = Integer.parseInt(str2[2]);
				task[i].level = Integer.parseInt(str2[3]);
				for(int j=0;j<input_thema.length;j++){
					if(theme[j].level==task[i].level){
						theme[j].num += 1;
						task[i].id = theme[j].num;
						break;
					}
				}
				
				if(input_task.length == input_clear.length){
//					if(Integer.parseInt(str2[4]) == 1){
					if(input_clear[i].equals("1")){
						task[i].clear = true;
						task[i].color = clear_color;
					}	
					else task[i].clear = false;	
				}else{
					task[i].clear = false;
				}
				
				task[i].open = Integer.parseInt(str2[5]);
				task[i].click_num = 0;
				task[i].posy = 0;
				task[i].read = false;
				task[i].canopen = false;
				task[i].pictype = str2[6].trim();
			}
		}else {
			for(int i=0;i<input_thema.length;i++)
			{

				str = input_thema[i];
				String [] str2 = str.split(",",0);
									
				theme[i].goal = str2[2];
				theme[i].num = 0;
				theme[i].level = Integer.parseInt(str2[1]);
				theme[i].exp = 0;
				
				theme[i].color = color[theme[i].level % 4];
				
				theme[i].click_num = 0;
				theme[i].posy = 0;
			}	
		}			
	}
	
	Support support;
	int classstage_num = 0;
	
	MakeSupportTaskData(Control control, Support support, int classstage_num, int version_num, String questionDir, String questionFilename)
	{
		this.control = control;
		this.support = support;
		this.classstage_num = classstage_num;
        
        this.questionDir = questionDir;
        this.questionFilename = questionFilename;
		
		file_add = control.absolutePath + "source" 
				+ File.separator + questionDir + File.separator;
		
		
		init_datas(classstage_num);
		
		task = new Items[task_num];
		theme = new Items[thema_num];
		
		for(int i=0;i<task_num;i++)
			task[i] = new Items();
		
		for(int i=0;i<thema_num;i++)
			theme[i] = new Items();
		
		set_item_data(1);
		set_item_data(0);
	}
	
	void init_datas(int classstage_num)
	{
		input_String = FILEIO.TextFileAllReadCodeArray
				(new File(file_add + questionFilename + support.classstage[classstage_num] + ".txt"));
		
		for(int i=0;i<input_String.length;i++){
			if(input_String[i].indexOf("Mission")!=-1)thema_num += 1;
			else task_num += 1;
		}
		
		int k = 0,l = 0;
		
		input_task = new String[task_num];
		input_thema = new String[thema_num];
		
		for(int i=0;i<input_String.length;i++){
			if(input_String[i].indexOf("Mission")==-1){
				input_task[k] = input_String[i];
				k++;
			}else { 
				input_thema[l] = input_String[i];
				l++;
			}
		}
		input_clear = FILEIO.TextFileAllReadCodeArray
				(new File(file_add + questionFilename + support.classstage[classstage_num] + "_clear.txt"));
	}
	
	void read_task_details(int num)
	{
		if(task[num].read == true)return;
		task[num].read = true;
		input_String = FILEIO.TextFileAllReadCodeArray
				(new File(file_add + support.classstage[support.classstage_num] + File.separator + task[num].no + ".txt"));
		check_text(num);
	}

	void check_text(int num){
		String p_f = "";
		String fign = "";
		String que = "";
		String ans = "";
		int f_num = 0;
		int j=0;
		
		task[num].p_str = new String[input_String.length][2];
		
		for(int i=0;i<input_String.length;i++){		
			if(input_String[i].indexOf("page")!=-1){
				task[num].max_page += 1;
				p_f += Integer.toString(f_num) + ",";
				f_num = 0;
				continue;
			}
			if(input_String[i].indexOf("fig,")!=-1){
				String str = input_String[i];
				task[num].max_fig += 1;
				fign += str.substring(str.indexOf(",")+1) + ",";
				f_num += 1;
				continue;
			}
			if(input_String[i].indexOf("question,")!=-1){
				String str = input_String[i];
				task[num].max_que += 1;
				que += str.substring(str.indexOf(",")+1) + ",";
				continue;
			}
			if(input_String[i].indexOf("answer,")!=-1){
				String str = input_String[i];
				ans += str.substring(str.indexOf(",")+1) + ",";
				continue;
			}
			if(input_String[i].indexOf("RMI,")!=-1){
				String str = input_String[i];
				String str2 = str.substring(str.indexOf(",")+1);
		
				
				String result_txt = "";
				String	result_len = "";
				String	meaning_txt = "";
				String	meaning_len = "";
				String	importance = "";
				
				task[num].meaning_txt = "";
				task[num].meaning_txt = "";
	
				String str3 = str2.substring(0,str2.indexOf("/"));
				
				String str4 = str2.substring(str2.indexOf("/")+1);
				String str5 = str4.substring(0,str4.indexOf("/"));
				String str6 = str4.substring(str4.indexOf("/")+1);
		
				result_txt = str3.substring(0,str3.indexOf(":"));
				result_len = str3.substring(str3.indexOf(":")+1);
				meaning_txt = str5.substring(0,str5.indexOf(":"));
				meaning_len = str5.substring(str5.indexOf(":")+1);
				importance = str6;
				
				task[num].result_txt = str3.substring(0,str3.indexOf(":"));

				if(result_len.equals(""))task[num].result_len = 0;
				else task[num].result_len = Integer.parseInt(result_len);
				
				task[num].meaning_txt = str5.substring(0,str5.indexOf(":"));
								
				if(meaning_len.equals(""))task[num].meaning_len = 0;
				else task[num].meaning_len = Integer.parseInt(meaning_len);
				
				if(importance.equals(""))task[num].importance = 0;
				else task[num].importance = Integer.parseInt(importance);
				
		/////		System.out.println("1:"+result_txt+",2:"+result_len+",3:"+meaning_txt+",4:"+meaning_len+",5:"+importance);
				continue;
			}
			if(input_String[i].indexOf("AssignTool,")!=-1){
				String str = input_String[i];
				String str2 = str.substring(str.indexOf(",")+1);
		
				task[num].AssignMiningname = "";
				task[num].AssignVisualizationname = "";
				
				task[num].AssignMiningname = str2.substring(0,str2.indexOf("/"));
				task[num].AssignVisualizationname = str2.substring(str2.indexOf("/")+1);
				
		////		System.out.println("1:"+task[num].AssignMiningname+",2:"+task[num].AssignVisualizationname);
				continue;
			}
			else{
	//			System.out.println(input_String[i]);
				task[num].p_str[j][0] = Integer.toString(task[num].max_page);
				task[num].p_str[j][1] = input_String[i];
				j++;
			}
		}
		task[num].line_max = j;
		
		p_f += Integer.toString(f_num) + ",";
		
		task[num].page_fig = new int [task[num].max_page+1];  //page_num/fig_num in page
		task[num].fig_name = new String[task[num].max_fig]; //page_num/fig_name
		task[num].que_ans = new String [task[num].max_que+1][3];  //
		
		String [] p_f2 = p_f.split(",",0);
		for(int i=0;i<task[num].max_page+1;i++){
			task[num].page_fig[i] = Integer.parseInt(p_f2[i]);
		}
		String [] fign2 = fign.split(",",0);
		for(int i=0;i<task[num].max_fig;i++){
			task[num].fig_name[i] = fign2[i];
		}

		String [] que2 = que.split(",",0);
		String [] ans2 = ans.split(",",0);
		for(int i=0;i<task[num].max_que;i++){
			task[num].que_ans[i][0] = que2[i];
			task[num].que_ans[i][1] = ans2[i];
			task[num].que_ans[i][2] = "F";
		}
	}
	
	void initializeClearList(int classstage_num){
		try{
			//			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file_add + "operate" + support.classstage[classstage_num] + ".txt")));
			PrintWriter pw = new PrintWriter(
					new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_add + questionFilename + support.classstage[classstage_num] + "_clear.txt"),"Shift_JIS")));
			
 			for(int i=0;i<input_task.length;i++)
			{
				pw.println("0");
				System.out.println("0");
			}
			pw.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}	
	
	void preserve_clear_list(int classstage_num){
		try{
//			PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file_add + "operate" + support.classstage[classstage_num] + ".txt")));
			PrintWriter pw = new PrintWriter(
					new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_add + questionFilename + support.classstage[classstage_num] + "_clear.txt"),"Shift_JIS")));
 
 			for(int i=0;i<input_task.length;i++)
			{
				if(task[i].clear == true)pw.println("1");
				else pw.println("0");
			}
			pw.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}
	/*
	//save last challenged
	void preserve_try_stage(int classstage_num){
		try{
			PrintWriter pw = new PrintWriter(
					new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file_add + support.ver + "_trystage.txt"),"Shift_JIS")));
 
			pw.print(classstage_num);
			for(int i=0;i<support.classstage.length;i++){
				pw.print(","+support.originalClassstage[i]);
			}
			pw.println();
			pw.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}
	*/
	void sendtimeData(int classstage_num,String text){
		String sendtext = "";
		Calendar now = Calendar.getInstance();
		DecimalFormat df = new DecimalFormat("00");
    
		int y = now.get(Calendar.YEAR);
		int mo = now.get(Calendar.MONTH) + 1;
		int d = now.get(Calendar.DATE);
		int h = now.get(now.HOUR_OF_DAY);
		int m = now.get(now.MINUTE);
		int s = now.get(now.SECOND);
		System.out.print(df.format(y)+"/"+df.format(mo)+"/"+df.format(d)+" "+df.format(h)+":"+df.format(m)+":"+df.format(s)+" ");
		System.out.print("["+support.ver+":"+support.classstage[classstage_num]+"]  ");
		System.out.println(text);
		
		sendtext += df.format(y)+"/"+df.format(mo)+"/"+df.format(d)+" "+df.format(h)+":"+df.format(m)+":"+df.format(s)+" ";
		sendtext += "["+support.ver+":"+support.classstage[classstage_num]+"]  ";
		sendtext += text;
		
		ACTIONLOG.writeActionLog("SUPPORT*"+sendtext);
	}
}
