package com.base.datarecovery.ads;

import android.content.SharedPreferences;
import android.util.Log;

import com.base.datarecovery.BuildConfig;
import com.base.datarecovery.MyApplication;
import com.base.datarecovery.help.BaseApplication;
import com.base.datarecovery.utils.EventUtils;
import com.base.datarecovery.utils.LogEx;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class AdDisplayUtils {
    private static final int DEFAULT_MAX_AD_DISPLAY_COUNT = 45; // 总广告展示次数限制默认值
    private static final int DEFAULT_MAX_AD_CLICK_COUNT = 10; // 单个广告点击次数限制默认值
    private static final int DEFAULT_MAX_AD_REQUEST_FAIL_COUNT = 20; // 单个广告点击次数限制默认值
    private static final String AD_PREFS_NAME = "ad_prefs"; // SharedPreferences 名称
    private static final String AD_DISPLAY_COUNT_KEY = "ad_display_count"; // 广告展示次数的键
    private static final String AD_CLICK_COUNT_KEY = "ad_click_count"; // 广告点击次数的键
    private static final String MAX_AD_DISPLAY_COUNT_KEY = "max_ad_display_count"; // 总广告展示次数限制的键
    private static final String MAX_AD_CLICK_COUNT_KEY = "max_ad_click_count"; // 单个广告点击次数限制的键
    private static final String MAX_AD_REQUEST_FAIL_COUNT_KEY = "max_ad_request_fail_count"; // 单个广告点击次数限制的键
    private static final String AD_REQUEST_FAIL_COUNT_KEY = "ad_request_fail_count"; // 单个广告点击次数限制的键

    private static AdDisplayUtils instance; // 单例对象
    private int adDisplayCount = 0; // 当前广告展示次数
    private int adClickCount = 0; // 当前广告点击次数
    private int maxAdDisplayCount; // 总广告展示次数限制
    private int maxAdClickCount; // 单个广告点击次数限制

    private int maxAdRequestFailCount; // 请求失败总
    private int adRequestFailCount = 0; // 请求失败当前
    private String currentDate; // 当前日期


    private AdDisplayUtils() {
        currentDate = getCurrentDate();
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        maxAdDisplayCount = prefs.getInt(MAX_AD_DISPLAY_COUNT_KEY, DEFAULT_MAX_AD_DISPLAY_COUNT);
        maxAdClickCount = prefs.getInt(MAX_AD_CLICK_COUNT_KEY, DEFAULT_MAX_AD_CLICK_COUNT);
        maxAdRequestFailCount = prefs.getInt(MAX_AD_REQUEST_FAIL_COUNT_KEY, DEFAULT_MAX_AD_REQUEST_FAIL_COUNT);
        adDisplayCount = prefs.getInt(getAdDisplayCountKey(), 0);
        adClickCount = prefs.getInt(getAdClickCountKey(), 0);
        adRequestFailCount = prefs.getInt(getAdRequestFailCountKey(), 0);
    }

    public static synchronized AdDisplayUtils getInstance() {
        if (instance == null) {
            instance = new AdDisplayUtils();
        }
        return instance;
    }

    public boolean shouldDisplayAd() {
        return adDisplayCount < getMaxAdDisplayCount();
    }

    public boolean shouldIncrementRequestFailAd() {
        return adRequestFailCount < getMaxAdRequestFailCount();
    }

    public boolean shouldIncrementClickCount() {
        return adClickCount < getMaxAdClickCount();
    }

    public boolean shouldShowAd(String ad_unit) {
        if (BuildConfig.DEBUG) {
            return true;
        }
        boolean s = shouldDisplayAd() && shouldIncrementClickCount() && shouldIncrementRequestFailAd();
        if (!s) {
            LogEx.INSTANCE.logDebug("glc", "!shouldShowAd", false);
            JSONObject obj2 = new JSONObject();
            try {
                obj2.put("reason", "no ad");
                obj2.put("ad_unit", ad_unit);
                EventUtils.INSTANCE.event("ad_show_error", null, obj2, false);
            } catch (JSONException e) {

            }
        }
        return s;
    }

    public void incrementAdDisplayCount() {
        if (!currentDate.equals(getCurrentDate())) {
            currentDate = getCurrentDate();
            adDisplayCount = 0;
        }
        adDisplayCount++;
        saveAdDisplayCount();
    }

    public void incrementAdClickCount() {
        if (!currentDate.equals(getCurrentDate())) {
            currentDate = getCurrentDate();
            adClickCount = 0;
        }
        adClickCount++;
        saveAdClickCount();
    }

    public void incrementAdRequestFailCount() {
        if (!currentDate.equals(getCurrentDate())) {
            currentDate = getCurrentDate();
            adRequestFailCount = 0;
        }
        adRequestFailCount++;
        saveAdRequestFailCount();
        Log.d("glc", "广告请求失败：" + adRequestFailCount);
    }


    public void setAdClickCount(int s) {
        if (!currentDate.equals(getCurrentDate())) {
            currentDate = getCurrentDate();
            adClickCount = 0;
        }
        adClickCount = s;
        saveAdClickCount();
    }

    public String getCurrentDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        Date currentDate = Calendar.getInstance().getTime();
        return dateFormat.format(currentDate);
    }

    private String getAdDisplayCountKey() {
        return AD_DISPLAY_COUNT_KEY + "_" + currentDate;
    }

    private String getAdClickCountKey() {
        return AD_CLICK_COUNT_KEY + "_" + currentDate;
    }

    private void saveAdDisplayCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(getAdDisplayCountKey(), adDisplayCount);
        editor.apply();
    }

    private void saveAdClickCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(getAdClickCountKey(), adClickCount);
        editor.apply();
    }

    private int getMaxAdDisplayCount() {
        return maxAdDisplayCount;
    }

    public void setMaxAdDisplayCount(int maxAdDisplayCount) {
        this.maxAdDisplayCount = maxAdDisplayCount;
        saveMaxAdDisplayCount();
    }

    public int getMaxAdClickCount() {
        return maxAdClickCount;
    }

    public void setMaxAdClickCount(int maxAdClickCount) {
        this.maxAdClickCount = maxAdClickCount;
        saveMaxAdClickCount();
    }

    private void saveMaxAdDisplayCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(MAX_AD_DISPLAY_COUNT_KEY, maxAdDisplayCount);
        editor.apply();
    }

    private void saveMaxAdClickCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(MAX_AD_CLICK_COUNT_KEY, maxAdClickCount);
        editor.apply();
    }

    public int getMaxAdRequestFailCount() {
        return maxAdRequestFailCount;
    }

    public void setMaxAdRequestFailCount(int maxAdRequestFailCount) {
        this.maxAdRequestFailCount = maxAdRequestFailCount;
        saveMaxAdRequestFailCount();
    }

    private void saveMaxAdRequestFailCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(MAX_AD_REQUEST_FAIL_COUNT_KEY, maxAdRequestFailCount);
        editor.apply();
    }

    private void saveAdRequestFailCount() {
        SharedPreferences prefs = BaseApplication.context.getSharedPreferences(AD_PREFS_NAME, 0);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(getAdRequestFailCountKey(), adRequestFailCount);
        editor.apply();
    }

    private String getAdRequestFailCountKey() {
        return AD_REQUEST_FAIL_COUNT_KEY + "_" + currentDate;
    }
}
