#!/usr/bin/env python

import Settings
import json


CONF_KEY_TWITTER = 'twitter'
CONF_KEY_AUTH = 'authentication'
CONF_KEY_ACCOUNT_ID = 'account_id'
CONF_KEY_CONSUMER_KEY = 'consumer_key'
CONF_KEY_CONSUMER_SECRET = 'consumer_secret'
CONF_KEY_ACCESS_KEY = 'access_key'
CONF_KEY_ACCESS_SECRET = 'access_secret'
CONF_KEY_RT_USERS = 'retweet_users'
CONF_KEY_AC_NAME = 'account_name'
CONF_KEY_AC_ID = 'account_id'
CONF_KEY_OTT_MSG = 'ott_msg'
CONF_KEY_GEOLOCATOR = 'geolocator'
CONF_KEY_MODES = 'modes'
CONF_KEY_EMERGENCYSQUAWK = 'emergencySquawk'
CONF_KEY_LAT = 'lat'
CONF_KEY_LONG = 'long'
CONF_KEY_ADD_MSGS = 'additionalMessages'
CONF_KEY_OPR_TO_TWEET = 'operatorsToTweet'
CONF_KEY_MAN_TO_TWEET = 'manufacturerToTweet'
CONF_KEY_OPR = 'operator'
CONF_KEY_MAN = 'manufacturer'
CONF_KEY_ADD_CS = 'callsignRegex'
CONF_KEY_ADD_PRE_T = 'preTweet'
CONF_KEY_ADD_POST_T = 'postTweet'
CONF_KEY_ADD_SF = 'specialFlight'
CONF_KEY_VRS = 'vrs'
CONF_KEY_URL = 'url'
CONF_KEY_NAME = 'name'
CONF_KEY_FULL_URL = 'full_url'


def init():

	global oConfig

	# Open the saved flights
	with open(Settings.filePathConfig) as configFile:    
		oConfig = json.load(configFile)
		configFile.close();	
	
	return
	
	
def GetTwitterAccountId():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_AUTH][CONF_KEY_ACCOUNT_ID]

def GetTwitterConsumerKey():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_AUTH][CONF_KEY_CONSUMER_KEY]

def GetTwitterConsumerSecret():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_AUTH][CONF_KEY_CONSUMER_SECRET]

def GetTwitterAccessKey():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_AUTH][CONF_KEY_ACCESS_KEY]

def GetTwitterAccessSecret():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_AUTH][CONF_KEY_ACCESS_SECRET]

def IsUserAuthorizedForRetweet(i_strAccountId):

	for oRtUser in oConfig[CONF_KEY_TWITTER][CONF_KEY_RT_USERS]:
		if oRtUser[CONF_KEY_AC_ID] == i_strAccountId:
			return True

	return False

def GetTwitterOttMsg():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_OTT_MSG]

def GetLocationFromGeolocator():
	if not CONF_KEY_GEOLOCATOR in oConfig[CONF_KEY_TWITTER]:
		return False
	else:
		return oConfig[CONF_KEY_TWITTER][CONF_KEY_GEOLOCATOR]
	
def DoTweetModes():
	if not CONF_KEY_MODES in oConfig[CONF_KEY_TWITTER]:
		return False
	else:
		return oConfig[CONF_KEY_TWITTER][CONF_KEY_MODES]

def TriggerEmergencySquawk():
	if not CONF_KEY_EMERGENCYSQUAWK in oConfig[CONF_KEY_TWITTER]:
		return True
	else:
		return oConfig[CONF_KEY_TWITTER][CONF_KEY_EMERGENCYSQUAWK]
		
def GetTwitterLat():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_LAT]

def GetTwitterLong():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_LONG]

def GetAdditionalMessages():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_ADD_MSGS]

def GetOperatorsToTweet():
	return oConfig[CONF_KEY_TWITTER][CONF_KEY_OPR_TO_TWEET]

def GetManufacturerToTweet():
	if not CONF_KEY_MAN_TO_TWEET in oConfig[CONF_KEY_TWITTER]:
		return None
	else:
		return oConfig[CONF_KEY_TWITTER][CONF_KEY_MAN_TO_TWEET]

def GetUrls():
	return oConfig[CONF_KEY_VRS][CONF_KEY_URL]

def dump():


	print "Account id: ", str(GetTwitterAccountId() == 804046134255685632)
	print "Consumer key: ", GetTwitterConsumerKey()
	print "Consumer secret: ", GetTwitterConsumerSecret()
	print "Access key: ", GetTwitterAccessKey()
	print "Access secret: ", GetTwitterAccessSecret()

	print "rt allowed: ", str(IsUserAuthorizedForRetweet(758003579684786176))
	print "rt allowed: ", str(IsUserAuthorizedForRetweet(34566))

	print "OTT msg: ", GetTwitterOttMsg()
	
	
	
	return
