package com.github.pedrovgs.sample.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.ImageView;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import com.github.pedrovgs.DraggablePanel;
import com.github.pedrovgs.sample.R;
import com.github.pedrovgs.sample.fragment.MoviePosterFragment;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.squareup.picasso.Picasso;

/**
 * @author Pedro Vicente Gómez Sánchez.
 */
public class YoutubeSampleActivity extends FragmentActivity {

    private static final String YOUTUBE_API_KEY = "AIzaSyC1rMU-mkhoyTvBIdTnYU0dss0tU9vtK48";
    private static final String VIDEO_KEY = "pK2zYHWDZKo";
    private static final String VIDEO_POSTER_THUMBNAIL = "http://4.bp.blogspot.com/-BT6IshdVsoA/UjfnTo_TkBI/AAAAAAAAMWk/JvDCYCoFRlQ/s1600/xmenDOFP.wobbly.1.jpg";
    private static final String SECOND_VIDEO_POSTER_THUMBNAIL = "http://media.comicbook.com/wp-content/uploads/2013/07/x-men-days-of-future-past-wolverine-poster.jpg";

    @InjectView(R.id.iv_thumbnail)
    ImageView iv_thumbnail;
    @InjectView(R.id.draggable_panel)
    DraggablePanel draggablePanel;

    private YouTubePlayer youtubePlayer;

    private YouTubePlayerSupportFragment youtubeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youtube_sample);
        ButterKnife.inject(this);
        initializeYoutubeFragment();
        initializeDraggablePanel();
    }

    private void initializeYoutubeFragment() {
        youtubeFragment = new YouTubePlayerSupportFragment();
        youtubeFragment.initialize(YOUTUBE_API_KEY,
                new YouTubePlayer.OnInitializedListener() {

                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                        YouTubePlayer player, boolean wasRestored) {
                        if (!wasRestored) {
                            youtubePlayer = player;
                            youtubePlayer.loadVideo(VIDEO_KEY);
                            youtubePlayer.setShowFullscreenButton(true);
                        }
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                        YouTubeInitializationResult error) {
                    }

                }
        );
    }

    private void initializeDraggablePanel() {
        draggablePanel.setFragmentManager(getSupportFragmentManager());
        draggablePanel.setTopFragment(youtubeFragment);
        draggablePanel.setBottomFragment(new MoviePosterFragment(VIDEO_POSTER_THUMBNAIL));
        draggablePanel.initializeView();
        Picasso.with(this).load(SECOND_VIDEO_POSTER_THUMBNAIL).into(iv_thumbnail);
    }

    @OnClick(R.id.fl_container)
    void onContainerClicked() {
        draggablePanel.maximize();
    }
}

@@ -237,16 +237,62 @@
 nClick;%0A
+import com.github.pedrovgs.DraggableListener;%0A
 import c
@@ -326,16 +326,16 @@
 ePanel;%0A
-
 import c
@@ -1770,24 +1770,63 @@
 blePanel();%0A
+        hookDraggablePanelListeners();%0A
     %7D%0A%0A    p
@@ -3117,24 +3117,24 @@
 lizeView();%0A
-
         Pica
@@ -3207,24 +3207,807 @@
 il);%0A    %7D%0A%0A
+    private void hookDraggablePanelListeners() %7B%0A        draggablePanel.setDraggableListener(new DraggableListener() %7B%0A            @Override%0A            public void onMaximized() %7B%0A                if (!youtubePlayer.isPlaying()) %7B%0A                    youtubePlayer.play();%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onMinimized() %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onClosedToLeft() %7B%0A                if (youtubePlayer.isPlaying()) %7B%0A                    youtubePlayer.pause();%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onClosedToRight() %7B%0A                if (youtubePlayer.isPlaying()) %7B%0A                    youtubePlayer.pause();%0A                %7D%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     @OnClick

Improve YoutubeSampleActivity adding a DraggableListener to pause/resume youtubePlayer when the view is maximized or closed