/*
 * Copyright 2017 Martin Kamp Jensen
 *
 * 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.github.mkjensen.tv.inject;

import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.LoadControl;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.TransferListener;

import android.content.Context;
import android.support.annotation.CheckResult;
import android.support.annotation.NonNull;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.internal.Version;

@Module
public class PlaybackModule {

  /**
   * The maximum bitrate in bits per second that should be assumed when a bandwidth estimate is
   * unavailable.
   */
  private static final int ADAPTIVE_TRACK_SELECTION_MAX_INITIAL_BITRATE = 10 * 1024 * 1024;

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  SimpleExoPlayer exoPlayer(@NonNull Context context, @NonNull LoadControl loadControl,
                            @NonNull TrackSelector trackSelector) {

    return ExoPlayerFactory.newSimpleInstance(context, trackSelector, loadControl);
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  BandwidthMeter exoPlayerBandwidthMeter(@NonNull DefaultBandwidthMeter defaultBandwidthMeter) {

    return defaultBandwidthMeter;
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  DataSource.Factory exoPlayerDataSourceFactory(@NonNull OkHttpClient okHttpClient,
                                                @NonNull TransferListener<? super DataSource> transferListener) {

    return new OkHttpDataSourceFactory(okHttpClient, Version.userAgent(), transferListener);
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  DefaultBandwidthMeter exoPlayerDefaultBandwidthMeter() {

    return new DefaultBandwidthMeter();
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  LoadControl exoPlayerLoadControl() {

    return new DefaultLoadControl();
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  TrackSelector exoPlayerTrackSelector(TrackSelection.Factory trackSelectionFactory) {

    return new DefaultTrackSelector(trackSelectionFactory);
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  TrackSelection.Factory exoPlayerTrackSelectionFactory(BandwidthMeter bandwidthMeter) {

    return new AdaptiveTrackSelection.Factory(
        bandwidthMeter,
        ADAPTIVE_TRACK_SELECTION_MAX_INITIAL_BITRATE,
        AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS,
        AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS,
        AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS,
        AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION);
  }

  @CheckResult
  @NonNull
  @Provides
  @Singleton
  TransferListener<? super DataSource> exoPlayerTransferListener(
      @NonNull DefaultBandwidthMeter defaultBandwidthMeter) {

    return defaultBandwidthMeter;
  }
}

@@ -685,32 +685,94 @@
 ultLoadControl;%0A
+import com.google.android.exoplayer2.DefaultRenderersFactory;%0A
 import com.googl
@@ -852,32 +852,87 @@
 r2.LoadControl;%0A
+import com.google.android.exoplayer2.RenderersFactory;%0A
 import com.googl
@@ -2195,24 +2195,70 @@
 ull 
-Context context,
+RenderersFactory renderersFactory,%0A                           
  @No
@@ -2291,36 +2291,8 @@
 rol,
-%0A                           
  @No
@@ -2379,15 +2379,24 @@
 nce(
-context
+renderersFactory
 , tr
@@ -3244,32 +3244,211 @@
 Control();%0A  %7D%0A%0A
+  @CheckResult%0A  @NonNull%0A  @Provides%0A  @Singleton%0A  RenderersFactory exoPlayerRenderersFactory(@NonNull Context context) %7B%0A%0A    return new DefaultRenderersFactory(context);%0A  %7D%0A%0A
   @CheckResult%0A 

Use ExoPlayerFactory.newSimpleInstance method with RenderersFactory parameter instead of deprecated method with Context parameter