package hotchemi.android.rate;

import android.app.Activity;
import android.content.Context;
import android.view.View;

import java.util.Date;

public class AppRate {

    private static final AppRate SINGLETON = new AppRate();

    private int installDate = 10;

    private int launchTimes = 10;

    private int remindInterval = 1;

    private int eventsTimes = -1;

    private boolean isShowNeutralButton = true;

    private boolean isDebug = false;

    private View view;

    private OnClickButtonListener listener;

    private AppRate() {
    }

    public static AppRate getInstance() {
        return SINGLETON;
    }

    public AppRate setLaunchTimes(int launchTimes) {
        this.launchTimes = launchTimes;
        return this;
    }

    public AppRate setInstallDays(int installDate) {
        this.installDate = installDate;
        return this;
    }

    public AppRate setRemindInterval(int remindInterval) {
        this.remindInterval = remindInterval;
        return this;
    }

    public AppRate setShowNeutralButton(boolean isShowNeutralButton) {
        this.isShowNeutralButton = isShowNeutralButton;
        return this;
    }

    public AppRate setEventsTimes(int eventsTimes) {
        this.eventsTimes = eventsTimes;
        return this;
    }

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

    public AppRate setDebug(boolean isDebug) {
        this.isDebug = isDebug;
        return this;
    }

    public AppRate setView(View view) {
        this.view = view;
        return this;
    }

    public AppRate setOnClickButtonListener(OnClickButtonListener listener) {
        this.listener = listener;
        return this;
    }

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

    public static boolean showRateDialogIfMeetsConditions(Activity activity) {
        if (SINGLETON.isDebug || SINGLETON.shouldShowRateDialog(activity)) {
            SINGLETON.showRateDialog(activity);
            return true;
        }
        return false;
    }

    public static boolean passSignificantEvent(Activity activity) {
        if (SINGLETON.isDebug || SINGLETON.isOverEventPass(activity.getApplicationContext())) {
            SINGLETON.showRateDialog(activity);
            return true;
        } else {
            Context context = activity.getApplicationContext();
            int eventTimes = PreferenceHelper.getEventTimes(context);
            PreferenceHelper.setEventTimes(context, ++eventTimes);
            return false;
        }
    }

    public void showRateDialog(Activity activity) {
        DialogManager.create(activity, isShowNeutralButton, listener, view).show();
    }

    public boolean isOverEventPass(Context context) {
        return eventsTimes != -1 && PreferenceHelper.getEventTimes(context) > eventsTimes;
    }

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

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

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

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

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

}
@@ -184,22 +184,16 @@
 tic 
-final 
 AppRate 
 SING
@@ -192,33 +192,17 @@
 ate 
-SINGLETON = new AppRate()
+singleton
 ;%0A%0A 
@@ -425,24 +425,54 @@
 g = false;%0A%0A
+    private Context context;%0A%0A
     private 
@@ -552,85 +552,377 @@
 ate(
-) %7B%0A    %7D%0A%0A    public static AppRate getInstance() %7B%0A        return SINGLETON
+Context context) %7B%0A        this.context = context.getApplicationContext();%0A    %7D%0A%0A    public static AppRate with(Context context) %7B%0A        if (singleton == null) %7B%0A            synchronized (AppRate.class) %7B%0A                if (singleton == null) %7B%0A                    singleton = new AppRate(context);%0A                %7D%0A            %7D%0A        %7D%0A        return singleton
 ;%0A  
@@ -1613,39 +1613,24 @@
 eShowDialog(
-Context context
 ) %7B%0A        
@@ -2074,31 +2074,16 @@
 monitor(
-Context context
 ) %7B%0A    
@@ -2387,33 +2387,33 @@
         if (
-SINGLETON
+singleton
 .isDebug %7C%7C 
@@ -2412,25 +2412,25 @@
 ebug %7C%7C 
-SINGLETON
+singleton
 .shouldS
@@ -2443,24 +2443,16 @@
 eDialog(
-activity
 )) %7B%0A   
@@ -2456,33 +2456,33 @@
             
-SINGLETON
+singleton
 .showRateDia
@@ -2640,25 +2640,25 @@
     if (
-SINGLETON
+singleton
 .isDebug
@@ -2661,25 +2661,25 @@
 ebug %7C%7C 
-SINGLETON
+singleton
 .isOverE
@@ -2691,40 +2691,8 @@
 ass(
-activity.getApplicationContext()
 )) %7B
@@ -2708,17 +2708,17 @@
     
-SINGLETON
+singleton
 .sho
@@ -3204,31 +3204,16 @@
 entPass(
-Context context
 ) %7B%0A    
@@ -3346,31 +3346,16 @@
 eDialog(
-Context context
 ) %7B%0A    
@@ -3449,23 +3449,16 @@
 chTimes(
-context
 ) &&%0A   
@@ -3488,23 +3488,16 @@
 allDate(
-context
 ) &&%0A   
@@ -3522,31 +3522,24 @@
 rRemindDate(
-context
 );%0A    %7D%0A%0A  
@@ -3574,31 +3574,16 @@
 chTimes(
-Context context
 ) %7B%0A    
@@ -3691,39 +3691,24 @@
 InstallDate(
-Context context
 ) %7B%0A        
@@ -3821,39 +3821,24 @@
 rRemindDate(
-Context context
 ) %7B%0A        

change interface like picasso.