package com.hikapro.backpack.presenter.adapters.helper;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.util.Log;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.hikapro.backpack.App;
import com.hikapro.backpack.R;

/**
 * Created by tariel on 26/08/16.
 */
public class AppRater {

    private final static String APP_PNAME = "com.hikapro.backpack";
    private final static String APP_PREFERENCES = "backpack";
    private final static String DO_NOT_SHOW = "notshow";
    private final static String COUNT = "count";
    private final static String TRIGGER_COUNT = "triggercount";
    private final static int COUNT_UNTIL_FIRST_PROMPT = 2;


    public static boolean checkRatePrompt() {
        boolean ret = false;
        SharedPreferences preferences = App.getAppContext().getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
            if (preferences.getBoolean(DO_NOT_SHOW, false))
                ret = false;
            else if (preferences.getInt(COUNT, 1) == preferences.getInt(TRIGGER_COUNT, COUNT_UNTIL_FIRST_PROMPT))
                ret = true;
        return ret;
    }

    public static void updateSharedPreferences(int countToAdd) {
        SharedPreferences preferences = App.getAppContext().getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        int count = preferences.getInt(COUNT, 0) + countToAdd;
        editor.putInt(COUNT, count);
        editor.apply();

    }

    private static void later() {
        SharedPreferences preferences = App.getAppContext().getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        int triggerCount = preferences.getInt(TRIGGER_COUNT, COUNT_UNTIL_FIRST_PROMPT);
        triggerCount = triggerCount * 2 + triggerCount;
        editor.putInt(TRIGGER_COUNT, triggerCount);
        editor.apply();
    }

    private static void sure() {
        SharedPreferences preferences = App.getAppContext().getSharedPreferences(APP_PREFERENCES, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(DO_NOT_SHOW, true);
        editor.apply();
    }

    public static void showRateDialog(final Context context) {

        final Dialog dialog = new Dialog(context);

        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_rate);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        final Button later = (Button) dialog.findViewById(R.id.button_later);
        Button rate = (Button) dialog.findViewById(R.id.button_rate);

        later.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                try {
                    later();
                    dialog.dismiss();
                } catch (Exception e) {
                    Log.e("Rate dialog", "Error while closing", e);
                }
            }
        });
        rate.setOnClickListener(new android.view.View.OnClickListener() {
            @Override
            public void onClick(android.view.View v) {
                try {
                    context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + APP_PNAME)));
                    sure();
                    dialog.dismiss();
                } catch (Exception e) {
                    Log.e("Rate dialog", "Error while rating", e);
                }

            }
        });

        dialog.show();
    }

}
