Controller = window.require 'spine/controller'

class Popup extends Controller
	document: $ document
	window: $ window

	show: =>
		@popup?.show()
		setTimeout => @document.bind 'click', @hide

	hide: =>
		@popup?.hide()
		@document.unbind 'click', @hide

	toggle: => @_createPopup().then => if @popup.visible then @hide() else @show()

	release: ->
		super
		@popup?.release()

	_createPopup: ->
		return @popupPromise if @popupPromise
		@popupPromise = Blogvio.UI.popup.new($.extend {}, {@anchor, css: @getPopupCSS(), @targetWindow}, (@popupOptions or {}))
			.then (@popup) => @popup.append(@el)
			.fail (err) -> console.error err.stack

	getPopupCSS: =>
		@css or= [
			'https://widgetic.com/bundles/blogvioapp/css/editor/editor.css'
		]

	setPopupCSS: (@css) => @

	setPopupTargetWindow: (@targetWindow) => @

module.exports = Popup
