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

package source;


import java.io.File;
import java.io.*;
import java.text.*;
import java.util.*;

import java.awt.*;
import javax.swing.*;


public class NavigationControl implements Runnable
{
    //VARIABLE
    Control control;
    Navigation navigation;
    
    Thread th = null;
    int requestedNumber = 0;
    
    int initialVoice[] = {102,201,301,401,501,601,701,801,901,1001};
    final int MAXNUMBER = 1100;
    
    String menuInJapanese[];
    
    String serifFileName;
    int serifIDs[];
    int reverseSerifIDs[];
    String serifList[];
    int lineNumber = 0;
    
    JFrame windowFrame = null;  //toFront after yes no option
    
    NavigationControl(Control control)
    {
        this.control = control;
        navigation = new Navigation(control);
        
        navigation.soundNumber = 102;

        menuInJapanese = FILEIO.TextFileAllReadCodeArray(new File(control.sp1.absolutePath + "source" + File.separator + "japaneseText" + File.separator+ "JapaneseNavigationControl.txt"));
        
        //READ SERIF
        serifFileName = control.sp1.absolutePath + "source" + File.separator + "japaneseText" + File.separator+ "NavigationSerifList.txt";
        lineNumber = FILEIO.line_count(new File(serifFileName));
        serifIDs = new int[lineNumber];
        serifList = new String[lineNumber];
        readNavigationSerif();
        reverseSerifIDs = new int[MAXNUMBER];
        for(int i=0;i<lineNumber;i++)
            reverseSerifIDs[serifIDs[i]] = i;
    }
    
    void readNavigationSerif()
    {
        try
        {
            String oneLine[];
            BufferedReader br1 = new BufferedReader(new InputStreamReader(new FileInputStream(serifFileName), "JISAutoDetect"));
            for(int i=0;i<lineNumber;i++)
            {
                oneLine = br1.readLine().split(" ");
                serifIDs[i] = Integer.parseInt(oneLine[0]);
                serifList[i] = oneLine[1];
            }
            br1.close();
        }
        catch(Exception e){
            System.out.println("File Reading ERROR serifFileName");
        }
    }
    
    void startNavigation()
    {
        if(th != null)
        {
            navigation.soundStop();
            th = null;
        }
  //          th.stop();
        
        th = new Thread(this);
        th.start();
        System.out.println("------------------ NAVIGATION CONTROL THREAD START ------------------");
    }
    
