require('./social-bar.scss');

module.exports = new Class({
	Extends: Vn.Component
	,Tag: 'htk-social-bar'
	,Properties: {
		conn: {
			type: Db.Connection
			,set(x) {	
				this._conn = x;
				this._refresh();
			}
			,get() {
				return this._conn;
			}
		},
		priority: {
			type: Number
			,set(x) {	
				this._priority = x;
				this._refresh();
			}
			,get() {
				return this._priority;
			}
		}	
	}
	
	,_priority: 0
	
	,initialize() {
		var node = this.createRoot('div');
		node.className = 'htk-social-bar';
	}

	,async _refresh() {
		if (!this._conn || this._priority === null)
			return;
		
		const params = {priority: this._priority};
		
		const query = 'SELECT title, link, icon FROM social '
			+'WHERE priority >= #priority ORDER BY priority';
		const resultSet = await this._conn.execQuery(query, params);

		Vn.Node.removeChilds(this._node);	
		const res = resultSet.fetchResult();
		
		while (res.next()) {
			const a = this.createElement('a');
			a.href = res.get('link');
			a.target = '_blank';
			this._node.appendChild(a);
			
			const img = this.createElement('img');
			img.src = 'image/social/'+ res.get('icon');
			img.alt = res.get('title');
			img.title = res.get('title');
			a.appendChild(img);
		}
	}
});

