package com.base.datarecovery.ads;

import android.content.SharedPreferences;

import com.base.datarecovery.help.BaseApplication;

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 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 AdDisplayUtils instance; // 单例对象
    private int adDisplayCount = 0; // 当前广告展示次数
    private int adClickCount = 0; // 当前广告点击次数
    private int maxAdDisplayCount; // 总广告展示次数限制
    private int maxAdClickCount; // 单个广告点击次数限制
    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);
        adDisplayCount = prefs.getInt(getAdDisplayCountKey(), 0);
        adClickCount = prefs.getInt(getAdClickCountKey(), 0);
    }

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

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

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

    public boolean shouldShowAd() {
        return shouldDisplayAd() || shouldIncrementClickCount();
    }

    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 setAdClickCount(int s) {
        if (!currentDate.equals(getCurrentDate())) {
            currentDate = getCurrentDate();
            adClickCount = 0;
        }
        adClickCount = s;
        saveAdClickCount();
    }

    private 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();
    }
}
