# Path: indivo_client_py/lib/client.py
# class IndivoClient(APIConnector):
#     def create_account(self, user):
#         USER_EMAIL = 'user_email'
#         ACCOUNT_ID = 'account_id'
#         if not (user.has_key(USER_EMAIL) or user.has_key(ACCOUNT_ID)):
#             raise APIConnectorError("No user email or account id when posting account")
#         
#         account_id = None
#         if user.has_key(USER_EMAIL):
#             account_id = user[USER_EMAIL]
#         elif user.has_key(ACCOUNT_ID):
#             account_id = user[ACCOUNT_ID]
#         
#         if account_id:
#             primary_secret = user.get('primary_secret_p', 0)
#             secondary_secret = user.get('secondary_secret_p', 0)
#             password = user.get('user_pass', '')
#             
#             # There is a difference between app_type chrome without auth and admin
#             return self.api.call(self.ds.app_info, {'account_id': account_id,
#                                               'primary_secret_p': primary_secret,
#                                             'secondary_secret_p': secondary_secret,
#                                                       'password': password})
#         return False
#     
#     def set_record_id(self, id):
#         self.record_id, self.ds.record_id = id, id
#     
#     def set_app_id(self, id):
#         self.app_id, self.ds.app_id = id, id
#     
#     def create_session(self, user):
#         if user.has_key('username') and user.has_key('user_pass'):
#             chrome_auth = {'username' : user['username'], 'password' : user['user_pass']}
#             chrome_token = self.api.call(self.ds.app_info, chrome_auth)['prd']
#             if isinstance(chrome_token, dict):
#                 self.ds.app_info.update(chrome_token)
#                 return chrome_token
#         return False
#     
#     def update_token(self, oauth_token={}):
#         if oauth_token \
#             and isinstance(oauth_token, dict) \
#             and oauth_token.has_key(OAUTH_TOKEN) \
#             and oauth_token.has_key(OAUTH_TOKEN_SECRET):
#             self.ds.app_info[OAUTH_TOKEN] = oauth_token[OAUTH_TOKEN]
#             self.ds.app_info[OAUTH_TOKEN_SECRET] = oauth_token[OAUTH_TOKEN_SECRET]
#     
#     def get_surl_credentials(self):
#         """Requires there to be a token and secret set
#         
#         produces a token and secret dictionary for SURL (signing URLs).
#         """
#         
#         if self.ds.app_info.has_key(OAUTH_TOKEN) and \
#                 self.ds.app_info.has_key(OAUTH_TOKEN_SECRET):
#             token = self.ds.app_info[OAUTH_TOKEN]
#             secret = base64.b64encode(hmac.new(self.ds.app_info[OAUTH_TOKEN_SECRET], "SURL-SECRET", hashlib.sha1).digest())
#             return {'token' : token, 'secret' : secret}
#         return False


from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseForbidden, HttpRequest
from django.core.exceptions import *
from django.core.urlresolvers import reverse
from django.conf import settings
from smart_ui_server import utils
from smart_ui_server.ui.views import login, logout
from indivo_client_py.lib.client import IndivoClient
import urllib
import urlparse
import json


HTTP_METHOD_GET = 'GET'
HTTP_METHOD_POST = 'POST'
INDEX_PAGE = 'admin_ui/index'
LOGIN_PAGE = 'admin_ui/login'
DEBUG = True


SMART_SERVER_LOCATION = urlparse.urlparse(settings.SMART_API_SERVER_BASE)
SMART_SERVER_LOCATION = {
    'host': SMART_SERVER_LOCATION.hostname,
    'scheme': SMART_SERVER_LOCATION.scheme,
    'port': SMART_SERVER_LOCATION.port or (
        SMART_SERVER_LOCATION.scheme == 'http' and '80' or
        SMART_SERVER_LOCATION.scheme == 'https' and '443'
    )
}


def admin_login_url(request):
        url = "%s?return_url=%s" % (reverse(login), urllib.quote(request.get_full_path()))
        return url


def get_api(request=None):

