/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * 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.pyamsoft.pydroid.ui.ads;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.pyamsoft.pydroid.ads.AdSource;
import timber.log.Timber;

public class OnlineAdSource implements AdSource {

  @Nullable private final String adId;
  @StringRes private final int resAdId;
  private AdView adView;
  private AdRequest adRequest;

  public OnlineAdSource(@NonNull String adId) {
    this(adId, 0);
  }

  public OnlineAdSource(@StringRes int resAdId) {
    this(null, resAdId);
  }

  private OnlineAdSource(@Nullable String adId, @StringRes int resAdId) {
    this.adId = adId;
    this.resAdId = resAdId;
  }

  @NonNull @Override public View create(@NonNull FragmentActivity activity) {
    adView = new AdView(activity.getApplication());
    adView.setAdSize(AdSize.SMART_BANNER);

    final String realAdId;
    if (adId == null) {
      realAdId = activity.getApplicationContext().getString(resAdId);
    } else {
      realAdId = adId;
    }
    adView.setAdUnitId(realAdId);
    adView.setAdListener(null);

    adRequest = new AdRequest.Builder().build();
    return adView;
  }

  @NonNull @Override public View destroy(boolean isChangingConfigurations) {
    adView.destroy();
    return adView;
  }

  @Override public void start() {
    adView.resume();
  }

  @Override public void stop() {
    adView.pause();
  }

  @Override public void refreshAd(@NonNull AdRefreshedCallback callback) {
    if (adView.getAdListener() == null) {
      adView.setAdListener(new AdListener() {
        @Override public void onAdLoaded() {
          super.onAdLoaded();
          callback.onAdRefreshed();
        }

        @Override public void onAdFailedToLoad(int i) {
          super.onAdFailedToLoad(i);
          Timber.e("Online Ad failed to load");
          callback.onAdFailedLoad();
        }
      });
    }

    adView.loadAd(adRequest);
  }
}

@@ -1062,16 +1062,89 @@
 Source;%0A
+import java.util.Arrays;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A
 import t
@@ -1161,16 +1161,16 @@
 Timber;%0A
-
 %0Apublic 
@@ -1292,16 +1292,64 @@
 esAdId;%0A
+  @NonNull private final Set%3CString%3E testAdIds;%0A
   privat
@@ -1673,16 +1673,157 @@
 esAdId;%0A
+    testAdIds = new HashSet%3C%3E();%0A  %7D%0A%0A  public void addTestAdIds(@NonNull String... testIds) %7B%0A    testAdIds.addAll(Arrays.asList(testIds));%0A
   %7D%0A%0A  @
@@ -2227,25 +2227,25 @@
 );%0A%0A    
-a
+A
 dRequest
  = new A
@@ -2236,16 +2236,32 @@
 dRequest
+.Builder builder
  = new A
@@ -2279,16 +2279,204 @@
 ilder().
+addTestDevice(AdRequest.DEVICE_ID_EMULATOR);%0A%0A    //noinspection Convert2streamapi%0A    for (String testId : testAdIds) %7B%0A      builder.addTestDevice(testId);%0A    %7D%0A    adRequest = builder.
 build();

Add test ids