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

# Instagram service config
host = 'https://widgetic.com' + '/services/instagram' #live server path
INSTAGRAM_DEFAULTS = {
    client_id:      '542808610040105', #widgetic instagram basic display app id
    client_secret:  '0a88504381f577dbbef400d4232a4258',
    redirect_uri:   host + '/auth_token',
    scope: 			'user_profile,user_media',
    response_type:	'code',
    api_url: 		host,
    demo:           "IGQVJVNUw5cHB5cUFiZATFxVmgyeWtSVFJjTDYyRmNnWDZAJZA3llVXREa1BsS1EtVGJ2UEdlbHJNbDV1UUQ2YkVSZA3YwS1dPWXJzYXFGQzdZAMlhBLTBWWWNkTVNHTGNDLW10dkdhNW0wMDVqMkhZAWEhqbgZDZD",
    token_storage:  "redis",
    encodeToken:    false,
    redis_db:       0,
    redis_prefix:   "nstgrm:",
    port:           8082,
}
api = window.INSTAGRAM_SERVICE_CONFIG || INSTAGRAM_DEFAULTS
# console.log("EDITOR Instagram API:", api);

# read current logged user's instagram auth token
try api.token = localStorage.getItem 'instagramAuthToken'
console.log("Editor Instagram Service current user's token:", api.token);

# define auth url that will open in authorize popup
auth_url =  "https://api.instagram.com/oauth/authorize/?client_id=#{api.client_id}&redirect_uri=#{api.redirect_uri}&response_type=#{api.response_type}&scope=#{api.scope}"

# INSTAGRAM SERVICE CLASS
class InstagramService extends Service
	@browser: 'browser/controller/instagram'

	constructor: ->
		super
		@user = {}
		@authenticated = false

	getToken: ->
		return api.token

	getUser: (token) ->
		# console.log("Editor Instagram Service getUser:", token, api.api_url);
		url = "#{api.api_url}/me"
		$.ajax(url: url, type: 'GET', dataType: 'json', headers: {'X-Token': token})

	_auth: (interactive=true) ->
		console.log("Editor Instagram Service _auth method:", interactive, api.token);
		# if authenticated get user info to show it in the browser
		if api.token?
			# console.log("Editor Instagram Service _auth method:", interactive, api.token);
			@getUser(api.token)
				.then (result) =>
					console.log("Editor Instagram Service getUser result:", result)
					@user = result
					@trigger 'auth', @authenticated = true
				.catch => 
					console.log("Editor Instagram Service getUser error:", error)
					@deauth()
		# console.log("Editor INSTG service _auth method:", interactive, api);
		# if not authenticated open popup
		!@authenticated and interactive and @showAuthPopup()

	showAuthPopup: ->
		# console.log("Editor show Auth Window:", localStorage, auth_url)
		
		window.instagramReady = (token, user) =>
			# console.log("Editor instagramReady:", token, user);
			api.token = token
			try localStorage.setItem 'instagramAuthToken', api.token = token
			@user = user
			@trigger 'auth', @authenticated = true

		window.open auth_url, 'Instagram connect', 'width=450,height=600'

	refresh: (@entry) =>
		@trigger 'refresh', []

	deauth: ->
		@authenticated = false
		api.token = null
		@trigger 'auth', @authenticated
		try localStorage.removeItem 'instagramAuthToken'

module.exports = InstagramService
