package li.doerf.hacked.ui;

import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;

import li.doerf.hacked.CustomEvent;
import li.doerf.hacked.HackedApplication;
import li.doerf.hacked.R;
import li.doerf.hacked.util.RatingHelper;

public class RateUsDialogFragment extends DialogFragment {

    private final String LOGTAG = getClass().getSimpleName();

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new AlertDialog.Builder(getContext())
                .setTitle(getString(R.string.rating_dialog_title))
                .setMessage(getString(R.string.rating_dialog_message))
                .setPositiveButton(getString(R.string.rating_dialog_positive), (dialog, which) -> handleClickPositive())
                .setNeutralButton(getString(R.string.rating_dialog_neutral), (dialog, which) -> handleClickNeutral())
                .setNegativeButton(getString(R.string.rating_dialog_negative), (dialog, which) -> handleClickNegative()).create();
    }

    private void handleClickPositive() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(
                Uri.parse(
                "https://play.google.com/store/apps/details?id=li.doerf.hacked"));
        intent.setPackage("com.android.vending");
        startActivity(intent);
        saveSettingRated();
    }

    private void saveSettingRated() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(RatingHelper.PREF_KEY_HAS_RATED_US, true);
        editor.apply();
        ((HackedApplication) getActivity().getApplication()).trackCustomEvent(CustomEvent.RATE_NOW);
        Log.i(LOGTAG, "setting: rated us - true");
    }

    private void handleClickNeutral() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(RatingHelper.PREF_KEY_RATING_COUNTER, 0);
        editor.apply();
        Log.i(LOGTAG, "setting: reset rating counter");
        ((HackedApplication) getActivity().getApplication()).trackCustomEvent(CustomEvent.RATE_LATER);
    }

    private void handleClickNegative() {
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getContext());
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(RatingHelper.PREF_KEY_RATING_NEVER, true);
        editor.apply();
        Log.i(LOGTAG, "setting: never rate");
        ((HackedApplication) getActivity().getApplication()).trackCustomEvent(CustomEvent.RATE_NEVER);
    }
}

@@ -41,32 +41,82 @@
 oid.app.Dialog;%0A
+import android.content.ActivityNotFoundException;%0A
 import android.c
@@ -126,24 +126,24 @@
 ent.Intent;%0A
-
 import andro
@@ -291,16 +291,45 @@
 til.Log;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -443,24 +443,69 @@
 gFragment;%0A%0A
+import com.crashlytics.android.Crashlytics;%0A%0A
 import li.do
@@ -1452,32 +1452,50 @@
 ickPositive() %7B%0A
+        try %7B%0A    
         Intent i
@@ -1534,32 +1534,36 @@
 _VIEW);%0A        
+    
 intent.setData(%0A
@@ -1578,16 +1578,20 @@
         
+ 
+   
 Uri.pars
@@ -1593,16 +1593,28 @@
 .parse(%0A
+            
         
@@ -1688,32 +1688,36 @@
 ked%22));%0A        
+    
 intent.setPackag
@@ -1750,16 +1750,20 @@
         
+   
+ 
 startAct
@@ -1785,16 +1785,20 @@
         
+    
 saveSett
@@ -1805,24 +1805,332 @@
 ingRated();%0A
+        %7D catch (ActivityNotFoundException e) %7B%0A            Log.e(LOGTAG, %22caught ActivityNotFoundException%22, e);%0A            Crashlytics.logException(e);%0A            Toast.makeText(getContext(), getString(R.string.unable_to_start_browser, %22https://haveibeenpwned.com%22), Toast.LENGTH_LONG).show();%0A        %7D%0A
     %7D%0A%0A    p

handle npe in RateUsDialogFragment