package org.gdg.frisbee.android.onboarding;

import android.support.v4.app.ShareCompat;

import org.gdg.frisbee.android.BuildConfig;
import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.common.GdgActivity;
import org.gdg.frisbee.android.utils.PlusUtils;

import okhttp3.HttpUrl;

public class AppInviteLinkGenerator {

    private static final String SENDER = "sender";
    private static final HttpUrl NON_SIGNED_IN_INVITE_URL = HttpUrl.parse("https://fmec6.app.goo.gl/bVbA");

    private final String dynamicLinkDomain;
    private final String deepLinkBaseUrl;

    static String extractSender(HttpUrl httpUrl) {
        return httpUrl.queryParameter(SENDER);
    }

    public static AppInviteLinkGenerator create() {
        return new AppInviteLinkGenerator("https://fmec6.app.goo.gl/", "https://gdg.events/");
    }

    private AppInviteLinkGenerator(String dynamicLinkDomain, String deepLinkBaseUrl) {
        this.dynamicLinkDomain = dynamicLinkDomain;
        this.deepLinkBaseUrl = deepLinkBaseUrl;
    }

    public static void shareAppInviteLink(GdgActivity activity) {
        AppInviteLinkGenerator linkGenerator = create();
        String gplusId = PlusUtils.getCurrentPlusId(activity);
        HttpUrl appInviteLink = gplusId != null
            ? linkGenerator.createAppInviteLink(gplusId)
            : NON_SIGNED_IN_INVITE_URL;
        ShareCompat.IntentBuilder.from(activity)
            .setChooserTitle(R.string.invite_friends)
            .setText(activity.getString(R.string.invitation_message, appInviteLink))
            .setType("text/plain")
            .startChooser();

        activity.sendAnalyticsEvent("AppInvite", "Shared",
            gplusId != null ? "Signed In" : "Non Signed In");
    }

    private HttpUrl createAppInviteLink(String gplusId) {
        return HttpUrl.parse(dynamicLinkDomain)
            .newBuilder()
            .addQueryParameter("link", createDeepLink(gplusId))
            .addQueryParameter("apn", BuildConfig.APPLICATION_ID)
            .build();
    }

    private String createDeepLink(String gplusId) {
        return HttpUrl.parse(deepLinkBaseUrl)
            .newBuilder()
            .addPathSegment("invite")
            .addQueryParameter(SENDER, gplusId)
            .build()
            .toString();
    }

}
