package com.outsystems.plugins.appfeedback;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.WebView;

import com.outsystems.android.mobileect.MobileECTController;
import com.outsystems.android.mobileect.api.interfaces.OSECTProviderAPIHandler;
import com.outsystems.android.mobileect.interfaces.OSECTContainerListener;

import org.apache.cordova.CallbackContext;

public class OSAppFeedbackListener implements OSECTContainerListener {

    private final String SHAREDPREF_NAME = "com.outsystems.plugins.appfeedback.preferences";
    private final String SHAREDPREF_KEY_SKIP_HELP = "com.outsystems.plugins.appfeedback.key.skiphelp";

    private MobileECTController mobileECTController;

    private boolean appFeedbackAvailable;

    private Context context;

    public OSAppFeedbackListener(Activity activity, ViewGroup mainViewGroup, ViewGroup ectViewGroup, WebView currentWebView, String hostname) {

        this.context = activity;

        SharedPreferences sharedPref = context.getSharedPreferences(SHAREDPREF_NAME, Context.MODE_PRIVATE);
        boolean skipHelp = sharedPref.getBoolean(SHAREDPREF_KEY_SKIP_HELP, false);

        this.mobileECTController = new MobileECTController(activity,
                mainViewGroup,
                ectViewGroup,
                currentWebView,
                hostname,
                skipHelp,
                this);
    }

    @Override
    public void onSendFeedbackClickListener() {
        mobileECTController.sendFeedback();
    }

    @Override
    public void onCloseECTClickListener() {
        mobileECTController.closeECTNativeUI();
    }

    @Override
    public void onCloseECTHelperClickListener() {
        mobileECTController.setSkipECTHelper(true);
    }


    public boolean isAppFeedbackAvailable() {
        return appFeedbackAvailable;
    }


    public void openECT() {
        mobileECTController.openECTNativeUI();
        SharedPreferences sharedPref = this.context.getSharedPreferences(SHAREDPREF_NAME, Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(SHAREDPREF_KEY_SKIP_HELP, true);
        editor.commit();
    }

    /**
     * Public API for Plugin
     */

    public void handleDeviceReady(){
        this.mobileECTController.prepareECTData(new OSECTProviderAPIHandler() {
            @Override
            public void execute(boolean result) {
                // Do Nothing
            }
        });
    }

    private boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

    public void handleECTAvailable(final OSECTProviderAPIHandler apiHandler) {
        mobileECTController.checkECTAvailability(new OSECTProviderAPIHandler() {
            @Override
            public void execute(boolean result) {
                if(isNetworkAvailable(context)){
                    appFeedbackAvailable = result;
                    if(apiHandler != null)
                        apiHandler.execute(result);
                }
                else{
                    AlertDialog.Builder alert = new AlertDialog.Builder(context);
                    alert.setTitle("Can't send feedback");
                    alert.setMessage("Make sure your device has internet connection and try again.");
                    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });

                    alert.show();
                }
            }
        });
    }


    public void handleOpenECT(final OSECTProviderAPIHandler apiHandler) {
        if (this.isAppFeedbackAvailable()) {
            this.openECT();
            if(apiHandler != null) {
                apiHandler.execute(true);
            }
        } else {
            if(apiHandler != null) {
                apiHandler.execute(false);
            }
        }
    }

}

@@ -3245,32 +3245,79 @@
  apiHandler) %7B%0D%0A
+        if(isNetworkAvailable(context)) %7B%0D%0A    
         mobileEC
@@ -3386,32 +3386,36 @@
  %7B%0D%0A            
+    
 @Override%0D%0A     
@@ -3413,32 +3413,36 @@
 de%0D%0A            
+ 
+   
 public void exec
@@ -3468,58 +3468,8 @@
  %7B%0D%0A
-                if(isNetworkAvailable(context))%7B%0D%0A
     
@@ -3648,16 +3648,28 @@
     
-   
+%7D);%0D%0A        %7D
  else
+ 
 %7B%0D%0A 
@@ -3679,24 +3679,16 @@
         
- 
-       
 AlertDia
@@ -3738,32 +3738,24 @@
 (context);%0D%0A
-        
             
@@ -3802,32 +3802,24 @@
             
-        
 alert.setMes
@@ -3897,32 +3897,24 @@
             
-       
- 
 alert.setPos
@@ -3984,32 +3984,24 @@
             
-        
 @Override%0D%0A 
@@ -4011,32 +4011,24 @@
             
-      
-  
 public void 
@@ -4093,24 +4093,16 @@
         
-        
 dialog.d
@@ -4128,35 +4128,19 @@
         
-    
-    %7D%0D%0A        
+%7D%0D%0A
         
@@ -4158,32 +4158,24 @@
             
-   
-     
 alert.show()
@@ -4189,54 +4189,16 @@
     
-        %7D%0D%0A            %7D%0D%0A        %7D);%0D%0A    %7D%0D%0A
+%7D%0D%0A    %7D
 %0D%0A%0D%0A

Fix isNetworkAvailable condition location