Controller = window.require 'spine/controller'

class Popup extends Controller
	className:	'popup'
	template:	require './views/popup'

	_confirmBtnClass: 'confirm-button'
	_cancelBtnClass: 'cancel-button'

	events:
		'click .close':				'close'
		'click .confirm-button':	'confirm'
		'click .cancel-button':		'cancel'
		'keyup':					'keyup'

	###
	Popup constructor
	@param [Object]:
		[String] confirmBtnClass - Confirm button class
		[String] cancelBtnClass  - Cancel button class
		[String] title		- Popup Title
		[String] content 	- Popup ExtraContent
		[String] note
		[String] confirmBtn	- Confirm button text
		[String] cancelBtn 	- Cancel button text
		[Bool] separator	- Show or not the separator (default: false)
	###
	constructor:->
		super
		@render @

	render: (item) ->
		@el.html @template
			confirmBtnClass: [@_confirmBtnClass, item.confirmBtnClass].join ' '
			cancelBtnClass: [@_cancelBtnClass, item.cancelBtnClass].join ' '
			title: item.title
			content: item.content
			note: item.note
			confirm: item.confirmBtn
			cancel: item.cancelBtn
			separator: item.separator or false
		@item = item
		@

	keyup:(e)-> @confirm() if e.keyCode is 13

	confirm: => @_remove() and @onConfirm()

	cancel: => @_remove() and @onCancel()

	close: => @_remove() and @trigger 'canceled'

	onConfirm: => @trigger 'confirmed'

	onCancel: => @trigger 'canceled'

	pop: (place, @onConfirm = @onConfirm, @onCancel = @onCancel) =>
		return if @el.is ':visible'
		place.append @el.stop(true, true).css(opacity: 0).show().animate opacity: 1, 250
		@el.unbind('click', @_close).bind 'click', @_close

	_remove: =>
		@el.stop(true, true).animate opacity: 0, 250, => @el.hide().detach()

	_close: (e) =>
		do @close if @el.is e.target

module.exports = Popup
