package com.litmus.worldscope;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

import fragment.CommentFragment;
import fragment.StreamCreateFragment;
import fragment.StreamVideoControlFragment;
import fragment.StreamVideoFragment;

public class StreamActivity extends AppCompatActivity implements StreamVideoFragment.OnStreamVideoFragmentListener,
        StreamCreateFragment.OnStreamCreateFragmentListener,
        StreamVideoControlFragment.OnStreamVideoControlFragmentListener,
        CommentFragment.OnCommentFragmentInteractionListener {

    private static final String TAG = "StreamActivity";
    private String streamWhenReadyTag = "streamWhenReady";
    private String isRecordingTag = "isRecordingTag";
    private StreamVideoFragment.StreamVideoControls control;
    private StreamCreateFragment streamCreateFragment;
    private StreamVideoControlFragment streamVideoControlFragment;
    private CommentFragment commentFragment;
    private android.support.v4.app.FragmentManager sfm;
    private boolean streamWhenReady = false;
    private boolean isRecording = false;
    private GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(savedInstanceState != null) {
            savedInstanceState.getBoolean(streamWhenReadyTag);
            savedInstanceState.getBoolean(isRecordingTag);
        }

        setContentView(R.layout.activity_stream);
        sfm = getSupportFragmentManager();

        // Create GestureDetector
        gestureDetector = new GestureDetector(this, new GestureListener());

        // Get streamCreateFragment
        streamCreateFragment = (StreamCreateFragment) sfm.findFragmentById(R.id.streamCreateFragment);
        // Get streamVideoControlFragment
        streamVideoControlFragment = (StreamVideoControlFragment) sfm.findFragmentById(R.id.streamVideoControlFragment);
        // Get commentFragment
        commentFragment = (CommentFragment) sfm.findFragmentById(R.id.commentFragment);

        Log.d(TAG, "Streamer activity created!");
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean(streamWhenReadyTag, streamWhenReady);
        outState.putBoolean(isRecordingTag , isRecording);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gestureDetector.onTouchEvent(event);
    }

    /**
     * Implementing StreamVideoFragment
     */
    @Override
    public void streamVideoFragmentReady(StreamVideoFragment.StreamVideoControls control) {
        this.control = control;
        Log.d(TAG, "Streamer ready!");
        if(streamWhenReady) {
            control.startStreaming();
        }
    }

    /**
     * Implementing StreamCreateFragment
     */

    @Override
    public void onStreamCreationSuccess(String rtmpLink, String appInstance, String alias) {
        Log.d(TAG, rtmpLink);

        // Find streamVideoFragment and set the rtmp link from streamCreateFragment
        StreamVideoFragment streamVideoFragment = (StreamVideoFragment) sfm.findFragmentById(R.id.streamVideoFragment);
        streamVideoFragment.setRTMPLink(rtmpLink);

        // If control is ready, start streaming, else stream when ready
        if(control != null) {
            isRecording = true;
            control.startStreaming();
        } else {
            streamWhenReady = true;
        }

        // Start the streamVideoControls
        streamVideoControlFragment.startStreaming();

        // Join room and show comment UI
        commentFragment.joinRoom(appInstance, alias);
        commentFragment.initialize();
    }

    @Override
    public void onCancelStreamButtonClicked() {
        control.destroyStreamer();
        redirectToMainActivity();
    }


    @Override
    public void onStreamTerminationResolved(boolean isTerminated) {
        if(isTerminated) {
            redirectToMainActivity();
        } else {
            // Release the controls back
            streamVideoControlFragment.unBlockControls();
        }
    }

    /**
     * Implementing StreamVideoControlFragment
     */

    @Override
    public void onStreamRecordButtonShortPress() {
        // Signal to StreamVideoFragment to pause the stream
        if(isRecording) {
            control.stopStreaming();
        } else {
            control.startStreaming();
        }

        // Toggle the isRecording boolean
        isRecording = !isRecording;
    }

    @Override
    public void onStreamRecordButtonLongPress() {
        // Signal to StreamCreateFragment to do a confirmation and stop stream
        streamCreateFragment.confirmStreamTermination();
    }

    private void redirectToMainActivity() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    // For CommentFragment TODO: Update
    @Override
    public void onFragmentInteraction() {

    }

    // Gesture Listener to listen for double taps
    private class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // On Double Tap, calls the video control fragment back into view if hidden
            Log.d(TAG, "Double Tap detected, showing controls");
            streamVideoControlFragment.toggleControlVisibility();
            return true;
        }
    }

}

@@ -193,16 +193,46 @@
 tector;%0A
+import android.view.KeyEvent;%0A
 import a
@@ -256,16 +256,16 @@
 nEvent;%0A
-
 %0Aimport 
@@ -893,16 +893,42 @@
 ngTag%22;%0A
+    private String alias;%0A
     priv
@@ -1617,32 +1617,86 @@
 ag);%0A        %7D%0A%0A
+        alias = getIntent().getStringExtra(%22alias%22);%0A%0A
         setConte
@@ -2727,32 +2727,301 @@
 (event);%0A    %7D%0A%0A
+    // Override to intercept all touch events, required as listView was consuming touch events%0A    @Override%0A    public boolean dispatchTouchEvent(MotionEvent event) %7B%0A        gestureDetector.onTouchEvent(event);%0A        return super.dispatchTouchEvent(event);%0A    %7D%0A%0A%0A
     /**%0A     * I
@@ -3473,22 +3473,8 @@
 ance
-, String alias
 ) %7B%0A
@@ -4324,24 +4324,25 @@
 Streamer();%0A
+%0A
         redi
@@ -5402,24 +5402,345 @@
 nt);%0A    %7D%0A%0A
+    @Override%0A    public boolean onKeyDown(int keyCode, KeyEvent event)  %7B%0A        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 && isRecording) %7B%0A            streamCreateFragment.confirmStreamTermination();%0A            return true;%0A        %7D%0A%0A        return super.onKeyDown(keyCode, event);%0A    %7D%0A%0A
     // For C
@@ -5753,16 +5753,23 @@
 Fragment
+%0A    //
  TODO: U
@@ -6398,17 +6398,16 @@
       %7D%0A
+
     %7D%0A
-%0A
 %7D%0A

Fix comment list blocking touch events