    public void controlNavigation(int number)
    {

        
        /////5.
        if(number >= 511 && number <=522)
        {
            soundSkip();
            soundPlay(number);
            return;
        }
        
        /////7.
        if(number >= 711 && number <=718)
        {
            soundPlay(number);
            return;
        }
        
        switch(number)
        {
                /////0.
            case 100:
                soundPlay(100);
                /////1.
            case 101:
                soundPlay(101);
                if(yesNoAction(101))
                {
                    if(control.btTutorial.isSelected() == false)
                        control.btTutorial.doClick();
                    soundPlay(107);
                }
                else
                    soundPlay(105);
                break;
                
            case 102:
                soundPlay(102);
                soundPlay(103);
                if(yesNoAction(103))
                    soundPlay(106);
                else
                {
                    soundPlay(104);
//                    soundPlay(105);
                }
            /*    soundPlay(103);
                 soundPlay(104);
                 soundPlay(105);
                soundPlay(106);*/ // FOR TEST
                break;
                
                /////2.
            case 201:
                soundPlay(201);
                if(yesNoAction(201))
                {
                    soundPlay(202);
                    if(control.text.textLength == 0)
                    {
                        soundPlay(205);
                        soundPlay(206);
                        if(yesNoAction(206))
                        {
                            if(control.sp1.mode == 0 || control.sp1.mode == 1)
                            {
                                control.setMode(2); //change mode
                                soundPlay(207);
                            }
                            soundPlay(208);
                        }
                        else
                        {
                            control.getPanelInformation();
                            int i;
                            for(i=0;i<control.panelNumber;i++)
                            {
                                System.out.println("ID="+control.miningIDs[i]);
                                if(control.miningIDs[i] == 1)       //find EditModule
                                    break;
                            }
                            
                            if(i == control.panelNumber)
                            {
                                control.getPanelInformation();
                                control.miningIDs[control.panelNumber-1] = 1;       // SET EditModue at RIGHT END PANEL
                                control.visualizationIDs[control.panelNumber-1] = 1;
                                control.panelSet(control.miningIDs, control.visualizationIDs, control.visualizationIDs, control.balancedPanelSizeX);
                                soundPlay(209);
                            }
                            soundPlay(210);
                        }
                    }
                }
                else
                    soundPlay(203);
                break;
                
            case 220:
                soundPlay(211);
                if(control.text.sentenceNumber < control.text.textLength/100)
                    soundPlay(310);
                
                if(control.text.segmentNumber == 1)
                    soundPlay(311);

//                break;
                
                /////3.
            case 301:
                soundPlay(301);
                soundPlay(302);
                if(yesNoAction(302))
                {
                    if(control.sp1.mode == 0)
                        control.setMode(1); //change mode
                    if(control.bt0.isSelected() == false)
                        control.bt0.doClick(); //keyword
                    soundPlay(303);
                }
                
                soundPlay(304);
                if(yesNoAction(304))
                {
                     //same as 209
                    control.getPanelInformation();
                    int i;
                    for(i=0;i<control.panelNumber;i++)
                    {
                        System.out.println("ID="+control.miningIDs[i]);
                        if(control.miningIDs[i] == 1)       //find EditModule
                            break;
                    }
                    
                    if(i == control.panelNumber)
                    {
                        control.getPanelInformation();
                        control.miningIDs[control.panelNumber-1] = 1;       // SET EditModue at RIGHT END PANEL
                        control.visualizationIDs[control.panelNumber-1] = 1;
                        control.panelSet(control.miningIDs, control.visualizationIDs, control.visualizationIDs, control.balancedPanelSizeX);
                        soundPlay(305);
                    }
                    if(control.text.sentenceNumber < control.text.textLength/100)
                    {
                        soundPlay(310);
                        soundPlay(306);
                    }
                    if(control.text.segmentNumber == 1)
                    {
                        soundPlay(311);
                        soundPlay(307);
                    }
                    soundPlay(308);
                }
                else
                    soundPlay(309);
                break;
                

                
                /////4.
            case 401:
                boolean flag = false;
                soundPlay(401);
                soundPlay(402);
                soundPlay(403);
                if(yesNoAction(403))
                {
                    int m1[] = {40,35,53,54,25};
                    int v1[] = {3,24,24,2,21};
                    int t1[] = {0,0,0,0,0};
                    int s1[] = {4,12,4,14,12};
                    control.panelSet(m1,v1,t1,s1);
                    
                    soundPlay(404);
                }
                else
                {
                    soundPlay(405);
                    if(yesNoAction(405))
                    {
                        int m2[] = {40,52,55};
                        int v2[] = {3,26,2};
                        int t2[] = {0,0,0};
                        int s2[] = {4,10,8};
                        control.panelSet(m2,v2,t2,s2);
                        
                        soundPlay(406);
                    }
                    else
                        flag = true;
 
                }
                if(flag == false)
                    break;
                
            case 407:
                if(control.sp1.mode == 0)
                {
                    soundPlay(407);
                    soundPlay(408);
                    if(yesNoAction(408))
                    {
                        control.setMode(1); //change mode
                    }
                    else
                    {
                        soundPlay(409);
                        break;
                    }
                }
                
            case 410:
                soundPlay(410);
                soundPlay(411);
                if(control.btxTree.isSelected() == false)
                    control.btxTree.doClick();
                break;
                
 //           case 411:
   //             soundPlay(411);
     //           break;
                
                /////5.
            case 501:
                soundPlay(501);
                soundPlay(502);
                soundPlay(503);
                break;
                
                /////6.
            case 601:
                break;
                
                /////7.
            case 701:
                soundPlay(701);
                soundPlay(7001);
                break;
                
            case 702:
                soundPlay(702);
                if(yesNoAction(702))
                    soundPlay(703);
                else
                {
                    soundPlay(709);
                    soundPlay(910);
                    soundPlay(911);
                }
                break;
                
            case 735:
                soundPlay(735);
                break;
            case 740:
                soundPlay(740);
                break;
                
            case 745:
            case 750:
            case 755:
            case 760:
                soundPlay(number);
                if(yesNoAction(770))
                {
                    if(control.bt78.isSelected() == false)
                        control.bt78.doClick();
                }
                else
                    soundPlay(721);
                break;

                /////8.
            case 801:
                soundPlay(801);
                soundPlay(802);
                if(yesNoAction(802))
                    soundPlay(803);
                else
                    soundPlay(804);
                break;
                
            case 810:
                soundPlay(801);
                break;
                
                /////9.
            case 901:
                soundPlay(901);
                soundPlay(902);
                break;
                
                /////10.
            case 1001:
                soundPlay(1001);
                soundPlay(1002);
                if(yesNoAction(1002))
                {
                    if(control.bt78.isSelected() == false)
                        control.bt78.doClick();
                    soundPlay(1003);
                }
                break;
                
            case 1010:
                soundPlay(1010);
                break;

            case 1020:
                soundPlay(1020);
                break;
                
            case 1021:
                soundPlay(1021);
                break;
                
            case 7000:
            case 10000:
            case 10001:
            case 10002:
            case 10003:
            case 10004:
            case 10005:
            case 10006:
            case 15000:
            case 15001:
            case 15002:
            case 15003:
            case 15004:
            case 15005:
            case 15006:
            case 15007:
            case 15008:
            case 15009:
            case 15010:
            case 15011:
            case 15012:
            case 15013:
            case 15014:
            case 15015:
            case 15016:
            case 15017:
            case 15018:
            case 15019:
            case 20000:
            case 20001:
            case 20002:
            case 20003:
            case 20004:
            case 20005:
            case 20006:
            case 20007:
            case 20008:
            case 20009:
            case 20010:
            case 20011:
            case 20012:
            case 20013:
                soundPlay(number);
                break;
                
                
            default:
                System.out.println("Requested Number was not found.  --------> "+number);
                break;
        }
    }

