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

package source;

import source.TextData.*;
import source.Utility.*;

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

import javax.sound.sampled.*;

class Navigation implements Runnable
{
    //VARIABLE
    Control control;

    //SOUND
    AudioFormat format = null;
    DataLine.Info info = null;
    Clip line = null;
    File audioFile = null;

    int soundLength;
    
    String url;
    
    Thread th = null;
    
    int soundNumber = 101;
    
    int waitingList[] = new int[100];
    int waitingListNumber = 0;

    
    Navigation(Control control)
    {
        this.control = control;
    }
    
    public void soundPlay()
    {
        soundSkip();
        soundPlay(soundNumber);
        
        /* for TEST
        soundPlay(102);
        soundPlay(103);
        soundPlay(104);
        soundPlay(105);
         */
    }
    
    public void soundPlay(int number)
    {
        System.out.println("PLAYING NAVIGATION ORDERED = "+number);
        ACTIONLOG.writeActionLog("NAVIGATION*NAVIGATION*NAVIGATION*"+number);
        addOneToWaitingList(number);
  
        if(th == null)
        {
            System.out.println("CREATED SOUND THREAD -------------------------- for "+number);
            
            th = new Thread(this);
            th.start();
        }
    }
    
    public void soundSkip()
    {
        if(line != null)
        {
            line.setFramePosition(soundLength);
        }
    }
    
    public void soundStop()
    {
        if(line != null)
        {
            line.setFramePosition(soundLength);
            waitingListNumber = 0;
 //           line.stop();
        }
    }
    
    public void addOneToWaitingList(int number)
    {
        waitingList[waitingListNumber++] = number;
        System.out.println("WaitingNumber+ = "+waitingListNumber);
    }
    
    public void playing()
    {
        soundNumber = waitingList[0];

        url = control.absolutePath + "source" + File.separator  + "NavigationSound" + File.separator + soundNumber + ".wav";
        playLine();
        System.out.println("PLAYING NAVIGATION = "+soundNumber);

        ////This should be here
        /*
        try{
            th.sleep(10);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        */
        if(waitingListNumber == 0)
            return;
        
        for(int i=0;i<waitingListNumber;i++)
            waitingList[i] = waitingList[i+1];
        waitingListNumber--;
    }
    
    public void playLine()
    {
        System.out.println("sound play" + url);
        
        try{
            audioFile = new File(url);
            format = AudioSystem.getAudioFileFormat(audioFile).getFormat();
            info = new DataLine.Info(Clip.class, format);
            line = (Clip)AudioSystem.getLine(info);
            line.open(AudioSystem.getAudioInputStream(audioFile));
            line.start();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public boolean isPlaying()
    {
        if(th != null)
            return true;
        return false;
        /*
        if(line != null)
            if(line.getLongFramePosition() < soundLength)
                return true;
        return false;
         */
    }
 
    public void run()// play by soundNumber
    {

        while(waitingListNumber > 0)
        {
            System.out.println("waitingListNumber === "+waitingListNumber);
            playing();
            System.out.println("OUT Playing method === ");

            soundLength = line.getFrameLength();
            while(line.getLongFramePosition() < soundLength);
//            while(line.isActive() || line.isRunning());        //This does not work depending on a PC
        }

        th = null;
        System.out.println("SOUND THREAD END ------------------");
    }
    

   
}
