var Util = require('libresignage/util/Util');
var Assert = require('assert');

class SlideAsset {
	/**
	* Construct a SlideAsset.
	*
	* @param {string}  slide_id The ID of the slide this asset belongs to.
	* @param {string}  mime     The MIME type of the asset.
	* @param {string}  filename The filename of the asset.
	* @param {boolean} thumb    Whether the asset has a thumbnail.
	* @param {string}  hash     The hash of the asset.
	*/
	constructor(slide_id, mime, filename, thumb, hash) {
		Assert.ok(slide_id != null);
		Assert.ok(mime != null);
		Assert.ok(filename != null);
		Assert.ok(thumb != null);
		Assert.ok(hash != null);

		this.slide_id = slide_id;
		this.mime = mime;
		this.filename = filename;
		this.thumb = thumb;
		this.hash = hash;
	}

	/**
	* Build an asset of asset thumbnail URL.
	*
	* @param {string}   base         The base URL.
	* @param {string}   endpoint_uri The **relative** endpoint URI.
	* @param {string[]} params       The query parameters.
	*
	* @return {string} The generated URL.
	*/
	build_url(base, endpoint_uri, params) {
		let query = Util.querify(params);
		base = ((base == null || base.length === 0) ? '/' : base);
		return base + endpoint_uri + '?' + query;
	}

	/**
	* Get the URL of this asset.
	*
	* @param {string|null|undefined} The base URL of the relative endpoint URI.
	*
	* @return {string} The generated URL.
	*/
	get_url(base) {
		return this.build_url(
			base,
			'api/endpoint/slide/asset/slide_get_asset.php',
			{ id: this.slide_id, name: this.filename, hash: this.hash }
		);
	}

	/**
	* Get the URL of this asset's thumbnail.
	*
	* @param {string|null|undefined} The base URL of the relative endpoint URI.
	*
	* @return {string} The generated thumbnail URL.
	*/
	get_thumb_url(base) {
		return this.build_url(
			base,
			'api/endpoint/slide/asset/slide_get_asset_thumb.php',
			{ id: this.slide_id, name: this.filename, hash: this.hash }
		);
	}

	/**
	* Check whether an asset has a thumbnail.
	*
	* @return {boolean} True if a thumbnail exists, false otherwise.
	*/
	has_thumb() { return this.thumb; }

	get_filename() { return this.filename; }
	get_mime()     { return this.mime; }
	get_hash()     { return this.hash; }
}

module.exports = SlideAsset;
