PopupBase = require '../../controller/popup'

class Popup extends PopupBase
	className: 'control-popup date-time-popup'
	template: window.require 'core/views/date-time/popup'

	popupOptions: topOffset: 3

	events:
		'change': '_change'
		'resize:popup': 'resize'
		'click .date_time-footer': 'toggleViews'

	elements:
		'.date_time-footer' : 'footer'

	constructor: ->
		@controllers = []
		super

	render: ->
		super

		unless @options.disable is 'date'
			@controllers.push calendar = new (require './calendar')

		unless @options.disable is 'time'
			@controllers.push clock = new (require './clock')

		@el.append ctrl.el for ctrl in @controllers

		@refreshElements()
		@footer.toggle @controllers.length > 1
		@el.toggleClass 'clock', @options.disable is 'date'

	toggle: =>
		super.then =>
			setTimeout @resize, 50
			@controllers[@active = 0].activate() unless @active?

	update: (value) ->
		{@date, @time} = value
		ctrl.update value for ctrl in @controllers

	_change: (ev, data) ->
		@[prop] = val for prop, val of data
		@trigger 'change', {@date, @time}

	resize: =>
		@popup.resize()

	toggleViews: =>
		@controllers[@active].deactivate()
		@controllers[@active = (@active+1)%@controllers.length].activate()

module.exports = Popup
