import json
import requests
from pprint import pprint
from django.shortcuts import render
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from wit import Wit
from django.views import generic
from django.http.response import HttpResponse
from django.template.context_processors import request
# Create your views here.

def post_facebook_message(fbid, recevied_message):           
    post_message_url = 'https://graph.facebook.com/v2.6/me/messages?access_token=EAASfh0TDd8cBAHBMfkWQGAexatTOup01lZCXtUJ5CF5Imr5b7MeQu30v6TnEzQmvoJF9MZBzkoZBdhLaVcCSY2BtPivUNJh7pic5vfEA13qDr3TRQLuHn8aKpKZAip4X2QHqhBTa7XQNGPnII1cqNMP46gAaRYMzHHSnZA4NZCAwZDZD' 
    response_msg = json.dumps({"recipient":{"id":fbid}, "message":{"text":recevied_message}})
    status = requests.post(post_message_url, headers={"Content-Type": "application/json"},data=response_msg)
    pprint(status.json())


def send(request, response):
    pprint('Sending to user...', response['text'])
def my_action(request):
    pprint('Received from user...', request['text'])
    

def witConnect(incoming_message):  
    actions = {
        'send': send,
        'my_action': my_action,
    }  
    client = Wit(access_token='KVCNXSS7SD5RENA5PQ6QBS242ETDIBHC', actions=actions)
    client.interactive()
    try:
        #client.__run_actions('session id', incoming_message)
        resp = client.message(incoming_message)
        #resp= client.converse('session id', incoming_message)
       
        pprint('Yay, got Wit.ai response: ' + str(resp))
        return resp
    except:
        pprint('WIT.AI ERROR')
    
#KVCNXSS7SD5RENA5PQ6QBS242ETDIBHC
#turgut DJE4HFOBMAJO6DMIC2IEZRP5DDRQRZKS    


class SkyBotView(generic.View):
   # def get(self, request, *args, **kwargs):
    #    if self.request.GET['hub.verify_token'] == '93985762':
     #       return HttpResponse(self.request.GET['hub.challenge'])
      #  else:
       #     return HttpResponse('Error, invalid token')

    @method_decorator(csrf_exempt)
    def dispatch(self, request, *args, **kwargs):
        return generic.View.dispatch(self, request, *args, **kwargs)



    # Post function to handle Facebook messages
    def post(self, request, *args, **kwargs):
        # Converts the text payload into a python dictionary
        incoming_message = json.loads(self.request.body.decode('utf-8'))
        # Facebook recommends going through every entry since they might send
        # multiple messages in a single call during high load
        for entry in incoming_message['entry']:
            for message in entry['messaging']:
                # Check to make sure the received call is a message call
                # This might be delivery, optin, postback for other events 
                if 'message' in message:
                    # Print the message to the terminal

                    pprint(message)
                    resp=witConnect(message['message']['text'])
                    strResp = parseWitData(resp)
                    post_facebook_message(message['sender']['id'],strResp )     
        return HttpResponse()


#message['message']['text']
#resp['entities']['location'][0]['value']

def parseWitData(witOut):
    scannerInput = []
    if witOut['entities']['location']!= None:
        scannerInput.append(witOut['entities']['location'][0]['value'])
    else:
        return 'I couldnt find any location info in your message. Please enter your flight "from x to y"'
    
    if witOut['entities']['location'][1]!= None:
        scannerInput.append(witOut['entities']['location'][1]['value'])
    else:
        return 'I couldnt find your destination location info in your message. Please enter your flight "from x to y"'
    
    if witOut['entities']['datetime']!= None:
       scannerInput.append(witOut['entities']['datetime'][0]['value'])
       
 #   if witOut['entities']['datetime'][1]!= None:
  #      scannerInput.append(witOut['entities']['datetime'][0]['value'])   
    return scannerInput

def homeView(request):
    return HttpResponse('Hello')



#actions = {
#    'send': send,
#    'my_action': my_action,
#}





@@ -2974,16 +2974,17 @@
         
+#
  strResp
@@ -3074,20 +3074,22 @@
 id'%5D,str
-R
+(r
 esp
+)
  )     %0A
@@ -3195,826 +3195,8 @@
 '%5D%0A%0A
-def parseWitData(witOut):%0A    scannerInput = %5B%5D%0A    if witOut%5B'entities'%5D%5B'location'%5D!= None:%0A        scannerInput.append(witOut%5B'entities'%5D%5B'location'%5D%5B0%5D%5B'value'%5D)%0A    else:%0A        return 'I couldnt find any location info in your message. Please enter your flight %22from x to y%22'%0A    %0A    if witOut%5B'entities'%5D%5B'location'%5D%5B1%5D!= None:%0A        scannerInput.append(witOut%5B'entities'%5D%5B'location'%5D%5B1%5D%5B'value'%5D)%0A    else:%0A        return 'I couldnt find your destination location info in your message. Please enter your flight %22from x to y%22'%0A    %0A    if witOut%5B'entities'%5D%5B'datetime'%5D!= None:%0A       scannerInput.append(witOut%5B'entities'%5D%5B'datetime'%5D%5B0%5D%5B'value'%5D)%0A       %0A #   if witOut%5B'entities'%5D%5B'datetime'%5D%5B1%5D!= None:%0A  #      scannerInput.append(witOut%5B'entities'%5D%5B'datetime'%5D%5B0%5D%5B'value'%5D)   %0A    return scannerInput%0A%0A
 def 

check for wit.ai