# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.views.decorators.http import require_POST

from twilio.twiml.messaging_response import MessagingResponse
# Create your views here.
from twilio_sms_handler.TrelloQuery import TrelloQuery
from twilio_trello.twilio_util import validate_twilio_request
from django.views.decorators.csrf import csrf_exempt


@require_POST
@validate_twilio_request
@csrf_exempt
def sms_view(request):
    """Twilio Messaging URL - receives incoming messages from Twilio"""
    # Create a new TwiML response
    resp = MessagingResponse()

    # <Message> a text back to the person who texted us
    text = request.POST['Body']
    split_text = text.lower().split(" ")
    if len(split_text) < 2:
        body = '''1)get boards
2)get lists <board-no>
3)get cards <board-no:list-no>
'''
    else:
        trello_query = TrelloQuery()
        action = '_'.join(split_text[:2])
        try:
            body = getattr(trello_query, action)(split_text[2:])
        except:
            body = "Incorrect input!!"
    resp.message(body)

    # Return the TwiML
    return HttpResponse(resp)
