

//
// Core Program for TETDM 
// PopUpForSerif.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.GameMode.*;
import source.TextData.*;
import source.Utility.*;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.image.*;
import javax.imageio.ImageIO;

import static java.awt.BorderLayout.* ;


public class PopUpForSerif extends JFrame implements MouseListener,MouseMotionListener,Runnable  ////////////////////////
{
    Control control;
    
    Container pane;
	public SerifPanel serifPanel;

    Image image;
    ImageIcon icon;
    String fukidashiFilename;

    int numberOfSerif;   // number of serif 1-3
    int direction;  // 0:Left, 1:Right
    int serifType;    // 0:color(question,operation), 1:normal    //color = 1:normal, 2:question, 3:operation
	
    String serif[];

    int serifCount;
    int maxLength;
    long start,end;
    
    boolean clearFlag = false;
		
    public PopUpForSerif(int sX,int sY,int width,int height,boolean decolation,int serifCount, String serifText[], int color, Control control, boolean voice, boolean clearFlag)
    {
        start = System.currentTimeMillis();
        
        this.control = control;
        String imagePath = control.absolutePath+"source"+ File.separator + "Tutorial" +File.separator+"image"+File.separator;
        
        this.serifCount = serifCount;
        this.clearFlag = clearFlag;
        
        if(color == 1)
            serifType = 1;
        else
            serifType = 0;
        
        serif = serifText;
        numberOfSerif = serif.length;
        
        maxLength = serif[0].length();
        for(int i=1;i<numberOfSerif;i++)
            if(serif[i].length() > maxLength)
                maxLength = serif[i].length();
        
        width = ((maxLength*15)/7)*11;
        height = (numberOfSerif*15)+((numberOfSerif+1)*10);
        
        int X = sX-width+30;
        int Y = sY+35-25*numberOfSerif;
        
        if(X<0)
        {
            direction = 1;
            X = sX+120;
            switch(color)
            {
                case 1:
                    fukidashiFilename = "fukidashi1R.png";
                    break;
                case 2:
                    fukidashiFilename = "fukidashi2R.png";
                    break;
                case 3:
                    fukidashiFilename = "fukidashi3R.png";
                    break;
            }
        }
        else
        {
            direction = 0;
            switch(color)
            {
                case 1:
                    fukidashiFilename = "fukidashi1.png";
                    break;
                case 2:
                    fukidashiFilename = "fukidashi2.png";
                    break;
                case 3:
                    fukidashiFilename = "fukidashi3.png";
                    break;
            }
        }

        icon = new ImageIcon(imagePath + fukidashiFilename);
        image = icon.getImage();
        
        
        
        pane = getContentPane();
        pane.setLayout(new BorderLayout());
        
        /////////////////////
        pane.addMouseListener(this);
        pane.addMouseMotionListener(this);
        /////////////////////
        
        serifPanel = new SerifPanel(control);
        pane.add(serifPanel);
        
        setAlwaysOnTop(true);
        setUndecorated(decolation);
        setVisible(true);
        
        JFrame.setDefaultLookAndFeelDecorated(true);
        setBackground(new Color(0,0,0,0));
        
        setBounds(X, Y, width, height);
        
        if(voice == true)
        {
            Thread th = new Thread(this);
            th.start();
        }
    }

    public void run()
    {
        System.out.println("serif play");
        
        for(int i=0;i<numberOfSerif;i++)
            EXECUTE.sayJapaneseWords(serif[i]);
    }

	///////////////////////////////////////////
	int mousex ,mousey;
    int positionx, positiony;
	int incompox,incompoy;
	
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}

    public void mouseClicked(MouseEvent e)
    {
        int btn = e.getButton();
        end = System.currentTimeMillis();
        
        if(serifType==1)    // proceed except question and challengeA
        {
            if(clearFlag == true || (end-start)>500)        //500ms per line
                if (btn == MouseEvent.BUTTON1)
                {
                    serifCount++;
                    StoryControl.SerifControl(serifCount, true);
                    
                    this.dispose();
                    

                }

            if(clearFlag == true || (end-start)>200)
                if (btn == MouseEvent.BUTTON3)
                    if(serifCount>0)
                    {
                        serifCount--;
                        if(StoryControl.SerifControl(serifCount, false) == true)
                            this.dispose();
                        else
                            serifCount++;
                    }
        }
        else    // from question or challengeA
        {
            if(clearFlag == true || (end-start)>200)
                if (btn == MouseEvent.BUTTON3)
                {
                    System.out.println("Try to back. serifCount= "+serifCount);
                    if(serifCount>=0)
                        if(StoryControl.SerifControl(serifCount, false) == true)
                        {
                            StoryControl.actionNumber = 0;
                            this.dispose();
                        }
                    return;
                }
            
            StoryControl.nextSerif(1);
        }  
    }
	
	public void mousePressed(MouseEvent e)
    {
		incompox = 0;
		incompoy = 0;
		incompox = e.getPoint().x;
		incompoy = e.getPoint().y;
	}
	
	public void mouseReleased(MouseEvent e){}

	public void mouseDragged(MouseEvent e)
    {
		mousex = e.getLocationOnScreen().x;
		mousey = e.getLocationOnScreen().y;
				
		positionx = mousex - incompox;
		positiony = mousey - incompoy;
	
		positionx -= getInsets().left;
		positiony -= getInsets().top;

		setLocation(positionx, positiony);
	}	
	public void mouseMoved(MouseEvent e){}
	/////////////////////////////////////////////////////
	


	
	public class SerifPanel extends JPanel
	{
        public int sizeX, sizeY;
        
        int margin = 2;
        Control control;
        
		SerifPanel(Control control)
        {
            this.control = control;
        }
		
        public void getLocalSize()
        {
            sizeX = getWidth();
            sizeY = getHeight();
        }
        
		public void update(Graphics g)		//Avoid from blinking
		{
			paintComponent(g);
		}
		
		public void paintComponent(Graphics g)
		{
			Graphics2D g2 = (Graphics2D)g;

			
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);// Anti aliasing
			g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);			
			
			getLocalSize();
            
            g2.drawImage(image,0,0,sizeX,sizeY,this);
            g2.setFont(new Font(control.sp1.fontName,Font.BOLD,15));
            
            if(direction==1)
                for(int i=0;i<numberOfSerif;i++)
                    g2.drawString(serif[i],(sizeX/11)*3,20+25*i+margin);
            else
                for(int i=0;i<numberOfSerif;i++)
                    g2.drawString(serif[i],(sizeX/11),20+25*i+margin);
		}
	}	
}


