"use-strict";
const { objSerialize, objBulkSet } = require("./util.js")
const { RadioMetadata } = require("./RadioMetadata.js")
const { RadioMiscInfo } = require("./RadioMiscInfo.js")

/**
 * @public
 * @constructor
 * @params {string} id
 * @returns {RadioItem}
 */
const RadioItem = function (id, miscInfo) {
    this.id = id
    this.metadata = undefined
    this.info = undefined
    this.misc = miscInfo || undefined
    this.tags = undefined
    this.sources = []
    return this
}

/**
 * @public
 * @params {string} id
 * @returns {RadioItem}
 */
RadioItem.new = id => new RadioItem(id)

/**
 * @public
 * @params {string|undefined} title
 * @params {string|undefined} artist
 * @params {URL|string|undefined} [href] href
 * @params {string} [extra] extra Playback effects. i.e: Pitch, speed
 * @returns {RadioItem}
 */
RadioItem.prototype.setMetadata = function (title, artist, href, extra) {
    if (!this.metadata)
        this.metadata = RadioMetadata.new()
    objBulkSet(this, "metadata", { title, artist, href, extra });
    return this
}

/**
 * @public
 * @params {RadioMetadata} metadata
 * @returns {RadioItem}
 */
RadioItem.prototype.addItemInfo = function (metadata) {
    if (!this.info)
        this.info = []
    this.info.push(metadata)
    return this
}

/**
 * @public
 * @params {RadioMiscInfo} miscInfo
 * @returns {RadioItem}
 */
RadioItem.prototype.setMiscInfo = function (miscInfo) {
    this.misc = miscInfo
    return this
}

/**
 * @public
 * @returns {RadioItem}
 */
RadioItem.prototype.hoistMiscInfo = function () {
    return this.misc ? this.misc : this.misc = RadioMiscInfo.new()
}

/**
 * @public
 * @params {string[]} tags
 * @returns {RadioItem}
 */
RadioItem.prototype.addTags = function (tags) { this.tags = this.tags ? [ ...(this.tags), ...tags ] : tags; return this }

/**
 * Alias for addTags; prefixes all items with "radio-media-style-"
 * @public
 * @params {string[]} tags
 * @returns {RadioItem}
 * @see RadioItem.prototype.addTags
 */
RadioItem.prototype.addStyleTags = function (tags) { return this.addTags(tags.map(a => `radio-media-style-${a}`)); return this }

/**
 * @public
 * @params {RadioSource} source
 * @returns {RadioItem}
 */
RadioItem.prototype.addSource = function (source) { this.sources.push(source); return this }

/**
 * Set metadata for songs that were previously on nigge.rs; adds the "niggers" tag.
 * @public
 * @params {string|undefined} nigid
 * @params {string|undefined} nigkey
 * @returns {RadioItem}
 */
RadioItem.prototype.setNiggadata = function (nigid, nigkey) {
    this.setMetadata()
    this.metadata._nigid = nigid
    this.metadata._nigkey = nigkey
    //this.addTags([ "niggers" ])
    this.hoistMiscInfo().bNiggersSong()
    return this
}

/**
 * @deprecated
 * @returns {Object}
 */
RadioItem.prototype.serialize = function (index) {
    const self = this
    return objSerialize(this, [
        "id",
        "tags",
        "metadata"
    ], obj => ({
        index,
        ...obj,
        "misc": self.misc ? self.misc.serialize() : undefined,
        "info": self.info?.length ? self.info.map(info => info.serialize()) : undefined,
        "sources": self.sources.map(source => source.serialize())
    }))
}

module.exports = { RadioItem }