package com.github.sugoi_wada.appversionchecker;

import android.content.Context;
import android.content.pm.PackageManager;

import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.model.ApkListing;
import com.google.api.services.androidpublisher.model.ApkListingsListResponse;
import com.google.api.services.androidpublisher.model.AppEdit;
import com.google.api.services.androidpublisher.model.Track;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

import rx.Observable;
import rx.Single;

/**
 * Created by watyaa on 16/04/06.
 */
public class PlayStore {

    public enum ReleaseType {
        PRODUCTION, BETA, ALPHA;
    }

    public static Observable<AppListing> checkForUpdates(Context context, String jsonAssetsFileName) {
        return checkForUpdates(context, jsonAssetsFileName, ReleaseType.PRODUCTION);
    }

    public static Observable<AppListing> checkForUpdates(Context context, String jsonAssetsFileName, ReleaseType releaseType) {
        return Single.create((Single.OnSubscribe<AppListing>) singleSubscriber -> {
            AndroidPublisher publisher = null;
            String packageName = context.getPackageName();
            try {
                publisher = AndroidPublisherHelper.init(context, jsonAssetsFileName);
                AndroidPublisher.Edits edits = publisher.edits();
                AppEdit appEdit = edits.insert(packageName, null).execute();

                Track track = edits.tracks().get(packageName, appEdit.getId(), releaseType.name().toLowerCase(Locale.US)).execute();
                List<Integer> versionCodes = track.getVersionCodes();
                if (versionCodes == null || versionCodes.isEmpty()) {
                    // No updates
                    if (!singleSubscriber.isUnsubscribed()) {
                        singleSubscriber.onSuccess(null);
                    }
                    return;
                }

                // order by desc
                Collections.sort(versionCodes, (lhs, rhs) -> rhs.compareTo(lhs));
                int latestVersionCode = versionCodes.get(0);
                int appVersionCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
                if (appVersionCode >= latestVersionCode) {
                    // No updates.
                    if (!singleSubscriber.isUnsubscribed()) {
                        singleSubscriber.onSuccess(null);
                    }
                    return;
                }

                AppListing appListing = new AppListing();
                appListing.setVersionCode(latestVersionCode);
                appListing.setReleaseType(releaseType);

                ApkListingsListResponse apkListingsList = edits.apklistings().list(packageName, appEdit.getId(), latestVersionCode).execute();
                List<ApkListing> listings = apkListingsList.getListings();

                if (listings == null || listings.isEmpty()) {
                    if (!singleSubscriber.isUnsubscribed()) {
                        singleSubscriber.onSuccess(appListing);
                    }
                    return;
                }

                appListing.setDefaultLanguage(listings.get(0).getLanguage());
                appListing.setDefaultRecentChanges(listings.get(0).getRecentChanges());

                String bcp47Language = LocaleUtil.toBcp47Language(Locale.getDefault());
                for (ApkListing listing : listings) {
                    if (listing.getLanguage().equals(bcp47Language)) {
                        appListing.setLanguage(listing.getLanguage());
                        appListing.setRecentChanges(listing.getRecentChanges());
                    }
                }
                if (!singleSubscriber.isUnsubscribed()) {
                    singleSubscriber.onSuccess(appListing);
                }
            } catch (IOException | PackageManager.NameNotFoundException e) {
                if (!singleSubscriber.isUnsubscribed()) {
                    singleSubscriber.onError(e);
                }
            }
        }).toObservable();
    }
}

@@ -732,32 +732,260 @@
 , ALPHA;%0A    %7D%0A%0A
+    public static Observable%3CAppListing%3E checkForUpdates(Context context, String packageName, String jsonAssetsFileName) %7B%0A        return checkForUpdates(context, packageName, jsonAssetsFileName, ReleaseType.PRODUCTION);%0A    %7D%0A%0A
     public stati
@@ -1287,24 +1287,279 @@
 easeType) %7B%0A
+        return checkForUpdates(context, context.getPackageName(), jsonAssetsFileName, releaseType);%0A    %7D%0A%0A    public static Observable%3CAppListing%3E checkForUpdates(Context context, String packageName, String jsonAssetsFileName, ReleaseType releaseType) %7B%0A
         retu
@@ -1681,67 +1681,8 @@
 ll;%0A
-            String packageName = context.getPackageName();%0A
     

Make it possible to specify packane name