    public void soundPlay(){
        navigation.soundPlay();
    }
    public void soundPlay(int number){
        navigation.soundPlay(number);
    }
    public void soundSkip(){
        navigation.soundSkip();
    }
    
    //ACTIVATION    //USE THIS FROM ANOTHER MODULES TO ACTIVATION
    public void setSoundNumber(int number)
    {
        if(control.sp1.navigation == false)
            return;
  
        if(control.characterTutorial != 0)  //when TUTORIAL is on going
            return;
        
//        System.out.println("MOVE CHARACTER ======= "+StoryControl.popForMoveCharacter);
        
        navigation.soundStop();
        
        requestedNumber = number;
        startNavigation();
    }
    /*
    public void setSoundNumber(int number, boolean soundStop)
    {
        if(control.sp1.navigation == false)
            return;
        
        if(StoryControl.popForMoveCharacter != null)    //when TUTORIAL is on going
            return;
        
        if(soundStop)
            navigation.soundStop();
        
        requestedNumber = number;
        startNavigation();
    }
    */
    public void setSoundNumber(int number, JFrame windowFrame)
    {
        this.windowFrame = windowFrame;
        setSoundNumber(number);
    }
    
    //ACTIVATION    //USE THIS FROM ANOTHER MODULES TO ACTIVATION
    // This method is used for succeeding navigation
    // The formar navigation should not include yes-no option
    public void setSoundNumberAddition(int number)
    {
        if(control.sp1.navigation == false)
            return;
        
        if(control.characterTutorial != 0)  //when TUTORIAL is on going
            return;

        try{
            Thread.sleep(1000);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        
        soundPlay(number);
    }
    
    public void setStep(int step)
    {
        setSoundNumber(initialVoice[step]);
    }
    
    public boolean yesNoAction(int number)
    {
        int option;
        
        while(navigation.th != null)
        {
            try{
                Thread.sleep(0);
            }
            catch(Exception e){
                e.printStackTrace();
            }
        }
        
        if(control.sp1.inJapanese)
            option = JOptionPane.showConfirmDialog(control, serifList[reverseSerifIDs[number]], menuInJapanese[0], JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        else
            option = JOptionPane.showConfirmDialog(control, "Answer?", "Question", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);

        if(windowFrame != null)
        {
            windowFrame.toFront();
            windowFrame = null;
        }
        
        if(option == JOptionPane.YES_OPTION)
            return true;
        else
            return false;
    }
    

    public void run()// Detecting Loop
    {
        controlNavigation(requestedNumber);
        System.out.println("------------------ NAVIGATION CONTROL THREAD END ------------------");
//        th.stop();
        th = null;
    }

   
}
