package com.remax.notification.newUtil

import android.annotation.SuppressLint
import android.content.Context
import com.ama.core.architecture.BaseApp
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.ResUtil
import com.remax.notification.R
import com.remax.notification.builder.LANDING_NOTIFICATION_ACTION
import com.remax.notification.builder.LANDING_NOTIFICATION_FROM
import com.remax.notification.builder.NotificationType
import com.remax.notification.builder.entryPointPendingIntent
import com.remax.notification.builder.type2notificationId
import com.remax.notification.check.NotificationCheckController
import com.remax.notification.config.PushContent
import java.util.concurrent.ConcurrentHashMap



@SuppressLint("StaticFieldLeak")
object NotificationDatas {
    private lateinit var mCallback: IResidentDataCallback
    private val mLastRandomIndexMap: MutableMap<String, Int> = mutableMapOf()
    const val TIME_GAP_FOR_NOTIFY_INTERVAL = 30 * 1000

    var context: Context = BaseApp.appContext()

    // notifi type
    const val NOTI_TYPE_BG_WITHDRAW = "NOTI_TYPE_BG_WITHDRAW"
    const val NOTI_TYPE_BG_RANDOM = "NOTI_TYPE_BG_RANDOM"
    const val NOTI_TYPE_RESIDENT = "NOTI_TYPE_RESIDENT"
    const val NOTI_TYPE_UNLOCK = "NOTI_TYPE_UNLOCK"
    const val NOTI_TYPE_FXITIME = "NOTI_TYPE_FXITIME"

    // notifi id
    const val NOTI_ID_TYPE_BG_WITHDRAW = 666
    const val NOTI_ID_TYPE_BG_RANDOM = 667
    const val NOTI_ID_TYPE_RESIDENT = 668
    const val NOTI_ID_TYPE_UNLOCK = 669
    const val NOTI_ID_TYPE_FXITIME = 670

    const val NOTI_ID_TYPE_RANDOM_1 = 670
    const val NOTI_ID_TYPE_RANDOM_2 = 671
    const val NOTI_ID_TYPE_RANDOM_3 = 672
    const val NOTI_ID_TYPE_RANDOM_4 = 673
    const val NOTI_ID_TYPE_RANDOM_5 = 674
    const val NOTI_ID_TYPE_RANDOM_6 = 675
    const val NOTI_ID_TYPE_RANDOM_7 = 676
    const val NOTI_ID_TYPE_RANDOM_8 = 677
    const val NOTI_ID_TYPE_RANDOM_9 = 678
    const val NOTI_ID_TYPE_RANDOM_10 = 679
    const val NOTI_ID_TYPE_RANDOM_11 = 680

    // channelId type
    const val CHANNEL_TYPE_BG_WITHDRAW = "CHANNEL_TYPE_BG_WITHDRAW"
    const val CHANNEL_TYPE_BG_RANDOM = "CHANNEL_TYPE_BG_RANDOM"
    const val CHANNEL_TYPE_RESIDENT = "CHANNEL_TYPE_RESIDENT"
    const val CHANNEL_TYPE_UNLOCK = "CHANNEL_TYPE_UNLOCK"
    const val CHANNEL_TYPE_RANDOM = "CHANNEL_TYPE_RANDOM"

    // channelId name
    const val CHANNEL_TYPE_BG_WITHDRAW_NAME = "CHANNEL_TYPE_BG_WITHDRAW_NAME"
    const val CHANNEL_TYPE_BG_RANDOM_NAME = "CHANNEL_TYPE_BG_RANDOM_NAME"
    const val CHANNEL_TYPE_RESIDENT_NAME = "CHANNEL_TYPE_RESIDENT_NAME"
    const val CHANNEL_TYPE_UNLOCK_NAME = "CHANNEL_TYPE_UNLOCK_NAME"
    const val CHANNEL_TYPE_RANDOM_NAME = "CHANNEL_TYPE_RANDOM_NAME"


