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

package source;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
import source.TextData.*;
import source.Utility.*;

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 PopUpForSupport extends JFrame
{
	Container pane = getContentPane();
	Control control;
	MakeSupportTaskData data;
	int flameSize = 30;
	int fontSize = flameSize - 15;
	JTextArea texttitle;
	JPanel theme_panel,button_panel,basepane;
	
	///////////popup_picture/////////////
	Pic_Panel pic_panel;
	String file_add = "";

	///////////popup_task_detail/////////////
	JButton button[];
	JTextArea area;
	int page = 0;
	int X,Y,width,height,task_num;
	PopUpForSupport pop_detail, pop_answer;
	PopUpForSupport p;
        PopUpForSupport popForSupport3[];
	JPanel process_panel;
	
	///////////popup_task_answer/////////////
	JLabel label[] ;
	JTextField textanswer[];
	JTextArea textquestion[];
	JPanel question_panel;
		
	void init_pane(int X, int Y, int width, int height, Control control){
		pane.setLayout(new BorderLayout());
		
		setBounds(X, Y, width, height);
//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
//		setAlwaysOnTop(true);
		pane.setBackground(Color.white);
		
		
        this.control = control;
	}
	
	void set_text_title(int task_num){
		texttitle = new JTextArea ();

		texttitle.append(data.task[task_num].goal);
		texttitle.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize+5));
		texttitle.setLineWrap(true);
		texttitle.setEnabled(false);
		texttitle.setBackground(new Color(0xad, 0xff, 0x2f));
		texttitle.setDisabledTextColor(Color.black);
		theme_panel.add(texttitle);
	}
	
	Support support;
	
	///////////popup_task_list/////////////
	PopUpForSupport(int X, int Y, int width, int height, Control control, String ver, String name, String windowTitle)
	{		
		super(windowTitle);
		init_pane( X, Y, width/2, height, control);
//		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		this.addWindowListener(new MyWindowListener());		
		
		support = new Support(control,ver,width/2,height,name);
		pane.add(support);/*FOR TEST*/
	//	addWindowListener(this);
		setVisible(true);
	}
		
	///////////popup_picture/////////////	
	PopUpForSupport(int X, int Y, int width, int height, Control control, MakeSupportTaskData data,String pic_name)
	{	
		super("IMAGE");
		init_pane( X, Y, width, height/2, control);
		this.data = data;
		this.file_add = data.file_add + pic_name;
		
		pic_panel = new Pic_Panel(file_add);
		pane.add(pic_panel);
		setVisible(true);
	}
	
	Support supporttaskdetail;
	///////////popup_task_detail/////////////
	PopUpForSupport(int X, int Y, int width, int height, Control control, int task_num, MakeSupportTaskData data, Support support)
	{	
		super("MISSION");
		this.task_num = task_num;
		this.data = data;		
		this.width = width;
		this.Y = Y;
		this.X = X;
		this.height = height;
		supporttaskdetail = support;
		
		init_pane( 0, Y, width, height/2, control);/////////////
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);	//For Closing task-answer and figure windows by pushing CLOSE button
		
       	data.read_task_details(task_num);
		view_task_detail_panel();
		setVisible(true);
		pop_detail = this;

		if(data.task[task_num].max_que > 0){
			p = 
            new PopUpForSupport(0, Y, width, height,control, task_num,data,1, supporttaskdetail, this);
		}
	}
	
	class MyWindowListener extends WindowAdapter {
		public void windowClosing(WindowEvent e) 
		{
			//System.out.println("windowClosing");
			
			if(control.btxx.isSelected() == true)
			{
				control.btxx.setSelected(false);
				control.popForSupport.dispose();
				control.popForSupport = null;
			}
			if(control.btxxx.isSelected() == true)
			{
				control.btxxx.setSelected(false);
				control.popForSupportDeveloper.dispose();
				control.popForSupportDeveloper = null;
			}
		}
	}	
	
	public void initializeClearData()
	{
		System.out.println("CLEAR TUTORIAL DATA");
		
		for(int i=0;i<support.classstage.length;i++)
			support.data.initializeClearList(i);	
	}
	
    public void closeAllWindows()
    {
        support.closeAllWindows();        
        
        if(p != null)
        {
            p.dispose();
            p = null;
        }
        if(pop_detail != null)
        {
            pop_detail.closeFigureWindow();
            pop_detail.dispose();
            pop_detail = null;
        }
        if(pop_answer != null)
        {
            pop_answer.dispose();
            pop_answer = null;
        }
        
    }
    
	void view_task_detail_panel()
    {
		basepane = new JPanel();
		basepane.setLayout(new BorderLayout());
        pane.add(basepane);
		
		theme_panel = new JPanel();

		theme_panel.setLayout(new BoxLayout(theme_panel,BoxLayout.Y_AXIS));  ////////////////////////////
		basepane.add("North",theme_panel);
		
		set_text_title(task_num);
		
		process_panel = new JPanel();
		process_panel.setLayout(new BorderLayout());
		basepane.add("Center",process_panel);
		
		button_panel = new JPanel();
		button_panel.setLayout(new GridLayout(2,2));
		basepane.add("South",button_panel);
		
		String button_name[] = {"back <","next >","figure","close"};

        if(control.sp1.inJapanese)
        {
            button_name[0] = supporttaskdetail.japaneseWords[4];
            button_name[1] = supporttaskdetail.japaneseWords[5];
            button_name[2] = supporttaskdetail.japaneseWords[6];
            button_name[3] = supporttaskdetail.japaneseWords[7];
        }
		
		button = new JButton [button_name.length];
	    for(int i=0;i<button_name.length;i++){
	    	button[i] = new JButton(button_name[i]);
	    	button[i].addActionListener(new MyBtn());
	    	button_panel.add(button[i]);
	    }

	    button[0].setEnabled(false);
	    if(data.task[task_num].max_page == 0)button[1].setEnabled(false);
	    
		area = new JTextArea("");
		area.setLineWrap(true);
		area.setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
		set_text();
		
		JScrollPane scroll_panel;
		scroll_panel = new JScrollPane(area);
		process_panel.add(scroll_panel);
	}

	void set_text()
    {
		area.setText("");

		for(int i=0;i<data.task[task_num].line_max;i++)
        {
			if(page != Integer.parseInt(data.task[task_num].p_str[i][0]))continue;
			if(data.task[task_num].p_str[i][1].equals("end,")){
				data.task[task_num].clear = true;
//                writeActionLog("Question "+(task_num+1)+": clear");
                data.sendtimeData(supporttaskdetail.classstage_num,"Q "+(task_num+1)+" :"+data.task[task_num].goal+"clear");/////

			int lastTotalExp = supporttaskdetail.total_exp;				
				
                supporttaskdetail.check_exp();////////////////////////////////////////EXP///
				
				System.out.println("GET EXP:"+(supporttaskdetail.total_exp - lastTotalExp) );					
				control.ex1.addExp(supporttaskdetail.total_exp - lastTotalExp);
				control.ex1.addExptutorial(supporttaskdetail.total_exp - lastTotalExp);				
				
				if(control.expPanel != null)				
					control.expPanel.repaint();				
				
				break;
			}
			area.append(data.task[task_num].p_str[i][1]);
			area.append(System.getProperty("line.separator"));
		}
        
		if(data.task[task_num].page_fig[page] == 0)
            button[2].setEnabled(false);
		else
            button[2].setEnabled(true);
        
        area.setCaretPosition(0);
	}
    
	Point position;
    
    void closeFigureWindow()
    {
        if(popForSupport3 != null)
        {
            for(int j=0;j<data.task[task_num].page_fig[page];j++) //close old one
                if(popForSupport3[j] != null)
                {
                    popForSupport3[j].dispose();
                    popForSupport3[j] = null;
                }
            popForSupport3 = null;
        }
    }
	
	class MyBtn implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{		
			if(e.getSource() == button[0])//back		
			{
                closeFigureWindow();
                
				page -= 1;
				if(page <= 0){
					page = 0;
					button[0].setEnabled(false);
				}
				if(page != data.task[task_num].max_page)button[1].setEnabled(true);
				set_text();
			}		
			if(e.getSource() == button[1])//next		
			{
                closeFigureWindow();
                
				page += 1;
				if(page >= data.task[task_num].max_page){
					page = data.task[task_num].max_page;
					button[1].setEnabled(false);
				}
				if(page != 0)button[0].setEnabled(true);
				set_text();
			}		
			if(e.getSource() == button[2])//figure		
			{
                closeFigureWindow();
                
                
				popForSupport3 = new PopUpForSupport[data.task[task_num].page_fig[page]];
				position = getLocationOnScreen();
				
				int figid = 0;
				
				for(int k=0;k<page;k++){
					figid += data.task[task_num].page_fig[k];
				}
				

                        
                
				for(int j=0;j<data.task[task_num].page_fig[page];j++){ // among figure in page
					figid += j;
					popForSupport3[j] 
							= new PopUpForSupport(200+60*j, (int)position.getY()+60+60*j, width, height, control,data,data.task[task_num].fig_name[figid]);
				}
			}		
			if(e.getSource() == button[3])//close	
			{
				if(p != null)
				{
					p.dispose();
					p = null;
				}
				if(data.task[task_num].click_num %2 == 1)data.task[task_num].click_num += 1;
				{
                    if(pop_detail != null)
                    {
                        pop_detail.dispose();
                        pop_detail = null;
                    }
				}
                closeFigureWindow();
                
			}		
			repaint();
		}	
	}
	
	Support supporttaskanswer;
	///////////popup_task_answer/////////////
	
    PopUpForSupport detail_position;
    
	PopUpForSupport(int X, int Y, int width, int height, Control control, int task_num,MakeSupportTaskData data,int ans_num, Support support, PopUpForSupport detail)
	{	
		super("Q & A");
		this.task_num = task_num;
		this.data = data;		
		supporttaskanswer = support;
        detail_position = detail;
		
		init_pane( X, Y+height/2, width, calculate_size(width), control); /////////////
       	data.read_task_details(task_num);
		view_task_answer_panel();
		pop_answer = this;
		setVisible(true);
		getContentPane().setPreferredSize(new Dimension(width, textanswer[0].getSize().height*calculate_size(width)));	/////////////
		pack();	/////////////
	}
	
	int calculate_size(int width){/////////////
		int line_num = 0;

		    for(int i=0;i<data.task[task_num].max_que;i++)
		    	line_num += data.task[task_num].que_ans[i][0].length()/(width/fontSize);

		    return (line_num+6);
	} 
	
	void view_task_answer_panel(){
		basepane = new JPanel();
		basepane.setLayout(new BorderLayout());
		pane.add(basepane);
		
		theme_panel = new JPanel();
		theme_panel.setLayout(new BoxLayout(theme_panel,BoxLayout.Y_AXIS));  ////////////////////////////
		theme_panel.setBackground(new Color(0xad, 0xff, 0x2f));
		basepane.add("North",theme_panel);
		
		set_text_title(task_num);

		question_panel = new JPanel();
		question_panel.setLayout(new BoxLayout(question_panel,BoxLayout.Y_AXIS));  ////////////////////////////
		basepane.add("Center",question_panel);

		textquestion = new JTextArea [data.task[task_num].max_que];
		textanswer = new JTextField [data.task[task_num].max_que];
		
	    for(int i=0;i<data.task[task_num].max_que;i++){
	    	textquestion[i] = new JTextArea(data.task[task_num].que_ans[i][0]);
	    	textquestion[i].setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
	    	textquestion[i].setLineWrap(true);
	    	textquestion[i].setEnabled(false);
	    	textquestion[i].setBackground(new Color(0xe0,0xff,0xff));
	    	textquestion[i].setDisabledTextColor(Color.black);
			question_panel.add(textquestion[i]);
	    	textanswer[i] = new JTextField();
	    	textanswer[i].setColumns(10);
	    	textanswer[i].addActionListener(new MyBtn2());
	   	    textanswer[i].setFont(new Font(control.sp1.fontName, Font.BOLD, fontSize));
	   	    question_panel.add(textanswer[i]);
	    }
		
		button_panel = new JPanel();
		button_panel.setLayout(new GridLayout(1,2));
		basepane.add("South",button_panel);
		
		String button_name[] = {"check","close"};
		
        if(control.sp1.inJapanese)
        {
            button_name[0] = supporttaskanswer.japaneseWords[8];
            button_name[1] = supporttaskanswer.japaneseWords[9];
        }
        
		button = new JButton [button_name.length];
	    for(int i=0;i<button_name.length;i++){
	    	button[i] = new JButton(button_name[i]);
	    	button[i].addActionListener(new MyBtn2());
	    	button_panel.add(button[i]);
	    }		
	}
	/*
	public void writeActionLog(String data, Component component)
	{
		ACTIONLOG.writeButtonLog("SUPPORT", data, component);
	}	
*/
	class MyBtn2 implements ActionListener
	{	/*
		public void writeActionLog(String data, Component component)
		{
			ACTIONLOG.writeButtonLog("SUPPORT", data, component);
		}		
        */
		public void actionPerformed(ActionEvent e)
		{		
			if(e.getSource() == button[0])//check
			{
				for(int i=0;i<data.task[task_num].max_que;i++){
					String str = textanswer[i].getText();
							
         //           writeActionLog("Answer for "+(task_num+1)+"-"+(i+1)+": "+str);
                    data.sendtimeData(supporttaskanswer.classstage_num,"Q "+(task_num+1)+" :"+data.task[task_num].goal+" part"+(i+1)+" ("+str+")");/////
                    
                    String [] each_ans = data.task[task_num].que_ans[i][1].trim().split("/");
                    for(int j = 0;j<each_ans.length;j++){
                    	if(str.equals(each_ans[j])){
                    		if(each_ans[j] == null)continue;
                    		
                            if(control.sp1.inJapanese)
                                textanswer[i].setText(supporttaskanswer.japaneseWords[10]);
                            else
                                textanswer[i].setText("clear");
                    		textanswer[i].setEnabled(false);
                    		data.task[task_num].que_ans[i][2] = "T";
                    		break;
                    	}
                    	else {
                    		if(data.task[task_num].que_ans[i][2] != "T")
                            {
                                if(control.sp1.inJapanese)
                                    textanswer[i].setText(supporttaskanswer.japaneseWords[11]);
                                else
                                    textanswer[i].setText("miss");
                            }
                    	}
					}
				}
				int c = 0;
				for(int i=0;i<data.task[task_num].max_que;i++){

					if(data.task[task_num].que_ans[i][2].equals("T"))c++;
				}
				if(c == data.task[task_num].max_que){
					data.task[task_num].clear = true;
//                    writeActionLog("Question "+(task_num+1)+": clear");
                    data.sendtimeData(supporttaskanswer.classstage_num,"Q "+(task_num+1)+" :"+data.task[task_num].goal+" clear");/////

			int lastTotalExp = supporttaskanswer.total_exp;					
					
					supporttaskanswer.check_exp();////////////////////////////////////////EXP///
					
					System.out.println("GET EXP:"+(supporttaskanswer.total_exp - lastTotalExp) );				
					control.ex1.addExp(supporttaskanswer.total_exp - lastTotalExp);
					control.ex1.addExptutorial(supporttaskanswer.total_exp - lastTotalExp);
					if(control.expPanel != null)					
						control.expPanel.repaint();					
				}
			}
			if(e.getSource() == button[1])//close
			{
				//		data.task[task_num].click_num += 1;
                if(pop_answer != null)
                {
                    pop_answer.dispose();
                    pop_answer = null;
                }
                if(detail_position != null)
                {
                    detail_position.closeFigureWindow();
                    detail_position.dispose();
                    detail_position = null;
                }
			}
		}
	}
}
