metrics = require("metrics-sharelatex")
logger = require('logger-sharelatex')
_ = require('underscore')
User = require('../../models/User').User
Project = require('../../models/Project').Project
DocumentUpdaterHandler = require('../DocumentUpdater/DocumentUpdaterHandler')
Settings = require('settings-sharelatex')
util = require('util')
redis = require('redis')
rclient = redis.createClient(Settings.redis.web.port, Settings.redis.web.host)
rclient.auth(Settings.redis.web.password)
RecurlyWrapper = require('../Subscription/RecurlyWrapper')
SubscriptionHandler = require('../Subscription/SubscriptionHandler')
projectEntityHandler = require('../Project/ProjectEntityHandler')
TpdsPollingBackgroundTasks = require("../ThirdPartyDataStore/TpdsPollingBackgroundTasks")
EditorRealTimeController = require("../Editor/EditorRealTimeController")
SystemMessageManager = require("../SystemMessages/SystemMessageManager")

oneMinInMs = 60 * 1000

updateOpenConnetionsMetrics = ()->
	metrics.gauge "open_connections.socketio", require("../../infrastructure/Server").io?.sockets?.clients()?.length
	metrics.gauge "open_connections.http", _.size(require('http').globalAgent?.sockets)
	metrics.gauge "open_connections.https", _.size(require('https').globalAgent?.sockets)
	setTimeout updateOpenConnetionsMetrics, oneMinInMs

setTimeout updateOpenConnetionsMetrics, oneMinInMs



module.exports = AdminController =

	index : (req, res, next)=>
		http = require('http')
		openSockets = {}
		for url, agents of require('http').globalAgent.sockets
			openSockets["http://#{url}"] = (agent._httpMessage.path for agent in agents)
		for url, agents of require('https').globalAgent.sockets
			openSockets["https://#{url}"] = (agent._httpMessage.path for agent in agents)

		SystemMessageManager.getMessagesFromDB (error, systemMessages) ->
			return next(error) if error?
			res.render 'admin',
				title: 'System Admin'
				openSockets: openSockets
				systemMessages: systemMessages

	dissconectAllUsers: (req, res)=>
		logger.warn "disconecting everyone"
		EditorRealTimeController.emitToAll 'forceDisconnect', "Sorry, we are performing a quick update to the editor and need to close it down. Please refresh the page to continue."
		res.send(200)

	closeEditor : (req, res)->
		logger.warn "closing editor"
		Settings.editorIsOpen = req.body.isOpen
		res.send(200)

	writeAllToMongo : (req, res)->
		logger.log "writing all docs to mongo"
		Settings.mongo.writeAll = true
		DocumentUpdaterHandler.flushAllDocsToMongo ()->
			logger.log "all docs have been saved to mongo"
			res.send()

	syncUserToSubscription: (req, res)->
		{user_id, subscription_id} = req.body
		RecurlyWrapper.getSubscription subscription_id, {}, (err, subscription)->
			User.findById user_id, (err, user)->
				SubscriptionHandler.syncSubscriptionToUser subscription, user._id, (err)->
					logger.log user_id:user_id, subscription_id:subscription_id, "linked account to subscription"
					res.send()

	flushProjectToTpds: (req, res)->
		projectEntityHandler.flushProjectToThirdPartyDataStore req.body.project_id, (err)->
			res.send 200

	pollUsersWithDropbox: (req, res)->
		TpdsPollingBackgroundTasks.pollUsersWithDropbox ->
			res.send 200
			
	createMessage: (req, res, next) ->
		SystemMessageManager.createMessage req.body.content, (error) ->
			return next(error) if error?
			res.send 200
			
	clearMessages: (req, res, next) ->
		SystemMessageManager.clearMessages (error) ->
			return next(error) if error?
			res.send 200
