package com.reactlibrarynetperform;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.vodafone.netperform.NetPerformContext;
import com.vodafone.netperform.NetPerformStateListener;
import com.vodafone.netperform.data.DataRequest;
import static android.support.v4.app.ActivityCompat.requestPermissions;

public class NetPerformModule extends ReactContextBaseJavaModule implements NetPerformStateListener {
    private DataRequest dataRequest;
    int PERMISSION_ALL = 1;
    String userNumber = "";
    NetPerformContext netPerformContext=null;
    private final ReactApplicationContext reactContext;

    public NetPerformModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "NetPerform";
    }

    @ReactMethod
    public void initNetPerform(String userNumber,Callback callback) {
        // TODO: Implement some actually useful functionality
        try {
            this.userNumber = userNumber;
            // NetPerform SDK: Create NetPerform with NetPerform modules according to the configuration file.
            if (BuildConfig.DEBUG) {
                netPerformContext = new NetPerformContext(
                        this.reactContext,
                        NetPerformContext.NetPerformEnvironment.PRE_PRODUCTION
                );
            }else {
                netPerformContext = new NetPerformContext(
                        this.reactContext,
                        NetPerformContext.NetPerformEnvironment.PRODUCTION
                );
            }
            netPerformContext.init();
            startNetPerform(this.userNumber,callback);

        } catch (Exception e) {
            e.printStackTrace();
            callback.invoke("Exceptions ");
        }
    }

    @ReactMethod
    public boolean isNetPerfomInitialised() {
        if (this.netPerformContext != null){
            return true;
        }else{
            return false;
        }
    }

    private void showUsageAccessPermission() {
        new AlertDialog.Builder(reactContext.getCurrentActivity())
                .setTitle("Usage access permission")
                .setMessage("Please enable app usage access for this application from your device settings.")

                // Specifying a listener allows you to take an action before dismissing the dialog.
                // The dialog is automatically dismissed when a dialog button is clicked.
                .setPositiveButton("Go to Settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // Continue with delete operation
                        Intent settingIntent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
                        settingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        reactContext.startActivity(settingIntent);
                    }
                })

                // A null listener allows the button to dismiss the dialog and take no further action.
                .setNegativeButton("Cancel", null)
                .show();

    }

    public void startNetPerform(String number,Callback callback) {
        // TODO: Implement some actually useful functionality

        if (this.userNumber == ""){
            this.userNumber = number;
        }
        if (NetPerformContext.Permissions.hasRequiredPermissionsGranted()){
            if (NetPerformContext.Permissions.isUsageAccessPermissionRequired()) {
                showUsageAccessPermission();
                callback.invoke("isUsageAccessPermissionRequired ");

            }else{
                NetPerformContext.startPersonalized(this,this.userNumber);
                callback.invoke("Initialized ");
            }

        }else{
            String[] PERMISSIONS = NetPerformContext.Permissions.getRequiredPermissionsNotGranted();
            requestPermissions(reactContext.getCurrentActivity(), PERMISSIONS, PERMISSION_ALL);
            callback.invoke("Permissions not granted ");
        }

    }

    @ReactMethod
    public void stopNetPerform(){
        NetPerformContext.stopPersonalized(this);
    }


    @Override
    public void onStarted() {
        Log.d("NetPerform", "onStarted: ");
    }

    @Override
    public void onStopped() {
        Log.d("NetPerform", "onStopped: ");

    }

    @Override
    public void onPersonalizedStarted() {
        Log.d("NetPerform", "onPersonalizedStarted: ");

    }

    @Override
    public void onPersonalizedStopped() {
        Log.d("NetPerform", "onPersonalizedStopped: ");

    }

    @Override
    public void onPersonalizationUpdated() {
        Log.d("NetPerform", "onPersonalizationUpdated: ");
    }

    @Override
    public void onError(Error error) {
        Log.d("NetPerform", "onError: "+error);


    }
}
