import android.speech.tts.TextToSpeech;
import java.util.Locale;
import java.util.Set;
import android.media.AudioManager;
import android.content.Context;
import ketai.sensors.*;

int INDEX = -1;
int INDEX_ALT = -1;
int COUNTER = 0;

KetaiSensor sensor;
float accelerometerX, accelerometerY, accelerometerZ;

String[] aktuell; 
int aktuell_ebene = 0;

Baum baum;

TextToSpeech tts;

//Set<Voice> stimmen;
AudioManager audio;
int LAUT;
int LEISE;



public void setLangsamUndLaut()
{
    if(tts!=null && audio!=null)
    {
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, LAUT, 0);
        tts.setPitch(1.0);       
        tts.setSpeechRate(1.0);    
    }
}

public void setSchnellUndLeise()
{
    if(tts!=null && audio!=null)
    {
        audio.setStreamVolume(AudioManager.STREAM_MUSIC, LEISE, 0);
        tts.setPitch(1.2);       
        tts.setSpeechRate(2.0);    
    }
}

public void setup()
{
     baum = new Baum(""); //Wurzel
//     String[] ebene0 = {"Ja","Ich","Du bist","Wir","Bitte","Danke","Nein","Quatsch","Die Brille"};
//     String[] ebene0 = {"Die Brille","Nein","Quatsch","Bitte","Ich","Du bist","Wir","Danke","Ja"};
     String[] ebene0 = {"Bitte Helm abnehmen","Nein","Klappt nicht","Das klappt so","Ja"};
     Baum[] e0 = new Baum[ebene0.length];
     for(int i=0;i<ebene0.length;i++)
     {
           e0[i] = baum.addAst(ebene0[i]);
     }
     //"Ich" e0[1]
     String[] ebene1_ich = {"möchte","finde das","empfinde das als"};
     Baum[] e1_ich = new Baum[ebene1_ich.length];
     for(int i=0;i<ebene1_ich.length;i++)
     {
           e1_ich[i] = e0[1].addAst(ebene1_ich[i]);
     }     
     String[] ebene2_moechte = {"etwas trinken","etwas essen","ins Bad","spazieren","lesen","schreiben","fernsehen","mich unterhalten"};
     Baum[] e2_moechte = new Baum[ebene2_moechte.length];
     for(int i=0;i<ebene1_ich.length;i++)
     {
           e2_moechte[i] = e1_ich[0].addAst(ebene2_moechte[i]);
     }     

     aktuell = baum.getAeste();
  
     tts=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
      //@Override
         public void onInit(int status) {
            if(status != TextToSpeech.ERROR) {
               tts.setLanguage(Locale.GERMAN);
            }
         }
      });
      
    //stimmen = TextToSpeech.getVoices();
    audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int currentVolume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
    int maxVolume = audio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    LAUT  = (int) (maxVolume*0.9f);
    LEISE = (int) (maxVolume*0.5f);
    audio.setStreamVolume(AudioManager.STREAM_MUSIC, LAUT, 0);
      
    sensor = new KetaiSensor(this);
    sensor.start();
    textSize(36);
    orientation(LANDSCAPE);
    frameRate(15);
}
int UUU=0;
public void draw()
{
     background(255);
     stroke(0);
     fill(255,0,0);
     if(INDEX>-1)
         rect(50,0+INDEX*70,400,70);
     stroke(0);
     fill(0);
     textSize(64);
     for(int i=0;i<aktuell.length;i++)
         text(aktuell[i],50,70+70*i);
         
  text("Accelerometer: \n" + 
    "x: " + nfp(accelerometerX, 1, 3) + "\n" +
    "y: " + nfp(accelerometerY, 1, 3) + "\n" +
    "z: " + nfp(accelerometerZ, 1, 3), 600, 300);
         
  if(UUU%15==0 && INDEX>-1)
  {
       UUU=0;
       if(COUNTER==0)
       {
             setSchnellUndLeise();
             tts.speak(aktuell[INDEX], TextToSpeech.QUEUE_FLUSH, null);
       }
       else if(COUNTER<2)
       {
             //setSchnellUndLeise();
             //tts.speak(aktuell[INDEX], TextToSpeech.QUEUE_FLUSH, null);
       }
       else if(COUNTER==2)
       {
             setLangsamUndLaut();
             tts.speak(aktuell[INDEX], TextToSpeech.QUEUE_FLUSH, null);
             COUNTER=0;
       }
    
       COUNTER++;
  }
  UUU++;         
}

boolean AB=false;

void mousePressed()
{
     if(tts!=null)
     {
         if(AB)
         {
             setLangsamUndLaut();
             tts.speak("Der Bildschirm wurde berührt.", TextToSpeech.QUEUE_FLUSH, null);
         }    
         else
         {
             setSchnellUndLeise();
             tts.speak("Der Bildschirm wurde berührt.", TextToSpeech.QUEUE_FLUSH, null);
         }
         AB=!AB;
     }    
}

void onAccelerometerEvent(float x, float y, float z)
{
  accelerometerX = x;
  accelerometerY = y;
  accelerometerZ = z;
  
  if(aktuell!=null && aktuell.length>0)
  {
        z+=4.0;
        z/=12.0;
        if(z>12.0f)
            z=12.0f;
        if(z<0.0f)
            z=0.0f;
        INDEX_ALT=INDEX;
        INDEX = aktuell.length -1-(int)(z*(float)aktuell.length);
        
        if(INDEX_ALT!=INDEX)
        {
             COUNTER=0;
        }
  }
}

