# Path: event_store/models.py
# class Activist(models.Model):
#     hashed_email = models.CharField(max_length=64, null=True, blank=True,
#                                     help_text="sha256 hash hexdigest of the email address")
#     email = models.CharField(max_length=765, null=True, blank=True)
#     name = models.CharField(max_length=765, null=True, blank=True)
#     member_system_pk = models.CharField(max_length=765, null=True, blank=True)
#     member_system = models.ForeignKey('event_exim.EventSource', blank=True, null=True, db_index=True)
#     phone = models.CharField(max_length=75, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name or 'Activist {}:{}'.format(str(self.member_system), self.member_system_pk)
# 
#     def hash(self_or_email, email=None):
#         """Should work as a class OR instance method"""
#         if email is None:
#             if hasattr(self_or_email, 'email'):
#                 email = getattr(self, 'email', None)
#                 if email is None:
#                     raise Exception("You need to set the email or send it as an argument")
#             else:
#                 email = self_or_email
#         return hashlib.sha256(email.encode('utf-8')).hexdigest()
# 
#     def likely_same(self, other):
#         eq_attrs = ('id', 'email', 'hashed_email', 'phone')
#         for attr in eq_attrs:
#             if getattr(self, attr) and getattr(self, attr)==getattr(other,attr,None):
#                 return True
#         if self.member_system and self.member_system_pk\
#            and self.member_system_id == getattr(other,'member_system_id', None)\
#            and self.member_system_pk == getattr(other,'member_system_pk', None):
#             return True
#         return False
# 
# CHOICES = {
#     'unknown': 0,
#     #venues
#     'private home': 1,
#     'public space': 2,
#     'target location (e.g. congressional district office)': 3,
#     'virtual': 4,
#     #ticket types
#     'open': 1,
#     'ticketed': 2,
#     #is_private
#     'public': 0,
#     'private': 1,
#     #is_searchable
#     'not searchable': 0,
#     'searchable': 1,
# }


import datetime
import json
import re
import time
import requests
from itertools import chain
from event_store.models import Activist, CHOICES


#facebook wants php strtotime() format
# https://secure.php.net/manual/en/function.strtotime.php
DATE_FMT = '%Y-%m-%dT%H:%M:%S%z'

def strip_tz(datetime):
    """Strip the timezone for USE_TZ=False"""
    return datetime.replace(tzinfo=None)