# Path: indivo_client_py/lib/client.py
# class IndivoClient(APIConnector):
#     def create_account(self, user):
#         USER_EMAIL = 'user_email'
#         ACCOUNT_ID = 'account_id'
#         if not (user.has_key(USER_EMAIL) or user.has_key(ACCOUNT_ID)):
#             raise APIConnectorError("No user email or account id when posting account")
#         
#         account_id = None
#         if user.has_key(USER_EMAIL):
#             account_id = user[USER_EMAIL]
#         elif user.has_key(ACCOUNT_ID):
#             account_id = user[ACCOUNT_ID]
#         
#         if account_id:
#             primary_secret = user.get('primary_secret_p', 0)
#             secondary_secret = user.get('secondary_secret_p', 0)
#             password = user.get('user_pass', '')
#             
#             # There is a difference between app_type chrome without auth and admin
#             return self.api.call(self.ds.app_info, {'account_id': account_id,
#                                               'primary_secret_p': primary_secret,
#                                             'secondary_secret_p': secondary_secret,
#                                                       'password': password})
#         return False
#     
#     def set_record_id(self, id):
#         self.record_id, self.ds.record_id = id, id
#     
#     def set_app_id(self, id):
#         self.app_id, self.ds.app_id = id, id
#     
#     def create_session(self, user):
#         if user.has_key('username') and user.has_key('user_pass'):
#             chrome_auth = {'username' : user['username'], 'password' : user['user_pass']}
#             chrome_token = self.api.call(self.ds.app_info, chrome_auth)['prd']
#             if isinstance(chrome_token, dict):
#                 self.ds.app_info.update(chrome_token)
#                 return chrome_token
#         return False
#     
#     def update_token(self, oauth_token={}):
#         if oauth_token \
#             and isinstance(oauth_token, dict) \
#             and oauth_token.has_key(OAUTH_TOKEN) \
#             and oauth_token.has_key(OAUTH_TOKEN_SECRET):
#             self.ds.app_info[OAUTH_TOKEN] = oauth_token[OAUTH_TOKEN]
#             self.ds.app_info[OAUTH_TOKEN_SECRET] = oauth_token[OAUTH_TOKEN_SECRET]
#     
#     def get_surl_credentials(self):
#         """Requires there to be a token and secret set
#         
#         produces a token and secret dictionary for SURL (signing URLs).
#         """
#         
#         if self.ds.app_info.has_key(OAUTH_TOKEN) and \
#                 self.ds.app_info.has_key(OAUTH_TOKEN_SECRET):
#             token = self.ds.app_info[OAUTH_TOKEN]
#             secret = base64.b64encode(hmac.new(self.ds.app_info[OAUTH_TOKEN_SECRET], "SURL-SECRET", hashlib.sha1).digest())
#             return {'token' : token, 'secret' : secret}
#         return False


# Path: admin_ui/views.py
from django.http import HttpResponse, HttpResponseRedirect, Http404, HttpResponseForbidden, HttpRequest
from django.core.exceptions import *
from django.core.urlresolvers import reverse
from django.conf import settings
from smart_ui_server import utils
from smart_ui_server.ui.views import login, logout
from indivo_client_py.lib.client import IndivoClient
import urllib
import urlparse
import json



HTTP_METHOD_GET = 'GET'
HTTP_METHOD_POST = 'POST'
INDEX_PAGE = 'admin_ui/index'
LOGIN_PAGE = 'admin_ui/login'
DEBUG = True


SMART_SERVER_LOCATION = urlparse.urlparse(settings.SMART_API_SERVER_BASE)
SMART_SERVER_LOCATION = {
    'host': SMART_SERVER_LOCATION.hostname,
    'scheme': SMART_SERVER_LOCATION.scheme,
    'port': SMART_SERVER_LOCATION.port or (
        SMART_SERVER_LOCATION.scheme == 'http' and '80' or
        SMART_SERVER_LOCATION.scheme == 'https' and '443'
    )
}


def admin_login_url(request):
        url = "%s?return_url=%s" % (reverse(login), urllib.quote(request.get_full_path()))
        return url


def get_api(request=None):

    api = IndivoClient(settings.CONSUMER_KEY, settings.CONSUMER_SECRET, SMART_SERVER_LOCATION)