/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package mozilla.components.service.glean.private

import mozilla.components.service.glean.Glean
import mozilla.components.support.base.log.logger.Logger

/**
 * This implements the developer facing API for custom pings.
 *
 * Instances of this class type are automatically generated by the parsers at build time.
 *
 * The Ping API only exposes the [send] method, which schedules a ping for sending.
 */
data class PingType(
    val name: String,
    val includeClientId: Boolean
) {
    init {
        if (pingRegistry.containsKey(name)) {
            logger.error("Duplicate ping named $name")
        }
        pingRegistry[name] = this
    }

    companion object {
        private val logger = Logger("glean/PingType")
        internal val pingRegistry: MutableMap<String, PingType> = mutableMapOf()
    }

    /**
     * Send the ping.
     *
     * While the collection of metrics into pings happens synchronously, the
     * ping queuing and ping uploading happens asyncronously.
     * There are no guarantees that this will happen immediately.
     *
     * If the ping currently contains no content, it will not be queued.
     */
    fun send() {
        Glean.sendPings(listOf(this))
    }
}
