console = window.require 'spine/utils/console'

Controller      = window.require 'spine/controller'
Container       = window.require 'spine/controller/container'
Activateable    = window.require 'spine/controller/activateable'
Scrollable 		= require '../../../core/controller/scrollable'

Content 		= window.require 'core/models/content'
Field 			= require './field'
Composition     = window.require 'core/models/composition'

checkItem = (item) -> (i) -> i.item.eql item

class DirectInput extends Controller
	className   : 'directinput browser'
	title       : 'directinput'
	serviceTitle: 'link'

	@include Container
	@include Activateable
	@include Scrollable
	attributes: 'authenticated': true

	constructor: ->
		super
		console.group 'DirectInput:constructor'
		@append @contentItems()...

		@delay =>
			do @new#wait for parent to listen to @trigger
			@tab.el.attr 'control', @control.control
			@_updateTabCount()

		@listenTo @composition, 'save', @_onCompositionSave
		@listenTo Content, 'destroy', @delay.bind @, @new

		console.groupEnd 'DirectInput:constructor'

	contentItems: =>
		@composition.getContent().filter( (i) ->
			not i.metadata?.service or i.metadata?.service is 'directinput'
		).map (item) => new Field {@control, item}

	###
	Create a new input field for the `Content` item
	###
	addEntry: (item) ->
		# check if this Content item already has an input
		return if !!@children(checkItem item).length

		# create the input field
		@append field = new Field {@control, item}
		# move the `new field` after this input field
		@_getNewField().el.insertAfter field.el

		@_updateTabCount()
		@scroll().update()
		@

	_new: (i) =>
		return @field.enable() if @field?

		@item = new Content metadata: {service: 'directinput'}, composition_id: @composition.id
		@field = new Field {@control, @item}
		@item.one 'create', @onItemCreated
		@append @field

		@_updateTabCount()
		@scroll().update()
		@scroll().scrollTo('bottom', inertia: false) if i

	new: (i) =>
		@_updateTabCount()
		@_new i
		@field.disable()
		@trigger 'canAdd', @field.enable, true
		do @_new unless @_count()

	onItemCreated: =>
		@item = @field = null
		@new()

	_release:(e)=>
		Container._release.apply @,[e]
		@_updateTabCount()
		@scroll().update()

	active: =>
		Activateable.active.apply @, arguments
		@$trigger "editor:browser:active", {service: 'directinput', authenticated: false}
		@scroll().update()

	activate: =>
		Activateable.activate.apply @, arguments

		return unless @field?
		@field.disable()
		@trigger 'canAdd', @field.enable, true

	_updateTabCount: ->
		@tab?.count @_count()

	_count: ->
		@children((i) -> !i.item.isNew()).length

	_getNewField: -> @children((i)-> i.item.isNew())[0]

	###
	Event handler for the composition `save` event

	Updates the composition_id of the `new entry` field's Content item
	when the composition's id is changed after persistance
	###
	_onCompositionSave: (comp, options) =>
		return unless options.changedID
		@_getNewField()?.item.composition_id = @composition.id

module.exports = DirectInput
