Service = require '../../../browser/model'
Entry   = require '../../../browser/model/entry'

class DefaultService extends Service
	hideDisconnectBtn: true

	constructor: ->
		super
		@title = "#{@title[0].toUpperCase()}#{@title[1...]}"
		@authenticated = false
		@authPromise = (@authDef = new $.Deferred).promise()
		window.editor.el.bind "content:#{@title.toLowerCase()}:add", @addItems

	_auth: (interactive) -> 
		@triggerAuthRequire interactive
		@trigger 'auth', @authenticated

		if @authenticated
			setTimeout => @authDef.resolve()

	addItems: (e, data = {items: []}) => 
		@auth !(@authenticated = true) unless @authenticated

		@authPromise.then =>
			@trigger 'refresh', [@addContentEntry()] unless @hasAddContentEntry
			@trigger 'refresh', (data.items or []).map @newEntry

	refresh: (currentFolder) ->
		currentFolder

	addContentEntry: =>
		@hasAddContentEntry = true
		entry = @newEntry name: "Add content", url: '/', type: 'button'
		entry.className = 'add-content-entity'
		entry.bind 'select', @triggerAuthRequire.bind @, true
		entry

	newEntry: (item) =>
		_item = new Entry
			service: @
			name: item.name or item.title or item.url
			path: item.url
			type: item.type
			url: item.url
		_item[i] = o for i,o of item
		_item

	getURL: (entry, callback) ->
		callback entry.url

	preview: (entry) ->
		entry.url

	deauth: ->

	triggerAuthRequire: (interactive) =>
		@trigger 'auth-req', {interactive, source: @title, 'content-type': @options.types}

module.exports = DefaultService