package com.gh.gamecenter.home.custom.model

import android.content.Context
import com.gh.gamecenter.core.utils.SPUtils
import com.gh.gamecenter.feature.entity.FloatingWindowEntity
import com.halo.assistant.HaloApp
import java.text.SimpleDateFormat
import java.util.*

class CustomPageLocalDataSource {

    private val floatingSp by lazy {
        HaloApp.getInstance().getSharedPreferences(CUSTOM_FLOATING_WINDOW, Context.MODE_PRIVATE)
    }

    private val bigPopSp by lazy {
        HaloApp.getInstance().getSharedPreferences(CUSTOM_DIALOG_SHOW_BIG_IMAGE, Context.MODE_PRIVATE)
    }

    fun filterWindows(rawWindowList: List<FloatingWindowEntity>): List<FloatingWindowEntity> {
        // 过滤不需要显示的悬浮窗
        val targetList = rawWindowList.filter(::checkIfShowWindow)
        // 找到第一个需要自动弹出来大图的悬浮窗
        targetList.find {
            when (it.bigPopupNotice) {
                BIG_POPUP_NOTICE_NEVER -> {
                    it.autoExpandBigPopup = false
                }

                BIG_POPUP_NOTICE_ONCE -> {
                    val hasShow = bigPopSp.getBoolean(it.id, false)
                    if (hasShow) {
                        it.autoExpandBigPopup = false
                    } else {
                        it.autoExpandBigPopup = true
                        bigPopSp.edit().putBoolean(it.id, true).apply()
                    }
                }

                BIG_POPUP_NOTICE_FOLLOW_PUSH_TYPE -> {
                    it.autoExpandBigPopup = true
                }
            }
            it.autoExpandBigPopup
        }
        return targetList
    }


    private fun checkIfShowWindow(item: FloatingWindowEntity): Boolean {
        val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
        val currentTime = format.format(Calendar.getInstance().time)
        return when (item.pushType) {
            FLOATING_WINDOW_PUSH_TYPE_always -> {   // 常驻显示
                // 清除之前的弹出记录
                SPUtils.setString(item.spKey, "")
                true
            }

            FLOATING_WINDOW_PUSH_TYPE_ONCE -> { // 只显示一次
                // 只显示一次
                val value = SPUtils.getString(floatingSp, item.spKey)
                // value 为空，说明没有显示过，需要显示
                if (value.isEmpty()) {
                    SPUtils.setString(floatingSp, item.spKey, "$currentTime${DAY_AND_COUNT_SEPARATOR}1")
                }
                value.isEmpty()
            }

            else -> {   // 每天最多显示次数
                val value = SPUtils.getString(floatingSp, item.spKey)
                if (value.isEmpty()) {
                    SPUtils.setString(floatingSp, item.spKey, "$currentTime${DAY_AND_COUNT_SEPARATOR}1")
                    true
                } else {
                    val values = value.split(DAY_AND_COUNT_SEPARATOR)
                    val time = values.getOrNull(0) ?: ""
                    if (time == currentTime) {
                        val shouldShowCount = item.pushType.toIntOrNull() ?: 1
                        val hasShowCount = values.getOrNull(1)?.toIntOrNull() ?: 0
                        if (shouldShowCount > hasShowCount) {
                            SPUtils.setString(
                                floatingSp,
                                item.spKey,
                                "$time${DAY_AND_COUNT_SEPARATOR}${hasShowCount + 1}"
                            )
                        }
                        shouldShowCount > hasShowCount
                    } else {
                        SPUtils.setString(floatingSp, item.spKey, "$currentTime${DAY_AND_COUNT_SEPARATOR}1")
                        true
                    }
                }
            }
        }

    }

    private val FloatingWindowEntity.spKey: String
        get() = id

    companion object {
        private const val CUSTOM_FLOATING_WINDOW = "custom_floating_window"

        private const val FLOATING_WINDOW_PUSH_TYPE_ONCE = "once"
        private const val FLOATING_WINDOW_PUSH_TYPE_always = "always"

        private const val DAY_AND_COUNT_SEPARATOR = "_"

        private const val BIG_POPUP_NOTICE_NEVER = "NEVER"
        private const val BIG_POPUP_NOTICE_FOLLOW_PUSH_TYPE = "FOLLOW_PUSH_TYPE"
        private const val BIG_POPUP_NOTICE_ONCE = "ONCE"

        private const val CUSTOM_DIALOG_SHOW_BIG_IMAGE = "custom_dialog_show_big_image"
    }
}