package hotchemi.android.rate;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v4.app.FragmentActivity;

import java.util.Date;

public class AppRate {

    private static final AppRate SINGLETON = new AppRate();

    private static int sInstallDate = 10;

    private static int sLaunchTimes = 10;

    private static int sRemindInterval = 1;

    private static int sEventsTimes = -1;

    private static boolean sIsShowNeutralButton = true;

    private static boolean sIsDebug = false;

    private static OnClickButtonListener sListener;

    private AppRate() {
    }

    public static AppRate setLaunchTimes(int launchTimes) {
        sLaunchTimes = launchTimes;
        return SINGLETON;
    }

    public static AppRate setInstallDays(int installDays) {
        sInstallDate = installDays;
        return SINGLETON;
    }

    public static AppRate setRemindInterval(int remindInterval) {
        sRemindInterval = remindInterval;
        return SINGLETON;
    }

    public static AppRate setShowNeutralButton(boolean isShowNeutralButton) {
        sIsShowNeutralButton = isShowNeutralButton;
        return SINGLETON;
    }

    public static AppRate setEventsTimes(int eventsTimes) {
        sEventsTimes = eventsTimes;
        return SINGLETON;
    }

    public static AppRate clearAgreeShowDialog(Context context) {
        PreferenceHelper.setAgreeShowDialog(context, true);
        return SINGLETON;
    }

    public static AppRate setDebug(boolean isDebug) {
        sIsDebug = isDebug;
        return SINGLETON;
    }

    public static AppRate setOnClickButtonListener(OnClickButtonListener listener) {
        sListener = listener;
        return SINGLETON;
    }

    public static void monitor(Context context) {
        if (PreferenceHelper.isFirstLaunch(context)) {
            PreferenceHelper.setInstallDate(context);
        }
        PreferenceHelper.setLaunchTimes(context, PreferenceHelper.getLaunchTimes(context) + 1);
    }

    public static void showRateDialogIfMeetsConditions(Activity activity) {
        if (sIsDebug || shouldShowRateDialog(activity)) showRateDialog(activity);
    }

    public static void showRateDialogIfMeetsConditions(FragmentActivity activity) {
        if (sIsDebug || shouldShowRateDialog(activity)) showRateDialog(activity);
    }

    public static void passSignificantEvent(Activity activity) {
        if (sIsDebug || isOverEventPass(activity.getApplicationContext())) {
            showRateDialog(activity);
        } else {
            Context context = activity.getApplicationContext();
            int eventTimes = PreferenceHelper.getEventTimes(context);
            PreferenceHelper.setEventTimes(context, ++eventTimes);
        }
    }

    public static void passSignificantEvent(FragmentActivity activity) {
        if (sIsDebug || isOverEventPass(activity.getApplicationContext())) {
            showRateDialog(activity);
        } else {
            Context context = activity.getApplicationContext();
            int eventTimes = PreferenceHelper.getEventTimes(context);
            PreferenceHelper.setEventTimes(context, ++eventTimes);
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static void showRateDialog(Activity activity) {
        RateDialogFragment fragment = RateDialogFragment.getInstance(sIsShowNeutralButton, sListener);
        fragment.show(activity.getFragmentManager(), AppRate.class.getName());
    }

    public static void showRateDialog(FragmentActivity activity) {
        RateDialogSupportFragment fragment = RateDialogSupportFragment.newInstance(sIsShowNeutralButton, sListener);
        fragment.show(activity.getSupportFragmentManager(), AppRate.class.getName());
    }

    private static boolean isOverLaunchTimes(Context context) {
        return PreferenceHelper.getLaunchTimes(context) >= sLaunchTimes;
    }

    private static boolean isOverInstallDate(Context context) {
        return isOverDate(PreferenceHelper.getInstallDate(context), sInstallDate);
    }

    private static boolean isOverRemindDate(Context context) {
        return isOverDate(PreferenceHelper.getRemindInterval(context), sRemindInterval);
    }

    private static boolean isOverDate(long targetDate, int threshold) {
        return new Date().getTime() - targetDate >= threshold * 24 * 60 * 60 * 1000;
    }

    private static boolean isOverEventPass(Context context) {
        return sEventsTimes != -1 && PreferenceHelper.getEventTimes(context) > sEventsTimes;
    }

    private static boolean shouldShowRateDialog(Context context) {
        return PreferenceHelper.getIsAgreeShowDialog(context) &&
                isOverLaunchTimes(context) &&
                isOverInstallDate(context) &&
                isOverRemindDate(context);
    }

}
@@ -2599,34 +2599,31 @@
         
-Context context = 
+setEventsTimes(
 activity
@@ -2650,144 +2650,8 @@
 xt()
-;%0A            int eventTimes = PreferenceHelper.getEventTimes(context);%0A            PreferenceHelper.setEventTimes(context, ++eventTimes
 );%0A 
@@ -2887,26 +2887,23 @@
     
-Context context = 
+setEventsTimes(
 acti
@@ -2934,144 +2934,8 @@
 xt()
-;%0A            int eventTimes = PreferenceHelper.getEventTimes(context);%0A            PreferenceHelper.setEventTimes(context, ++eventTimes
 );%0A 
@@ -4578,9 +4578,203 @@
 %0A    %7D%0A%0A
+    private static void setEventsTimes(Context context) %7B%0A        int eventTimes = PreferenceHelper.getEventTimes(context);%0A        PreferenceHelper.setEventTimes(context, ++eventTimes);%0A    %7D%0A%0A
 %7D

create AppRate#setEventTImes method.