class Connector:
    """

    """

    description = """
[Updated 7/18/2017]
1. Create a Facebook account.
2. Go to [Facebook for Developers](developers.facebook.com). Click on the blue *Get Started* button in the top-right corner. Create a *New App ID*. Any name will do. We recommend `eventroller`.
3. Get a long-lived user access token:
    * Go to [Facebook's Graph API Explorer](https://developers.facebook.com/tools/explorer). Look for the top right dropdown. It should have a Label saying *Application*, and a value of *Graph API Explorer*. Click on the dropdown and change it it to your app.
    * Under the *Get Token* dropdown, click on *Get User Access Token*. A modal should popup. There, select `user_events` and `pages_show_list` as permissions. Then click on *Get Access Token*. (Facebook may ask for additional permissions at this point. Click yes.)
    * Under the *Get Token* dropdown, click on *Page Access Tokens*. (Don't accidently click on *Get App Token*. That will replace your User Access Token with an App Token. We want the Page Access Token to build on top of the User Access Token. If you lose the User Access Token, then repeat the previous step before proceeding with this one.)
    * Click the little blue i left of the Token. A box should popup with token information. Note the expiration time, then click *Open in Access Token Tool*.
    * Click the blue *Extend Access Token* button at the bottom of the page. A new Access Token should pop up at the bottom.
    * Click the white "Debug" button to the right. "Expires" should show a date two months into the future.

# Path: event_store/models.py
# class Activist(models.Model):
#     hashed_email = models.CharField(max_length=64, null=True, blank=True,
#                                     help_text="sha256 hash hexdigest of the email address")
#     email = models.CharField(max_length=765, null=True, blank=True)
#     name = models.CharField(max_length=765, null=True, blank=True)
#     member_system_pk = models.CharField(max_length=765, null=True, blank=True)
#     member_system = models.ForeignKey('event_exim.EventSource', blank=True, null=True, db_index=True)
#     phone = models.CharField(max_length=75, null=True, blank=True)
# 
#     def __str__(self):
#         return self.name or 'Activist {}:{}'.format(str(self.member_system), self.member_system_pk)
# 
#     def hash(self_or_email, email=None):
#         """Should work as a class OR instance method"""
#         if email is None:
#             if hasattr(self_or_email, 'email'):
#                 email = getattr(self, 'email', None)
#                 if email is None:
#                     raise Exception("You need to set the email or send it as an argument")
#             else:
#                 email = self_or_email
#         return hashlib.sha256(email.encode('utf-8')).hexdigest()
# 
#     def likely_same(self, other):
#         eq_attrs = ('id', 'email', 'hashed_email', 'phone')
#         for attr in eq_attrs:
#             if getattr(self, attr) and getattr(self, attr)==getattr(other,attr,None):
#                 return True
#         if self.member_system and self.member_system_pk\
#            and self.member_system_id == getattr(other,'member_system_id', None)\
#            and self.member_system_pk == getattr(other,'member_system_pk', None):
#             return True
#         return False
# 
# CHOICES = {
#     'unknown': 0,
#     #venues
#     'private home': 1,
#     'public space': 2,
#     'target location (e.g. congressional district office)': 3,
#     'virtual': 4,
#     #ticket types
#     'open': 1,
#     'ticketed': 2,
#     #is_private
#     'public': 0,
#     'private': 1,
#     #is_searchable
#     'not searchable': 0,
#     'searchable': 1,
# }


# Path: event_exim/connectors/facebook.py
import datetime
import json
import re
import time
import requests
from itertools import chain
from event_store.models import Activist, CHOICES



#facebook wants php strtotime() format
# https://secure.php.net/manual/en/function.strtotime.php
DATE_FMT = '%Y-%m-%dT%H:%M:%S%z'

def strip_tz(datetime):
    """Strip the timezone for USE_TZ=False"""
    return datetime.replace(tzinfo=None)

class Connector:
    """

    """

    description = """
[Updated 7/18/2017]
1. Create a Facebook account.
2. Go to [Facebook for Developers](developers.facebook.com). Click on the blue *Get Started* button in the top-right corner. Create a *New App ID*. Any name will do. We recommend `eventroller`.
3. Get a long-lived user access token:
    * Go to [Facebook's Graph API Explorer](https://developers.facebook.com/tools/explorer). Look for the top right dropdown. It should have a Label saying *Application*, and a value of *Graph API Explorer*. Click on the dropdown and change it it to your app.
    * Under the *Get Token* dropdown, click on *Get User Access Token*. A modal should popup. There, select `user_events` and `pages_show_list` as permissions. Then click on *Get Access Token*. (Facebook may ask for additional permissions at this point. Click yes.)
    * Under the *Get Token* dropdown, click on *Page Access Tokens*. (Don't accidently click on *Get App Token*. That will replace your User Access Token with an App Token. We want the Page Access Token to build on top of the User Access Token. If you lose the User Access Token, then repeat the previous step before proceeding with this one.)
    * Click the little blue i left of the Token. A box should popup with token information. Note the expiration time, then click *Open in Access Token Tool*.
    * Click the blue *Extend Access Token* button at the bottom of the page. A new Access Token should pop up at the bottom.
    * Click the white "Debug" button to the right. "Expires" should show a date two months into the future.

4. Create a `./local_settings.py` file in your root directory if you don't have one. Save this Access Token as an `auth_token` property on a facebook app. (See `local_settings.py.example` for a guide.)