package com.eggheadgames.aboutbox;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

public final class AboutBoxUtils {

    public final static String playStoreAppURI = "https://play.google.com/store/apps/details?id=";
    public final static String amznStoreAppURI = "https://www.amazon.com/gp/mas/dl/android?p=";

    private AboutBoxUtils() {
        //nothing
    }

    public static void getOpenFacebookIntent(Activity context, String name) {
        try {
            context.getPackageManager().getPackageInfo("com.facebook.katana", 0);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + name));
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/" + name));
                context.startActivity(intent);
            } catch (Exception e1) {
                Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public static void startTwitter(Activity context, String name) {
        try {
            context.getPackageManager().getPackageInfo("com.twitter.android", 0);
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=" + name));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/" + name));
                context.startActivity(intent);
            } catch (Exception e1) {
                Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public static void openApp(Activity context, AboutConfig.BuildType buildType, String packageName) {
        String appURI = null;
        String webURI = null;
        switch (buildType) {
            case GOOGLE:
                appURI = "market://details?id=" + packageName;
                webURI = playStoreAppURI + packageName;
                break;
            case AMAZON:
                appURI = "amzn://apps/android?p=" + packageName;
                webURI = amznStoreAppURI + packageName;
                break;
            default:
                //nothing
        }
        openApplication(context, appURI, webURI);
    }

    public static void openPublisher(Activity context, AboutConfig.BuildType buildType, String publisher, String packageName) {
        String appURI = null;
        String webURI = null;
        switch (buildType) {
            case GOOGLE:
                // per: https://developer.android.com/distribute/marketing-tools/linking-to-google-play.html#OpeningPublisher
                appURI = "market://dev?id=" + publisher;
                webURI = "http://play.google.com/store/dev?id=" + publisher;
                break;
            case AMAZON:
                appURI = "amzn://apps/android?showAll=1&p=" + packageName;
                webURI = "http://www.amazon.com/gp/mas/dl/android?showAll=1&p=" + packageName;
                break;
            default:
                //nothing
        }
        openApplication(context, appURI, webURI);
    }

    public static void openApplication(Activity context, String appURI, String webURI) {
        try {
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(appURI)));
        } catch (ActivityNotFoundException e1) {
            try {
                openHTMLPage(context, webURI);
            } catch (ActivityNotFoundException e2) {
                Toast.makeText(context, R.string.egab_can_not_open, Toast.LENGTH_SHORT).show();
            }
        }
    }

    public static void openHTMLPage(Activity context, String htmlPath) {
        context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(htmlPath)));
    }
}

@@ -2816,12 +2816,31 @@
  // 
-per:
+see:%0A                //
  htt
@@ -2938,16 +2938,331 @@
 blisher%0A
+                // https://stackoverflow.com/questions/32029408/how-to-open-developer-page-on-google-play-store-market%0A                if (publisher.matches(%22%5C%5Cd+%22)) %7B%0A                    webURI = %22http://play.google.com/store/dev?id=%22 + publisher;%0A                    appURI = webURI;%0A                %7D else %7B%0A    
         
@@ -3288,23 +3288,29 @@
 arket://
-dev?id=
+search?q=pub:
 %22 + publ
@@ -3308,32 +3308,36 @@
 :%22 + publisher;%0A
+    
                 
@@ -3367,39 +3367,45 @@
 oogle.com/store/
-dev?id=
+search?q=pub:
 %22 + publisher;%0A 
@@ -3395,32 +3395,50 @@
 :%22 + publisher;%0A
+                %7D%0A
                 

Handle both publisher or developer page.