package ch.hsr.challp.museum;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import ch.hsr.challp.museum.model.Content;


public class ContentActivity extends Activity {

    public static final String P_CONTENT_ID = "ContentId";
    private TextToSpeech tts;
    private ContentReader contentReader;

    @Override
    protected void onStart() {
        super.onStart();

        tts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int code) {
                if (code == TextToSpeech.SUCCESS) {
                    tts.setLanguage(Locale.GERMAN);
                } else {
                    tts = null;
                    Toast.makeText(getApplicationContext(), "Text2Speech kann nicht gestartet werden, bitte überprüfen Sie Ihre Einstellungen.",
                            Toast.LENGTH_SHORT).show();
                }
            }
        });
        final ArrayList<String> texts = new ArrayList<>();
        texts.add(((TextView) findViewById(R.id.page_title)).getText().toString());
        texts.add(((TextView) findViewById(R.id.page_text)).getText().toString());
        contentReader = new ContentReader(tts, texts);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_content);
        Content content = getIntent().getParcelableExtra(P_CONTENT_ID);

        ((ImageView) findViewById(R.id.page_preview_image)).setImageResource(content.getPreviewImageResource());
        ((TextView) findViewById(R.id.page_preview_description)).setText(content.getPreviewTitle());
        ((TextView) findViewById(R.id.page_preview_location)).setText(content.getPreviewLocation());
        ((TextView) findViewById(R.id.page_title)).setText(content.getTitle());
        ((TextView) findViewById(R.id.page_text)).setText(content.getContentText());
        ((ImageView) findViewById(R.id.page_image)).setImageResource(content.getImageResource());

        final ActionBar actionBar = getActionBar();
        if (actionBar != null) {
            actionBar.setTitle(R.string.title_activity_content);
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_content, menu);
        return true;
    }

    protected void onStop() {
        // Important, shut the tts service after leaving the activity
        if (tts != null) {
            tts.stop();
            tts.shutdown();
        }
        super.onDestroy();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_play_content) {
            Log.d(getClass().getName(), "Text2Speech Button clicked");
            toggleText2Speech();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void toggleText2Speech() {
        if (contentReader.isPlaying()) {
            contentReader.stopPlaying();
        } else {
            contentReader.play();
        }
    }


    private class ContentReader {

        private TextToSpeech tts;
        private List<String> stringsToSpeech;
        private boolean isPlaying = false;
        private Handler handler;
        private Runnable runnable;

        public ContentReader(TextToSpeech tts, List<String> stringsToSpeech) {
            this.tts = tts;
            this.stringsToSpeech = stringsToSpeech;
        }

        public boolean isPlaying() {
            return isPlaying;
        }

        public void stopPlaying() {
            handler.removeCallbacks(runnable);
            if (tts != null) {
                tts.stop();
            }
            isPlaying = false;
        }

        public void play() {
            isPlaying = true;
            handler = new Handler();
            runnable = new Runnable() {
                int index = 0;

                @Override
                public void run() {
                    if (tts != null && index < stringsToSpeech.size()) {
                        if (!tts.isSpeaking()) {
                            tts.speak(stringsToSpeech.get(index), TextToSpeech.QUEUE_FLUSH, null);
                            index++;
                        }
                        handler.postDelayed(this, 100);
                    } else {
                        isPlaying = false;
                    }
                }
            };
            handler.postDelayed(runnable, 100);
        }

    }
}



@@ -4398,47 +4398,156 @@
 tts 
-!
+=
 = null
- && index %3C stringsToSpeech.size
+) %7B%0A                        isPlaying = false;%0A                        return;%0A                    %7D%0A%0A                    if (!tts.isSpeaking
 ()) 
@@ -4572,39 +4572,52 @@
         if (
-!tts.isSpeaking
+index %3C stringsToSpeech.size
 ()) %7B%0A      
@@ -4763,32 +4763,39 @@
                %7D
+ else %7B
 %0A               
@@ -4807,38 +4807,29 @@
     
-handler.postDelayed(this, 100)
+    isPlaying = false
 ;%0A  
@@ -4842,37 +4842,53 @@
             
-%7D else %7B
+    %7D
 %0A    
+                %7D%0A%0A
             
@@ -4887,32 +4887,36 @@
                 
+if (
 isPlaying = fals
@@ -4904,32 +4904,84 @@
 f (isPlaying
- = false
+()) %7B%0A                        handler.postDelayed(this, 100)
 ;%0A          

fix bug in tts player (toggle now works properly)