package com.base.locationsharewhite.fcm

import android.util.SparseArray
import androidx.core.util.isNotEmpty
import com.base.locationsharewhite.notification.NotificationBean

/**
 * 消息触发配置与消息通知栏配置项目
 *
 * @property timer 定时器场景触发配置
 * @property battery 电量场景触发配置
 * @property unlock 解锁场景触发配置
 * @property packageChange app安装与卸载触发场景
 * @property hover 通知栏悬停配置
 * @property remoteViewContainer 自定义通知栏注册容器
 */
data class MsgConfig(
    var timer: MsgConfigBean, var battery: MsgConfigBean,
    var unlock: MsgConfigBean, var packageChange: MsgConfigBean,
    var hover: MsgConfigBean, val remoteViewContainer: SparseArray<NotificationBean>?
) {

    class Builder {
        private var timer: MsgConfigBean? = null
        private var battery: MsgConfigBean? = null
        private var unlock: MsgConfigBean? = null
        private var packageChange: MsgConfigBean? = null
        private var hover: MsgConfigBean? = null
        private val remoteViewContainer = SparseArray<NotificationBean>()

        fun setConfig(configBean: MsgConfigBean, msgType: MsgType): Builder {
            when (msgType) {
                MsgType.TIMER -> timer = configBean
                MsgType.BATTERY -> battery = configBean
                MsgType.UNLOCK -> unlock = configBean
                MsgType.PACKAGE -> packageChange = configBean
            }
            return this
        }

        /**
         * 设置悬停通知配置
         *
         * @param configBean
         * @return
         */
        fun setHover(configBean: MsgConfigBean): Builder {
            hover = configBean
            return this
        }

        fun addRemoteBean(actionId: Int, bean: NotificationBean): Builder {
            remoteViewContainer.put(actionId, bean)
            return this
        }

        fun build(): MsgConfig {
            val config = MsgConfig(
                //默认情况下，1.timer默认首次3分钟，之后7分钟。
                timer ?: MsgConfigBean(3, 7),
                //2.解锁场景，默认间隔1分钟，默认无次数限制。
                battery ?: MsgConfigBean(60),
                //3.电量场景，默认间隔1小时，默认无次数限制。
                unlock ?: MsgConfigBean(1),
                //4.安装与卸载场景，默认间隔5分钟，默认无次数限制。
                packageChange ?: MsgConfigBean(5),
                //设置悬停通知配置，默认开启，0代表关闭，1代表开启，持续时间默认为5s
                hover ?: MsgConfigBean(1, 5),
                if (remoteViewContainer.isNotEmpty()) remoteViewContainer else null
            )
            return config
        }
    }
}