package cl.json.social;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import cl.json.ShareFile;

/**
 * Created by disenodosbbcl on 23-07-16.
 */
public abstract class ShareIntent {

    protected final ReactApplicationContext reactContext;
    protected Intent intent;
    protected String chooserTitle = "Share";
    public ShareIntent(ReactApplicationContext reactContext) {
        this.reactContext = reactContext;
        this.setIntent(new Intent(android.content.Intent.ACTION_SEND));
        this.getIntent().setType("text/plain");
    }
    public void open(ReadableMap options) throws ActivityNotFoundException {
//        if (ShareIntent.hasValidKey("subject", options) ) {
//            this.getIntent().putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
//        }
//
//        if (ShareIntent.hasValidKey("message", options) && ShareIntent.hasValidKey("url", options)) {
//            ShareFile fileShare = getFileShare(options);
//            if(fileShare.isFile()) {
//                Uri uriFile = fileShare.getURI();
//                this.getIntent().setType(fileShare.getType());
//                this.getIntent().putExtra(Intent.EXTRA_STREAM, uriFile);
//                this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message"));
//                this.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//            } else {
//                this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message") + " " + options.getString("url"));
//            }
//        } else if (ShareIntent.hasValidKey("url", options)) {
//            ShareFile fileShare = getFileShare(options);
//            if(fileShare.isFile()) {
//                Uri uriFile = fileShare.getURI();
//                this.getIntent().setType(fileShare.getType());
//                this.getIntent().putExtra(Intent.EXTRA_STREAM, uriFile);
//                this.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//            } else {
//                this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("url"));
//            }
//        } else if (ShareIntent.hasValidKey("message", options) ) {
//            this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message"));
//        }
    }
    protected ShareFile getFileShare(ReadableMap options) {
        if (ShareIntent.hasValidKey("type", options)) {
            return new ShareFile(options.getString("url"), options.getString("type"), this.reactContext);
        } else {
            return new ShareFile(options.getString("url"), this.reactContext);
        }
    }
    protected static String urlEncode(String param) {
        try {
            return URLEncoder.encode( param , "UTF-8" );
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("URLEncoder.encode() failed for " + param);
        }
    }
    protected void openIntentChooser() throws ActivityNotFoundException {
        System.out.println(this.getIntent());
        System.out.println(this.getIntent().getExtras());
        Intent chooser = Intent.createChooser(this.getIntent(), this.chooserTitle);
        chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.reactContext.startActivity(chooser);
    }
    protected boolean isPackageInstalled(String packagename, Context context) {
        PackageManager pm = context.getPackageManager();
        try {
            pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    protected Intent getIntent(){
        return this.intent;
    }
    protected void setIntent(Intent intent) {
        this.intent = intent;
    }
    public static boolean hasValidKey(String key, ReadableMap options) {
        return options.hasKey(key) && !options.isNull(key);
    }
    protected abstract String getPackage();
    protected abstract String getDefaultWebLink();
    protected abstract String getPlayStoreLink();
}

@@ -930,18 +930,16 @@
 ption %7B%0A
-//
         
@@ -982,34 +982,32 @@
 %22, options) ) %7B%0A
-//
             this
@@ -1077,26 +1077,24 @@
 subject%22));%0A
-//
         %7D%0A//
@@ -1095,13 +1095,9 @@
   %7D%0A
-//%0A//
+%0A
     
@@ -1186,34 +1186,32 @@
 l%22, options)) %7B%0A
-//
             Shar
@@ -1243,34 +1243,32 @@
 Share(options);%0A
-//
             if(f
@@ -1280,34 +1280,32 @@
 are.isFile()) %7B%0A
-//
                 
@@ -1330,34 +1330,32 @@
 Share.getURI();%0A
-//
                 
@@ -1393,34 +1393,32 @@
 are.getType());%0A
-//
                 
@@ -1466,34 +1466,32 @@
 REAM, uriFile);%0A
-//
                 
@@ -1558,34 +1558,32 @@
 ng(%22message%22));%0A
-//
                 
@@ -1640,34 +1640,32 @@
 RI_PERMISSION);%0A
-//
             %7D el
@@ -1661,34 +1661,32 @@
        %7D else %7B%0A
-//
                 
@@ -1786,34 +1786,32 @@
 String(%22url%22));%0A
-//
             %7D%0A//
@@ -1800,34 +1800,32 @@
 ;%0A            %7D%0A
-//
         %7D else i
@@ -1866,26 +1866,24 @@
 options)) %7B%0A
-//
             
@@ -1927,18 +1927,16 @@
 tions);%0A
-//
         
@@ -1960,26 +1960,24 @@
 isFile()) %7B%0A
-//
             
@@ -2014,18 +2014,16 @@
 tURI();%0A
-//
         
@@ -2073,26 +2073,24 @@
 getType());%0A
-//
             
@@ -2150,18 +2150,16 @@
 iFile);%0A
-//
         
@@ -2232,18 +2232,16 @@
 SSION);%0A
-//
         
@@ -2253,18 +2253,16 @@
  else %7B%0A
-//
         
@@ -2337,26 +2337,24 @@
 ng(%22url%22));%0A
-//
             
@@ -2355,18 +2355,16 @@
       %7D%0A
-//
         
@@ -2422,18 +2422,16 @@
 ns) ) %7B%0A
-//
         
@@ -2514,10 +2514,8 @@
 ));%0A
-//
     

Update package