package org.deke.risk.riskahead.helper;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;

import org.deke.risk.riskahead.R;

/**
 *
 * (C) Copyright 2016 RiskAhead (http://www.riskahead.net)
 * Developer & Contributor: Dennis Thießen (dennis.thiessen@riskahead.net)
 * Date of Release: 10.02.2016
 *
 */
public class AppRater {

    private final static int DAYS_UNTIL_PROMPT = 1;
    private final static int MILLIS_UNTIL_PROMPT = DAYS_UNTIL_PROMPT * 24 * 60 * 60 * 1000;
    private final static int LAUNCHES_UNTIL_PROMPT = 10;

    public static void startAppRater(Context mContext) {
        SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
        if (prefs.getBoolean("dontshowagain", false)) { return; }

        SharedPreferences.Editor editor = prefs.edit();

        // Increment launch counter
        long launch_count = prefs.getLong("launch_count", 0) + 1;
        editor.putLong("launch_count", launch_count);

        // Get date of first launch
        Long date_firstLaunch = prefs.getLong("date_firstlaunch", 0);
        if (date_firstLaunch == 0) {
            date_firstLaunch = System.currentTimeMillis();
            editor.putLong("date_firstlaunch", date_firstLaunch);
        }

        // Wait at least n days before opening
        if (launch_count >= LAUNCHES_UNTIL_PROMPT &&
                System.currentTimeMillis() >= MILLIS_UNTIL_PROMPT) {
            showRateDialog(mContext, editor);
        }else{
            editor.apply();
        }
    }

    public static void showRateDialog(final Context mContext, final SharedPreferences.Editor editor) {
        AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
        alert.setTitle(R.string.txt_rate_title);
        alert.setMessage(R.string.txt_rate_message);

        alert.setIcon(R.drawable.logo_380);
        alert.setCancelable(true);
        alert.setPositiveButton(R.string.txt_rate_retenowbtn,
                (dialog, which) -> mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + AppConfig.APP_PNAME))));
        alert.setNeutralButton(R.string.txt_rate_laterbtn,
                (dialog, which) -> {
                    SharedPreferences prefs = mContext.getSharedPreferences("apprater", 0);
                    SharedPreferences.Editor editor1 = prefs.edit();

                    Long date_firstLaunch = System.currentTimeMillis();
                    editor1.putLong("date_firstlaunch", date_firstLaunch);
                    editor1.apply();
                });
        alert.setNegativeButton(R.string.txt_rate_neverbtn,
                (dialog, which) -> {
                    if (editor != null) {
                        editor.putBoolean("dontshowagain", true);
                        editor.commit();
                    }
                });

        AlertDialog ad = alert.create();
        ad.show();

    }
}