    private val mConfigMap: ConcurrentHashMap<String, NotificationConfig> = ConcurrentHashMap()
    private val mRandomConfigList: MutableList<NotificationConfig> = mutableListOf()



    init {

        initRandomConfigs()




        // # 切换后台 - 提现
        val bgWithdrawIntent = entryPointPendingIntent(context,
            type2notificationId[NotificationType.RESIDENT_WITHDRAW] ?: 0
        ) {
            it.putExtra(LANDING_NOTIFICATION_ACTION, PushContent.ACTION_TYPE_WITHDRAW)
            it.putExtra(LANDING_NOTIFICATION_FROM, NotificationCheckController.NotificationType.RESIDENT.string)
        }
        mConfigMap.put(NOTI_TYPE_BG_WITHDRAW, NotificationConfig(
            NOTI_ID_TYPE_BG_WITHDRAW,
            CHANNEL_TYPE_BG_WITHDRAW,
            CHANNEL_TYPE_BG_WITHDRAW_NAME,
            ResUtil.getString(R.string.title_bg_withdraw),
            ResUtil.getString(R.string.content_bg_withdraw),
            R.mipmap.icon_bg_withdraw,
            bgWithdrawIntent,
            true,
        ))




    }

    private fun initRandomConfigs() {
        try {
            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_1,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "Time to claim coins！",
                "You have free coins today!Open to claim!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_1))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_2,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "Time to claim cash！",
                "You have free cash today!Open to claim!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_2))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_3,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "Hey!",
                "You received R$10, click to withdraw immediately!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_3))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_4,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "\uD83D\uDCDE\uD83D\uDCDE\uD83D\uDCDE Do you miss me? ",
                "Come play the game and withdraw now!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_4))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_5,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "\uD83D\uDCDE\uD83D\uDCDE\uD83D\uDCDE Do you miss me? ",
                "Come play the game and withdraw now!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_5))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_6,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "\uD83D\uDCDE\uD83D\uDCDE\uD83D\uDCDE Do you miss me? ",
                "Come play the game and withdraw now!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_6))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_7,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "\uD83D\uDCDE\uD83D\uDCDE\uD83D\uDCDE Do you miss me? ",
                "Come play the game and withdraw now!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_7))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_8,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "Today’s 1,024th Cash-Out Champion is HERE!",
                "You’re the missing piece! Join now!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_8))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_9,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "User #888 Just Cashed Out！",
                "Don’t be left behind! Tap to claim yours!",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_9))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_10,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "You missed(2) calls",
                "+86-12345, +86-666999",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_10))
            )

            mRandomConfigList.add(NotificationConfig(
                NOTI_ID_TYPE_RANDOM_11,
                CHANNEL_TYPE_RANDOM,
                CHANNEL_TYPE_RANDOM_NAME,
                "Gmail",
                "support.gov@gmail.com",
                R.mipmap.task_cash,
                entryPointPendingIntent(context, NOTI_ID_TYPE_RANDOM_11))
            )

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    fun getConfigForType(notifiType: String): NotificationConfig? {
        return mConfigMap[notifiType]
    }

    fun getRandomConfig(randomType:String, specifyList: List<Int>): NotificationConfig {
        var randomIndex = AndroidUtil.randomInt(0, specifyList.size - 1)
        val newGenIndex = specifyList[randomIndex] - 1
        val lastIndex = mLastRandomIndexMap[randomType]

        if (lastIndex == newGenIndex) {
            randomIndex++
            if (randomIndex >= specifyList.size) {
                randomIndex = 0
            }
        }
        mLastRandomIndexMap.put(randomType, specifyList[randomIndex] - 1)

        return mRandomConfigList[mLastRandomIndexMap[randomType]!!]
    }

    interface IResidentDataCallback {
        fun getGoldNumber(): Long
        fun getCashNumber(): Double
        fun getBoxRestTimeStr(): String
        fun hasWithdrawed01(): Boolean
    }

    fun setResidentCallback(callback: IResidentDataCallback) {
        mCallback = callback
    }

    fun getResidentDatas(): IResidentDataCallback {
        return mCallback
    }
}