package com.growthbeat.link.callback;

import android.net.Uri;
import android.os.Handler;
import android.os.Looper;

import com.growthbeat.link.GrowthLink;
import com.growthbeat.link.model.Synchronization;
import com.growthbeat.utils.DeviceUtils;

public class DefaultSynchronizationCallback implements SynchronizationCallback {

	private static final long INSTALL_REFERRER_TIMEOUT = 10 * 1000;

	@Override
	public void onComplete(final Synchronization synchronization) {

		new Thread(new Runnable() {
			@Override
			public void run() {

				String installReferrer = GrowthLink.getInstance().waitInstallReferrer(INSTALL_REFERRER_TIMEOUT);
				if (synchronizeWithInstallReferrer(synchronization, installReferrer))
					return;

				if (synchronization.getBrowser()) {
					synchronizeWithCookieTracking(synchronization);
				}

				installReferrer = GrowthLink.getInstance().waitInstallReferrer(Long.MAX_VALUE);
				synchronizeWithInstallReferrer(synchronization, installReferrer);

			}
		}).start();

	}

	protected boolean synchronizeWithInstallReferrer(final Synchronization synchronization, String installReferrer) {

		if (installReferrer == null || installReferrer.length() == 0)
			return false;

		String uriString = "?" + installReferrer.replace("growthlink.clickId", "clickId").replace("growthbeat.uuid", "uuid");
		GrowthLink.getInstance().handleOpenUrl(Uri.parse(uriString));
		Synchronization.save(synchronization);

		return true;

	}

	protected void synchronizeWithCookieTracking(final Synchronization synchronization) {

		String advertisingId = null;
		try {
			advertisingId = DeviceUtils.getAdvertisingId().get();
		} catch (Exception e) {
			GrowthLink.getInstance().getLogger().warning("Failed to get advertisingId: " + e.getMessage());
		}

		final String urlString = GrowthLink.getInstance().getSyncronizationUrl() + "?applicationId="
				+ GrowthLink.getInstance().getApplicationId() + (advertisingId != null ? "&advertisingId=" + advertisingId : "");
		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				openBrowser(urlString);
				Synchronization.save(synchronization);
			}
		});

	}

	protected void openBrowser(String urlString) {

		Uri uri = Uri.parse(urlString);
		android.content.Intent androidIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW, uri);
		androidIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
		GrowthLink.getInstance().getContext().startActivity(androidIntent);

	}

}

@@ -387,16 +387,76 @@
  * 1000;
+%0A%09private static final long FINGERPRINT_TIMEOUT = 60 * 1000;
 %0A%0A%09@Over
@@ -520,24 +520,70 @@
 onization) %7B
+%0A%09%09%0A%09%09if (synchronization == null) %0A%09%09%09return;
 %0A%0A%09%09new Thre
@@ -638,17 +638,336 @@
 run() %7B%0A
+%09%09%09%09
 %0A
+%09%09%09%09if (!synchronization.getInstallReferrer() && !synchronization.getBrowser() && synchronization.getClickId() != null ) %7B%0A%09%09%09%09%09GrowthLink.getInstance().waitFingerprint(FINGERPRINT_TIMEOUT);%0A%09%09%09%09%09synchronizeWithFingerprint(synchronization);%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%09%0A%09%09%09%09if ( synchronization.getInstallReferrer() )%7B%0A%09
 %09%09%09%09Stri
@@ -1059,24 +1059,25 @@
 MEOUT);%0A%09%09%09%09
+%09
 if (synchron
@@ -1134,16 +1134,17 @@
 errer))%0A
+%09
 %09%09%09%09%09ret
@@ -1148,17 +1148,17 @@
 return;%0A
-%0A
+%09
 %09%09%09%09if (
@@ -1185,24 +1185,25 @@
 rowser()) %7B%0A
+%09
 %09%09%09%09%09synchro
@@ -1247,19 +1247,20 @@
 n);%0A%09%09%09%09
+%09
 %7D%0A
-%0A
+%09
 %09%09%09%09inst
@@ -1335,24 +1335,25 @@
 VALUE);%0A%09%09%09%09
+%09
 synchronizeW
@@ -1406,16 +1406,119 @@
 errer);%0A
+%09%09%09%09%7D else if(synchronization.getBrowser())%7B%0A%09%09%09%09%09synchronizeWithCookieTracking(synchronization);%0A%09%09%09%09%7D
 %0A%09%09%09%7D%0A%09%09
@@ -2669,24 +2669,242 @@
 %09%7D%0A%09%09%7D);%0A%0A%09%7D
+%0A%09%0A%09protected void synchronizeWithFingerprint(final Synchronization synchronization) %7B%0A%09%09String uriString = %22?clickId=%22 + synchronization.getClickId();%0A%09%09GrowthLink.getInstance().handleOpenUrl(Uri.parse(uriString));%0A%09%7D
 %0A%0A%09protected

change default sync callback