package com.base.pdfviewerscannerwhite.ads

import com.base.pdfviewerscannerwhite.ads.AdmobHelper.inter_limit_click
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.inter_limit_request
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.inter_limit_show
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.native_limit_request
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.native_limit_show
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.open_limit_click
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.open_limit_request
import com.base.pdfviewerscannerwhite.ads.AdmobHelper.open_limit_show
import com.base.pdfviewerscannerwhite.helper.EventUtils
import com.base.pdfviewerscannerwhite.utils.AppPreferences
import com.base.pdfviewerscannerwhite.utils.LogEx
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale

object AdDisplayUtils {


    //region open
    private val open_max_request = AppPreferences.getInstance().getString(open_limit_request, "15").toInt()
    private val open_max_show = AppPreferences.getInstance().getString(open_limit_show, "10").toInt()
    private val open_max_click = AppPreferences.getInstance().getString(open_limit_click, "1").toInt()

    fun incrementOpenRequestCount() {
        currentOpenRequest += 1
    }

    fun incrementOpenShow() {
        currentOpenShow += 1
    }

    fun incrementClickShow() {
        currentOpenClick += 1
    }


    //当前开屏请求次数
    private var currentOpenRequest = 0
        get() {
            return AppPreferences.getInstance().getInt("currentOpenRequest_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentOpenRequest_${currentDate()}", value, true)
        }

    //当前开屏展示次数
    private var currentOpenShow = 0
        get() {
            return AppPreferences.getInstance().getInt("currentOpenShow_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentOpenShow_${currentDate()}", value, true)
        }

    //当前开屏点击次数
    private var currentOpenClick = 0
        get() {
            return AppPreferences.getInstance().getInt("currentOpenClick_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentOpenClick_${currentDate()}", value, true)
        }

    fun shouldShowOpenAd(): Boolean {

        if (currentOpenRequest > open_max_request) {
            LogEx.logDebug(TAG, "currentOpenRequest=$currentOpenRequest open_max_request=$open_max_request")
            EventUtils.event("ad_limit", "currentOpenRequest=$currentOpenRequest open_max_request=$open_max_request")
            return false
        }
        if (currentOpenShow > open_max_show) {
            LogEx.logDebug(TAG, "currentOpenShow=$currentOpenShow open_max_show=$open_max_show")
            EventUtils.event("ad_limit", "currentOpenShow=$currentOpenShow open_max_show=$open_max_show")
            return false
        }
        if (currentOpenClick > open_max_click) {
            LogEx.logDebug(TAG, "currentOpenClick=$currentOpenClick open_max_click=$open_max_click")
            EventUtils.event("ad_limit", "currentOpenClick=$currentOpenClick open_max_click=$open_max_click")
            return false
        }

        return true
    }
    //endregion

    //region inter

    private val inter_max_request = AppPreferences.getInstance().getString(inter_limit_request, "15").toInt()
    private val inter_max_show = AppPreferences.getInstance().getString(inter_limit_show, "10").toInt()
    private val inter_max_click = AppPreferences.getInstance().getString(inter_limit_click, "1").toInt()
    fun incrementInterRequestCount() {
        currentInterRequest += 1
    }

    fun incrementInterShowCount() {
        currentInterShow += 1
    }

    fun incrementInterClickCount() {
        currentInterClick += 1
    }

    //当前插页请求次数
    private var currentInterRequest = 0
        get() {
            return AppPreferences.getInstance().getInt("currentInterRequest_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentInterRequest_${currentDate()}", value, true)
        }

    //当前插页展示次数
    private var currentInterShow = 0
        get() {
            return AppPreferences.getInstance().getInt("currentInterShow_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentInterShow_${currentDate()}", value, true)
        }

    //当前插页点击次数
    private var currentInterClick = 0
        get() {
            return AppPreferences.getInstance().getInt("currentInterClick_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentInterClick_${currentDate()}", value, true)
        }


    fun shouldShowInterAd(): Boolean {
        if (currentInterRequest > inter_max_request) {
            LogEx.logDebug(TAG, "currentInterRequest=$currentInterRequest inter_max_request=$inter_max_request")
            EventUtils.event("ad_limit", "currentInterRequest=$currentInterRequest inter_max_request=$inter_max_request")
            return false
        }
        if (currentInterShow > inter_max_show) {
            LogEx.logDebug(TAG, "currentInterShow=$currentInterShow inter_max_show=$inter_max_show")
            EventUtils.event("ad_limit", "currentInterShow=$currentInterShow inter_max_show=$inter_max_show")
            return false
        }
        if (currentInterClick > inter_max_click) {
            LogEx.logDebug(TAG, "currentInterClick=$currentInterClick inter_max_click=$inter_max_click")
            EventUtils.event("ad_limit", "currentInterClick=$currentInterClick inter_max_click=$inter_max_click")
            return false
        }
        return true
    }
    //endregion

    //region native
    private val native_max_request = AppPreferences.getInstance().getString(native_limit_request, "15").toInt()
    private val native_max_show = AppPreferences.getInstance().getString(native_limit_show, "10").toInt()
    private val native_max_click = AppPreferences.getInstance().getString(native_limit_show, "1").toInt()

    fun incrementNativeRequestCount() {
        currentNativeRequest += 1
    }

    fun incrementNativeShowCount() {
        currentNativeShow += 1
    }

    fun incrementNativeClickCount() {
        currentNativeClick += 1
    }

    private var currentNativeRequest = 0
        get() {
            return AppPreferences.getInstance().getInt("currentNativeRequest_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentNativeRequest_${currentDate()}", value, true)
        }
    private var currentNativeShow = 0
        get() {
            return AppPreferences.getInstance().getInt("currentNativeShow_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentNativeShow_${currentDate()}", value, true)
        }

    private var currentNativeClick = 0
        get() {
            return AppPreferences.getInstance().getInt("currentNativeClick_${currentDate()}", field)
        }
        set(value) {
            field = value
            AppPreferences.getInstance().put("currentNativeClick_${currentDate()}", value, true)
        }


    fun shouldShowNative(): Boolean {
        if (currentNativeRequest > native_max_request) {
            LogEx.logDebug(TAG, "currentNativeRequest=$currentNativeRequest native_max_request=$native_max_request")
            EventUtils.event("ad_limit", "currentNativeRequest=$currentNativeRequest native_max_request=$native_max_request")
            return false
        }
        if (currentNativeShow > native_max_show) {
            LogEx.logDebug(TAG, "currentNativeShow=$currentNativeShow native_max_show=$native_max_show")
            EventUtils.event("ad_limit", "currentNativeShow=$currentNativeShow native_max_show=$native_max_show")
            return false
        }
        if (currentNativeClick > native_max_click) {
            LogEx.logDebug(TAG, "currentNativeClick=$currentNativeClick native_max_click=$native_max_click")
            EventUtils.event("ad_limit", "currentNativeClick=$currentNativeClick native_max_click=$native_max_click")
            return false
        }
        return true
    }

    //endregion

    private val TAG = "AdDisplayUtils"
    private fun currentDate(): String {
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val currentDate = Calendar.getInstance().time
        return dateFormat.format(currentDate)
    }

}
