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 (installReferrer != null && installReferrer.length() != 0) {
					synchronizeWithInstallReferrer(installReferrer);
					return;
				}

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

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

			}
		});

	}

	protected void synchronizeWithInstallReferrer(String installReferrer) {
		String uriString = "?" + installReferrer.replace("growthlink.clickId", "clickId").replace("growthbeat.uuid", "uuid");
		GrowthLink.getInstance().handleOpenUrl(Uri.parse(uriString));
	}

	protected void synchronizeWithCookieTracking() {

		String urlString = GrowthLink.getInstance().getSyncronizationUrl() + "?applicationId="
				+ GrowthLink.getInstance().getApplicationId();
		try {
			String advertisingId = DeviceUtils.getAdvertisingId().get();
			if (advertisingId != null) {
				urlString += "&advertisingId=" + advertisingId;
			}
		} catch (Exception e) {
			GrowthLink.getInstance().getLogger().warning("Failed to get advertisingId: " + e.getMessage());
		}

		Uri uri = Uri.parse(urlString);
		final android.content.Intent androidIntent = new android.content.Intent(android.content.Intent.ACTION_VIEW, uri);
		androidIntent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);

		new Handler(Looper.getMainLooper()).post(new Runnable() {
			public void run() {
				GrowthLink.getInstance().getContext().startActivity(androidIntent);
			}
		});

	}

}

@@ -730,32 +730,49 @@
 InstallReferrer(
+synchronization, 
 installReferrer)
@@ -830,16 +830,18 @@
 owser())
+ %7B
 %0A%09%09%09%09%09sy
@@ -868,18 +868,39 @@
 racking(
-);
+synchronization);%0A%09%09%09%09%7D
 %0A%0A%09%09%09%09in
@@ -1012,16 +1012,33 @@
 eferrer(
+synchronization, 
 installR
@@ -1053,24 +1053,36 @@
 ;%0A%0A%09%09%09%7D%0A%09%09%7D)
+.start();%0A%09%09
 ;%0A%0A%09%7D%0A%0A%09prot
@@ -1123,16 +1123,55 @@
 eferrer(
+final Synchronization synchronization, 
 String i
@@ -1372,16 +1372,57 @@
 ring));%0A
+%09%09Synchronization.save(synchronization);%0A
 %09%7D%0A%0A%09pro
@@ -1463,23 +1463,292 @@
 racking(
-) %7B%0A%0A%09%09
+final Synchronization synchronization) %7B%0A%0A%09%09String advertisingId = null;%0A%09%09try %7B%0A%09%09%09advertisingId = DeviceUtils.getAdvertisingId().get();%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09GrowthLink.getInstance().getLogger().warning(%22Failed to get advertisingId: %22 + e.getMessage());%0A%09%09%7D%0A%0A%09%09final 
 String u
@@ -1879,28 +1879,12 @@
 Id()
-;%0A%09%09try %7B%0A%09%09%09String 
+ + (
 adve
@@ -1897,265 +1897,276 @@
 gId 
+!
 = 
-DeviceUtils.getAdvertisingId().get();%0A%09%09%09if (advertisingId != null) %7B%0A%09%09%09%09urlString += %22&advertisingId=%22 + advertisingId;%0A%09%09%09%7D%0A%09%09%7D catch (Exception e) %7B%0A%09%09%09GrowthLink.getInstance().getLogger().warning(%22Failed to get advertisingId: %22 + e.getMessage());%0A%09%09%7D
+null ? %22&advertisingId=%22 + advertisingId : %22%22);%0A%09%09new Handler(Looper.getMainLooper()).post(new Runnable() %7B%0A%09%09%09public void run() %7B%0A%09%09%09%09openBrowser(urlString);%0A%09%09%09%09Synchronization.save(synchronization);%0A%09%09%09%7D%0A%09%09%7D);%0A%0A%09%7D%0A%0A%09protected void openBrowser(String urlString) %7B
 %0A%0A%09%09
@@ -2390,94 +2390,8 @@
 K);%0A
-%0A%09%09new Handler(Looper.getMainLooper()).post(new Runnable() %7B%0A%09%09%09public void run() %7B%0A%09%09
 %09%09Gr
@@ -2455,27 +2455,16 @@
 Intent);
-%0A%09%09%09%7D%0A%09%09%7D);
 %0A%0A%09%7D%0A%0A%7D%0A

Save synchronization after handling deeplink