###
 * coffeescript-ui - Coffeescript User Interface System (CUI)
 * Copyright (c) 2013 - 2016 Programmfabrik GmbH
 * MIT Licence
 * https://github.com/programmfabrik/coffeescript-ui, http://www.coffeescript-ui.org
###

class CUI.DocumentBrowser.NodeMatch extends CUI.Element
	initOpts: ->
		super()
		@addOpts
			title_match:
				check: CUI.DocumentBrowser.SearchMatch
			text_matches:
				check: (v) ->
					for item in v
						if item not instanceof CUI.DocumentBrowser.SearchMatch
							return false
					true
			searchQuery:
				check: CUI.DocumentBrowser.SearchQuery
			node:
				check: CUI.DocumentBrowser.Node

	marked: (markdown) ->
		@_node.getBrowser().marked(@_node, markdown)

	render: ->
		titlePath =[]
		for part in @_node.getTitlePath()
			titlePath.push(CUI.util.toHtml(part))

		if @_title_match
			# replace last item
			titlePath[titlePath.length-1] = @_title_match.getHighlighted()
		# console.debug titlePath

		lbl = new CUI.Label
			class: "cui-document-browser-search-match--title"
			multiline: true
			content: CUI.dom.htmlToNodes(titlePath.join("<span class='cui-document-browser-node-match-hierarchy'>"+new CUI.Icon(icon: "right").DOM.outerHTML+"</span>"))

		CUI.dom.setAttribute(lbl.DOM, "tabindex", 0)

		CUI.Events.listen
			type: "focus"
			node: lbl
			call: (ev) =>
				@_node.select(search: @_searchQuery.getSearch())
				return

		arr = [ lbl ]

		if @_text_matches
			ul = CUI.dom.element("UL")
			arr.push(ul)
			for text_match in @_text_matches
				html = text_match.getHighlighted(true)
				li = CUI.dom.element("LI", title: text_match.getString(), tabindex: 0)
				do (text_match) =>
					CUI.Events.listen
						type: "focus"
						node: li
						call: (ev) =>
							@_node.select(search: @_searchQuery.getSearch(), nodeIdx: text_match.nodeIdx)
							return
				CUI.dom.append(li, CUI.dom.htmlToNodes(html))
				ul.appendChild(li)
		arr
