/*
 *
 *  * Copyright (C) 2016 Track & Talk Ltd
 *  *
 *  * Licensed under the Apache License, Version 2.0 (the "License");
 *  * you may not use this file except in compliance with the License.
 *  * You may obtain a copy of the License at
 *  *
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  * Unless required by applicable law or agreed to in writing, software
 *  * distributed under the License is distributed on an "AS IS" BASIS,
 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  * See the License for the specific language governing permissions and
 *  * limitations under the License.
 *
 */

package com.trackandtalk.pafos17.ui.intro;

import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.trackandtalk.pafos17.BuildConfig;
import com.trackandtalk.pafos17.R;

/**
 * Created by Konstantinos Papageorgiou and Charalambos Xinaris
 */
public class IntroVideoFragment extends Fragment {

    private static final String ARG_BG_COLOR = "bg_color";
    private int bgColor;

    private LinearLayout rootLayout;
    private YouTubePlayerSupportFragment youTubePlayerFragment;

    public static IntroVideoFragment newInstance(@ColorInt int bgColor) {
        IntroVideoFragment sampleSlide = new IntroVideoFragment();

        Bundle args = new Bundle();
        args.putInt(ARG_BG_COLOR, bgColor);
        sampleSlide.setArguments(args);

        return sampleSlide;
    }

    public IntroVideoFragment() {
        //  required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bgColor = getArguments().getInt(ARG_BG_COLOR);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_intro_video, container, false);
        rootLayout = (LinearLayout)view.findViewById(R.id.intro_video_background);
        setBackgroundColor(bgColor);

        initYouTube();

        return view;
    }

    private void initYouTube() {
        if (youTubePlayerFragment == null) {
            youTubePlayerFragment = (YouTubePlayerSupportFragment) getChildFragmentManager().findFragmentById(R.id.youtube_fragment);
            youTubePlayerFragment.initialize(BuildConfig.YOUTUBE_KEY, new YouTubePlayer.OnInitializedListener() {
                @Override
                public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
                    youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
                    youTubePlayer.cueVideo("2B1g1K5xVsU");
                    youTubePlayer.play();
                }

                @Override
                public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                    Snackbar.make(rootLayout, "Failed to load Youtube video", Snackbar.LENGTH_SHORT).show();

                }
            });
        }
    }

    private void setBackgroundColor(@ColorInt int color) {
        rootLayout.setBackgroundColor(color);
    }
}

@@ -1606,16 +1606,57 @@
 ragment;
+%0A    private YouTubePlayer youTubePlayer;
 %0A%0A    pu
@@ -3122,32 +3122,45 @@
                 
+startYouTube(
 youTubePlayer.ad
@@ -3160,178 +3160,8 @@
 ayer
-.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);%0A                    youTubePlayer.cueVideo(%222B1g1K5xVsU%22);%0A                    youTubePlayer.play(
 );%0A 
@@ -3531,94 +3531,561 @@
 id s
-etBackgroundColor(@ColorInt int color) %7B%0A        rootLayout.setBackgroundColor(color);
+tartYouTube(YouTubePlayer youTubePlayer) %7B%0A        this.youTubePlayer = youTubePlayer;%0A        this.youTubePlayer.addFullscreenControlFlag(YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);%0A        this.youTubePlayer.cueVideo(%222B1g1K5xVsU%22);%0A        this.youTubePlayer.play();%0A    %7D%0A%0A    private void setBackgroundColor(@ColorInt int color) %7B%0A        rootLayout.setBackgroundColor(color);%0A    %7D%0A%0A    @Override%0A    public void onDestroyView() %7B%0A        super.onDestroyView();%0A%0A        if (youTubePlayer!= null) %7B%0A            youTubePlayer.release();%0A        %7D
 %0A   

Fix YoutubePlayer memory leak