console = window.require 'spine/utils/console'
Service = require '../../../browser/model'
Entry   = require '../../../browser/model/entry'
###
events:
	auth
###
class FacebookService extends Service
	constructor: ->
		super
		@permissions = {}
		@authenticated = false
		@root.id = 'me'
		@root.album = true

	refresh: (@entry) ->
		console.group 'FacebookService:refresh'
		console.log @entry

		@entry.children = []
		@pages = false
		@_requestFBPhotoSet @getEntryFetchPath(@entry), @_onFBRespond
		console.groupEnd 'FacebookService:refresh'

	getEntryFetchPath: (entry) =>
		imageType = !!~@options.types.indexOf 'image'
		end = if imageType then 'photos' else 'videos/uploaded'
		end = 'albums' if entry.album

		entry.id + '/' + end

	_requestFBPhotoSet: (path, _callback) =>
		FB.api path, _callback

	_requestFBPages: =>
		FB.api 'me/accounts', @_onFBRespond

	_onFBRespond: (response) =>
		console.group 'FacebookService:_onFBRespond'
		console.log 'fb response', {response}

		data = response.data or []

		# remove empty folders
		if @entry.root or @entry.album
			# TODO: eliminate empty folders for video type too
			data = data.filter (i) -> i.count > 0 or (i.perms and i.category and !i.count?)

		# map data response as entry items
		data = @mapEntries data

		# add the 'My Photos' or 'My Videos' folder to the root folder
		if (@entry.root or @entry.album) and !@entry.children?.filter((i) -> i.my_album).length
			data or= []
			# unless imageType = !!~@options.types.indexOf 'image'
			# 	data = []
			imageType = !!~@options.types.indexOf 'image'
			[name, path] = if imageType then ['Photos', @entry.path+'/photos'] else ['Videos', @entry.path+'/videos']
			name = 'My '+name if @entry.id is 'me'
			data.push new Entry {service: @, name, path, id: @entry.id, type: 'folder', my_album: true}

		if @entry.root and @permissions.manage_pages? and !@permissions.manage_pages and !@entry.children?.filter((i) -> i.pages).length
			data.push pagesEntry = new Entry {service: @, name:'Pages', path: 'accept_pages', type: 'button', pages: true}
			pagesEntry.bind 'select', @_auth.bind @, true

		@trigger 'refresh', data
		console.log @entry.path+' children:', data

		if (path = response.paging?.next)?
			@_requestFBPhotoSet path, @_onFBRespond
		# append the user's pages as folders
		else if @entry.root and not @pages
			@pages = true
			@_requestFBPages()

		console.groupEnd 'FacebookService:_onFBRespond'

	mapEntries: (data) ->
		data.map (item) =>
			opts = images: item.images, id:item.id, name: item.name
			opts.type    = if item.images? then 'image' else (if item.format then 'video' else 'folder')
			opts.album   = !!(item.perms and item.category) if opts.type is 'folder'
			opts.path    = @entry.path+'/'+item.id
			opts.preview = if opts.type is 'image' then item.images[Math.max(0,item.images.length-4)].source else @entry.picture
			opts.source  = item.source if opts.type is 'video'
			new Entry opts

	preview: (i)-> i.preview

	getURL: (entry, callback) ->
		callback if entry.type is 'image' then entry.images[0].source else entry.source

	_auth: (interactive = true) ->
		console.group 'FacebookService:auth'

		callback = (response) =>
			if response.status is 'connected'
				@getPermissions()
			else
				@trigger 'auth', @authenticated = false

			console.groupEnd 'FacebookService:auth:interactive_api_callback'

		unless interactive
			FB.getLoginStatus callback
		else
			FB.login callback, scope:'user_photos,user_videos,manage_pages'

		console.groupEnd 'FacebookService:auth'

	deauth: ->
		FB.logout => @trigger 'auth', false

	getPermissions: =>
		FB.api '/me/permissions', (resp)=>
			console.group 'FacebookService:auth:api_callback'

			@permissions = {}
			resp.data.forEach (d) => @permissions[d.permission] = d.status is 'granted'
			console.log 'fb permissions response', resp, {@permissions}

			photo = 'image' in @options.types or 'photo' in @options.types
			video = 'video' in @options.types

			if ((photo and @permissions.user_photos) or (video and @permissions.user_videos)) and @permissions.manage_pages?
				@trigger 'auth', @authenticated = true
			else
				# @auth true
				@trigger 'auth', @authenticated = false

			console.groupEnd 'FacebookService:auth:api_callback'

module.exports = FacebookService