package com.kickstarter.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.ProgressBar;

import com.google.android.exoplayer.AspectRatioFrameLayout;
import com.google.android.exoplayer.ExoPlayer;
import com.kickstarter.R;
import com.kickstarter.libs.BaseActivity;
import com.kickstarter.libs.KSVideoPlayer;
import com.kickstarter.libs.KsrRendererBuilder;
import com.kickstarter.models.Project;
import com.kickstarter.models.Video;

import butterknife.Bind;
import butterknife.ButterKnife;

public final class VideoPlayerActivity extends BaseActivity implements KSVideoPlayer.Listener {
  private MediaController mediaController;
  private KSVideoPlayer player;
  private long playerPosition;

  public @Bind(R.id.video_player_layout) View rootView;
  public @Bind(R.id.surface_view) SurfaceView surfaceView;
  public @Bind(R.id.loading_indicator) ProgressBar loadingIndicatorProgressBar;
  public @Bind(R.id.video_frame) AspectRatioFrameLayout videoFrame;

  @Override
  public void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.video_player_layout);
    ButterKnife.bind(this);

    final Intent intent = getIntent();
    final Project project = intent.getParcelableExtra(getString(R.string.intent_project));
    final Video video = project.video();

    rootView.setOnTouchListener(((view, motionEvent) -> {
      if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
        toggleControlsVisibility();
      }
      return true;
    }));

    // Create player
    player = new KSVideoPlayer(new KsrRendererBuilder(this, video.high()));
    player.setListener(this);
    player.seekTo(playerPosition);  // todo: will be used for inline video playing

    // Set media controller
    mediaController = new MediaController(this);
    mediaController.setMediaPlayer(player.getPlayerControl());
    mediaController.setAnchorView(rootView);
    mediaController.setEnabled(true);

    player.prepare();
    player.setSurface(surfaceView.getHolder().getSurface());
    player.setPlayWhenReady(true);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    releasePlayer();
  }

  @Override
  public void onStateChanged(final boolean playWhenReady, final int playbackState) {
    if (playbackState == ExoPlayer.STATE_ENDED) {
      mediaController.show();
    }

    if (playbackState == ExoPlayer.STATE_BUFFERING) {
      loadingIndicatorProgressBar.setVisibility(View.VISIBLE);
    } else {
      loadingIndicatorProgressBar.setVisibility(View.GONE);
    }
  }

  @Override
  public void onWindowFocusChanged(final boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
      rootView.setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
          | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
          | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
          | View.SYSTEM_UI_FLAG_FULLSCREEN
          | View.SYSTEM_UI_FLAG_IMMERSIVE
      );
    }
  }

  private void releasePlayer() {
    playerPosition = player.getCurrentPosition();
    player.release();
    player = null;
  }

  public void toggleControlsVisibility() {
    if (mediaController.isShowing()) {
      mediaController.hide();
    } else {
      mediaController.show(0);
    }
  }
}

@@ -903,16 +903,39 @@
 osition;
+%0A  private Video video;
 %0A%0A  publ
@@ -1529,20 +1529,8 @@
     
-final Video 
 vide
@@ -1749,559 +1749,181 @@
 ));%0A
-%0A
   
-  // Create player%0A    player = new KSVideoPlayer(new KsrRendererBuilder(this, video.high()));%0A    player.setListener(this);%0A    player.seekTo(playerPosition);  // todo: will be used for inline video playing%0A%0A    // Set media controller%0A    mediaController = new MediaController(this);%0A    mediaController.setMediaPlayer(player.getPlayerControl
+%7D%0A%0A  @Override%0A  public void onDestroy() %7B%0A    super.onDestroy();%0A    releasePlayer
 ()
-)
 ;%0A  
-  mediaController.setAnchorView(rootView);%0A    mediaController.setEnabled(true);%0A%0A    player.prepare();%0A    player.setSurface(surfaceView.getHolder().getSurfac
+%7D%0A%0A  @Override%0A  public void onResume() %7B%0A    super.onResum
 e()
-)
 ;%0A    p
-layer.setPlayWhenReady(true
+reparePlayer(
 );%0A 
@@ -1946,39 +1946,37 @@
   public void on
-Destroy
+Pause
 () %7B%0A    super.o
@@ -1968,39 +1968,37 @@
 ) %7B%0A    super.on
-Destroy
+Pause
 ();%0A    releaseP
@@ -2899,24 +2899,52 @@
 ePlayer() %7B%0A
+    if (player != null) %7B%0A  
     playerPo
@@ -2977,24 +2977,26 @@
 Position();%0A
+  
     player.r
@@ -3005,24 +3005,26 @@
 ease();%0A    
+  
 player = nul
@@ -3026,16 +3026,612 @@
 = null;%0A
+    %7D%0A  %7D%0A%0A  public void preparePlayer() %7B%0A    // Create player%0A    player = new KSVideoPlayer(new KsrRendererBuilder(this, video.high()));%0A    player.setListener(this);%0A    player.seekTo(playerPosition);  // todo: will be used for inline video playing%0A%0A    // Set media controller%0A    mediaController = new MediaController(this);%0A    mediaController.setMediaPlayer(player.getPlayerControl());%0A    mediaController.setAnchorView(rootView);%0A    mediaController.setEnabled(true);%0A%0A    player.prepare();%0A    player.setSurface(surfaceView.getHolder().getSurface());%0A    player.setPlayWhenReady(true);%0A
   %7D%0A%0A  p

refactor for proper release and resume behavior