package de.tomgrill.gdxfirebase.android.admob;

import android.app.Activity;
import android.provider.Settings;
import com.badlogic.gdx.Gdx;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import de.tomgrill.gdxfirebase.core.FirebaseConfiguration;
import de.tomgrill.gdxfirebase.core.admob.Admob;
import de.tomgrill.gdxfirebase.core.admob.VideoRewardAd;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;

public class AndroidAdmob implements Admob {

    private final FirebaseConfiguration firebaseConfiguration;
    private final Activity activity;
    private final AdRequest adRequest;

    public AndroidAdmob(Activity activity, FirebaseConfiguration firebaseConfiguration) {
        this.firebaseConfiguration = firebaseConfiguration;
        this.activity = activity;

        MobileAds.initialize(activity, firebaseConfiguration.admobAppId);

        if (firebaseConfiguration.admobUseTestDevice) {
            String deviceId = getDeviceId();
            adRequest = new AdRequest.Builder().addTestDevice(deviceId).build();
            Gdx.app.debug("gdx-firebase", "Load Admob AdRequest with test device id: " + deviceId);
        } else {
            adRequest = new AdRequest.Builder().build();
            Gdx.app.debug("gdx-firebase", "Load Admob AdRequest (release; no test device)");
        }
    }


    @Override
    public VideoRewardAd getVideoRewardAd() {
        AndroidVideoRewardAd androidVideoRewardAd = new AndroidVideoRewardAd(activity, adRequest);
        return androidVideoRewardAd;
    }

    @Override
    public boolean isTestDevice() {
        return adRequest.isTestDevice(activity);
    }

    private String getDeviceId() {
        String android_id = Settings.Secure.getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
        return md5(android_id).toUpperCase(Locale.ENGLISH);
    }

    private String md5(final String s) {
        try {
            // Create MD5 Hash
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(s.getBytes());
            byte messageDigest[] = digest.digest();

            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++) {
                String h = Integer.toHexString(0xFF & messageDigest[i]);
                while (h.length() < 2)
                    h = "0" + h;
                hexString.append(h);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}

@@ -666,27 +666,28 @@
 nal 
-AdRequest adRequest
+boolean isTestDevice
 ;%0A%0A 
@@ -870,16 +870,82 @@
 ivity;%0A%0A
+        isTestDevice = firebaseConfiguration.admobUseTestDevice;%0A%0A
         
@@ -1015,108 +1015,104 @@
 );%0A%0A
+%0A
     
-    if (firebaseConfiguration.admobUseTestDevice) %7B%0A            String deviceId = ge
+%7D%0A%0A%0A    @Override%0A    public VideoRewardAd getVideoRewardAd() %7B%0A        if (isTes
 tDevice
-Id();
+) %7B
 %0A   
@@ -1124,72 +1124,36 @@
     
-adRequest = new AdRequest.Builder().addTestDevice(deviceId).buil
+String deviceId = getDeviceI
 d();
@@ -1265,40 +1265,53 @@
     
-%7D else %7B%0A            adRequest =
+    return new AndroidVideoRewardAd(activity,
  new
@@ -1335,17 +1335,59 @@
 r().
+addTestDevice(deviceId).
 build()
+)
 ;%0A
+        %7D else %7B%0A
     
@@ -1450,166 +1450,45 @@
 est 
-(release; no test device)%22);%0A        %7D%0A    %7D%0A%0A%0A    @Override%0A    public VideoRewardAd getVideoRewardAd() %7B%0A        AndroidVideoRewardAd androidVideoRewardAd =
+for production)%22);%0A            return
  new
@@ -1519,25 +1519,29 @@
 tivity, 
-a
+new A
 dRequest
 );%0A     
@@ -1536,47 +1536,38 @@
 uest
-);%0A        return androidVideoRewardAd;
+.Builder().build());%0A        %7D
 %0A   
@@ -1639,18 +1639,8 @@
 urn 
-adRequest.
 isTe
@@ -1651,18 +1651,8 @@
 vice
-(activity)
 ;%0A  

Add test device support