import json

from django.template.loader import render_to_string
from django.http import HttpResponse
from django.utils import timezone

from cir.models import *
import claim_views

from cir.phase_control import PHASE_CONTROL
import utils

def api_load_all_documents(request):
    response = {}
    context = {}
    context["docs"] = []
    forum = Forum.objects.get(id=request.session['forum_id'])
    # retrieve docs in a folder
    docs = Doc.objects.filter(forum_id=request.session['forum_id'])
    for doc in docs:
        doc_attr = {}
        doc_attr['folder'] = doc.folder
        doc_attr['title'] = doc.title
        doc_attr['sections'] = []
        ordered_sections = doc.sections.filter(order__isnull=False).order_by('order')
        for section in ordered_sections:
            doc_attr['sections'].append(section.getAttr(forum))
        unordered_sections = doc.sections.filter(order__isnull=True).order_by('updated_at')
        for section in unordered_sections:
            doc_attr['sections'].append(section.getAttr(forum))
        context["docs"].append(doc_attr);
        response['workbench_document'] = render_to_string("workbench-documents.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_toc(request):
    response = {}
    context = {}
    # retrieve docs not in any folder
    context['root_docs'] = []
    root_docs = Doc.objects.filter(forum_id=request.session['forum_id'], folder__isnull=True).order_by("order")
    for doc in root_docs:
        m_doc = {}
        m_doc['name'] = doc.title
        m_doc['id'] = doc.id
        m_doc['description'] = doc.description
        m_doc['content'] = []
        for section in doc.sections.all():
            m_sec = {}
            m_sec["name"] = section.title
            m_sec["id"] = section.id
            m_doc['content'].append(m_sec)
        m_doc['content'].sort(key = lambda x: x["id"])
        context['root_docs'].append(m_doc)
    # retrieve docs in a folder
    folders = EntryCategory.objects.filter(forum_id=request.session['forum_id'], category_type='doc')
    context['folders'] = []
    for folder in folders:
        m_folder = {}
        m_folder['name'] = folder.name
        m_folder['content'] = [] 
        docs = Doc.objects.filter(folder=folder)
        for doc in docs:
            m_doc = {}
            m_doc['name'] = doc.title
            m_doc['id'] = doc.id
            m_doc['description'] = doc.description
            m_doc['content'] = []
            for section in doc.sections.all():
                m_sec = {}
                m_sec["name"] = section.title
                m_sec["id"] = section.id
                m_doc['content'].append(m_sec)
            m_doc['content'].sort(key = lambda x: x["id"])
            m_folder['content'].append(m_doc)
        context['folders'].append(m_folder)
    response['document_toc'] = render_to_string("document-toc.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_doc_by_hl_id(request):
    response = {}
    context = {}
    forum = Forum.objects.get(id=request.session['forum_id'])
    # retrieve docs in a folder
    # nugget claim one-one mapping
    claim_id = request.REQUEST.get("claim_id")
    if HighlightClaim.objects.filter(claim = Claim.objects.get(id=claim_id)).count() > 0:
        hl = HighlightClaim.objects.filter(claim = Claim.objects.get(id=claim_id))[0].highlight
        sec = DocSection.objects.get(id=hl.context.id)
        doc = sec.doc
        context['doc_name'] = doc.title
        context['sections'] = []
        context['doc_id'] = doc.id
        ordered_sections = doc.sections.filter(order__isnull=False).order_by('order')
        for section in ordered_sections:
            context['sections'].append(section.getAttr(forum))
        unordered_sections = doc.sections.filter(order__isnull=True).order_by('updated_at')
        for section in unordered_sections:
            context['sections'].append(section.getAttr(forum))
        response['workbench_document'] = render_to_string("workbench-document.html", context)
        response['doc_id'] = doc.id
        response['highlight'] = hl.getAttr()
        response['hl_id'] = hl.id
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_doc_by_sec_id(request):
    response = {}
    context = {}
    forum = Forum.objects.get(id=request.session['forum_id'])
    # retrieve docs in a folder
    sec_id = request.REQUEST.get("sec_id")
    sec = DocSection.objects.get(id = sec_id)
    doc = sec.doc
    context['doc_name'] = doc.title
    context['sections'] = []
    context['doc_id'] = doc.id
    ordered_sections = doc.sections.filter(order__isnull=False).order_by('order')
    for section in ordered_sections:
        context['sections'].append(section.getAttr(forum))
    unordered_sections = doc.sections.filter(order__isnull=True).order_by('updated_at')
    for section in unordered_sections:
        context['sections'].append(section.getAttr(forum))
    response['workbench_document'] = render_to_string("workbench-document.html", context)
    response['doc_id'] = doc.id
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_doc_by_doc_id(request):
    response = {}
    context = {}
    forum = Forum.objects.get(id=request.session['forum_id'])
    # retrieve docs in a folder
    doc_id = request.REQUEST.get("doc_id")
    doc = Doc.objects.get(id = doc_id)
    context['doc_name'] = doc.title
    context['doc_id'] = doc.id
    context['sections'] = []
    ordered_sections = doc.sections.filter(order__isnull=False).order_by('order')
    for section in ordered_sections:
        context['sections'].append(section.getAttr(forum))
    unordered_sections = doc.sections.filter(order__isnull=True).order_by('updated_at')
    for section in unordered_sections:
        context['sections'].append(section.getAttr(forum))
    response['workbench_document'] = render_to_string("workbench-document.html", context)
    response['doc_id'] = doc.id
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_init_doc(request):
    response = {}
    context = {}
    forum = Forum.objects.get(id=request.session['forum_id'])
    # retrieve docs in a folder
    doc = Doc.objects.filter(forum_id=request.session['forum_id'], order__isnull=False).order_by('order')[0]
    doc_id = doc.id
    context['doc_name'] = doc.title
    context['doc_id'] = doc_id
    context['sections'] = []
    ordered_sections = doc.sections.filter(order__isnull=False).order_by('order')
    for section in ordered_sections:
        context['sections'].append(section.getAttr(forum))
    unordered_sections = doc.sections.filter(order__isnull=True).order_by('updated_at')
    for section in unordered_sections:
        context['sections'].append(section.getAttr(forum))
    response['workbench_document'] = render_to_string("workbench-document.html", context)
    response['doc_id'] = doc_id
    return HttpResponse(json.dumps(response), mimetype='application/json')

def add_nugget_comment(request):
    response = {}
    context = {}
    context['nugget_comments'] = []
    author = request.user
    forum_id = request.session['forum_id']
    theme_id = request.REQUEST.get('theme_id')
    content = request.REQUEST.get('content')
    now = timezone.now()
    nugget_comments = NuggetComment.objects.filter(forum_id = forum_id, theme_id = theme_id).order_by('created_at')
    if (content != ""):
        newNuggetComment = NuggetComment(author = author, forum_id = forum_id, theme_id = theme_id, content = content, created_at = now)
        newNuggetComment.save()
        nugget_comments = NuggetComment.objects.filter(forum_id = forum_id, theme_id = theme_id).order_by('created_at')
    for nugget_comment in nugget_comments:
        context['nugget_comments'].append(nugget_comment)
    response['workbench_nugget_comments'] = render_to_string("workbench_nugget_comments.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_all_themes(request):
    response = {}
    context = {}
    forum = Forum.objects.get(id=request.session['forum_id'])
    context['forum_name'] = forum.full_name
    context['forum_url'] = forum.url    
    themes = ClaimTheme.objects.filter(forum_id=request.session['forum_id'])
    context["themes"] = []
    for theme in themes:
        context["themes"].append(theme)
    context["phase"] = PHASE_CONTROL[forum.phase]
    response['workbench_container'] = render_to_string("workbench-theme-container.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_highlights(request):
    response = {}
    response['highlights'] = []
    theme_id = request.REQUEST.get('theme_id')
    doc_id = request.REQUEST.get('doc_id')
    doc = Doc.objects.get(id = doc_id)
    if theme_id == "-1":
        for section in doc.sections.all():
            highlights = section.highlights.all()
            for highlight in highlights:
                highlight_info = highlight.getAttr()
                response['highlights'].append(highlight_info)
    else:
        for section in doc.sections.all():
            highlights = section.highlights.all()
            for highlight in highlights:
                if (highlight.theme != None and int(highlight.theme.id) == int(theme_id)):
                    highlight_info = highlight.getAttr()
                    response['highlights'].append(highlight_info)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_one_highlight(request):
    response = {}
    response['highlights'] = []
    hl_id = request.REQUEST.get('hl_id')
    hl = Highlight.objects.get(id = hl_id)
    highlight_info = hl.getAttr()
    response['highlight'] = highlight_info
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_remove_claim(request):
    response = {}
    claim_id = request.REQUEST.get('claim_id')
    c = Claim.objects.get(id=claim_id)
    c.delete()
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_add_claim(request):
    response = {}
    content = request.REQUEST.get('content')
    theme_id = request.REQUEST.get('theme_id')
    data_hl_ids = request.REQUEST.get('data_hl_ids')
    category = request.REQUEST.get('category')
    now = timezone.now()
    if 'actual_user_id' in request.session:
        actual_author = User.objects.get(id=request.session['actual_user_id'])
    else:
        actual_author = None
    if actual_author:
        newClaim = Claim(forum_id=request.session['forum_id'], author=actual_author, delegator=request.user, content=content,
            created_at=now, updated_at=now, theme_id=theme_id, claim_category=category)
    else:
        newClaim = Claim(forum_id=request.session['forum_id'], author=request.user, created_at=now, updated_at=now, content=content,
            theme_id=theme_id, claim_category=category)
    newClaim.save()
    if actual_author:
        claim_version = ClaimVersion(forum_id=request.session['forum_id'], author=actual_author, delegator=request.user,
            content=content, created_at=now, updated_at=now, claim=newClaim)
    else:
        claim_version = ClaimVersion(forum_id=request.session['forum_id'], author=request.user, content=content,
            created_at=now, updated_at=now, claim=newClaim)
    claim_version.save()
    data_hl_ids = data_hl_ids.strip()
    data_hl_ids_set = data_hl_ids.split(" ")
    for data_hl_id in data_hl_ids_set:
        newHighlightClaim = HighlightClaim(claim_id=newClaim.id, highlight_id=data_hl_id)
        newHighlightClaim.save()
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_assign_nugget(request):
    highlight_id = request.REQUEST.get("highlight_id")
    theme_id = request.REQUEST.get("theme_id")
    highlight = Highlight.objects.get(id=highlight_id)
    highlight.theme_id = theme_id
    highlight.save()
    response = {}
    return HttpResponse(json.dumps(response), mimetype='application/json')

# nugget list zone

def api_change_to_nugget(request):
    # input: highlight_ids, output: set as nugget
    response = {}
    context = {}
    data_hl_ids = request.REQUEST.get("data_hl_ids").split(" ")
    for data_hl_id in data_hl_ids:
        hl = Highlight.objects.get(id = data_hl_id)
        hl.is_nugget = True
        hl.save()
    docs = Doc.objects.filter(forum_id=request.session["forum_id"])
    context['highlights'] = []
    for doc in docs:
        for section in doc.sections.all():
            highlights = section.highlights.filter(is_nugget = True)
            for highlight in highlights:
                context['highlights'].append(highlight.getAttr())
    response['workbench_nuggets'] = render_to_string("workbench-nuggets.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_change_to_nugget_1(request):
    # input: highlight_id, output: one nugget
    response = {}
    context = {}
    data_hl_id = request.REQUEST.get("data_hl_id")
    hl = Highlight.objects.get(id = data_hl_id)
    hl.is_nugget = True
    hl.save()
    context['highlight'] = hl.getAttr()
    response['workbench_single_nugget'] = render_to_string("workbench-single-nugget.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_remove_nugget(request):
    # input: highlight_ids, output: set as not nugget
    response = {}
    context = {}
    hl_id = request.REQUEST.get("hl_id")
    hl = Highlight.objects.get(id = hl_id)
    # hl.is_nugget = False
    # hl.save()
    hl.delete()
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_nugget_list(request):
    response = {}
    context = {}
    theme_id = int(request.REQUEST.get("theme_id"))
    docs = Doc.objects.filter(forum_id=request.session["forum_id"])
    context['highlights'] = []
    for doc in docs:
        for section in doc.sections.all():
            if (theme_id > 0):
                highlights = section.highlights.filter(theme_id = theme_id)
            else:
                highlights = section.highlights.all()
            for highlight in highlights:
                context['highlights'].append(highlight.getAttr())
    context['highlights'].sort(key = lambda x: x["created_at"], reverse=True)
    response['workbench_nugget_list'] = render_to_string("workbench-nuggets.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_nugget_list_partial(request):
    response = {}
    context = {}
    context['highlights'] = []
    highlight_ids = request.REQUEST.get("highlight_ids")
    highlight_ids = highlight_ids.split()
    for highlight_id in highlight_ids:
        highlight = Highlight.objects.get(id = highlight_id)
        context['highlights'].append(highlight.getAttr())
    response['workbench_nugget_list'] = render_to_string("workbench-nuggets.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_load_claim_list_partial(request):
    response = {}
    context = {}
    context['highlights'] = []
    highlight_id = request.REQUEST.get("highlight_id")
    highlightClaims = HighlightClaim.objects.filter(highlight_id = highlight_id)
    context["claims"] = []
    for highlightClaim in highlightClaims:
        claim = highlightClaim.claim
        item = {}
        item['date'] = utils.pretty_date(claim.updated_at)
        item['content'] = unicode(ClaimVersion.objects.filter(claim_id = claim.id)[0]) + " (" + claim.claim_category + ")" 
        item['id'] = claim.id
        item['author_name'] = claim.author.first_name + " " + claim.author.last_name
        item['is_author'] = (request.user == claim.author)
        item['highlight_ids'] = ""
        for highlight in claim.source_highlights.all():
            item['highlight_ids'] += (str(highlight.id) + " ")
        item['highlight_ids'].strip(" ")
        context["claims"].append(item)
    response['workbench_claims'] = render_to_string("workbench-claims.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_edit_claim(request):
    claim_id = request.REQUEST.get("claim_id")
    content = request.REQUEST.get("content")
    claim = Claim.objects.get(id = claim_id)
    claim.content = content
    claim.save()
    response = {}
    return HttpResponse(json.dumps(response), mimetype='application/json')

def api_get_claim_by_theme(request):
    forum = Forum.objects.get(id=request.session['forum_id'])
    response = {}
    context = {}
    theme_id = int(request.REQUEST.get('theme_id'))
    claim_category = request.REQUEST.get('claim_category')
    if (theme_id > 0): 
        claims = Claim.objects.filter(theme_id = theme_id, claim_category = claim_category)
    else:
        claims = Claim.objects.filter(forum = forum, claim_category = claim_category)
    context["claims"] = []
    for claim in claims:
        item = {}
        item['date'] = utils.pretty_date(claim.updated_at)
        item['created_at'] = utils.pretty_date(claim.created_at)
        item['created_at_used_for_sort'] = claim.created_at
        item['content'] = unicode(ClaimVersion.objects.filter(claim_id = claim.id)[0]) 
        item['id'] = claim.id
        item['author_name'] = claim.author.first_name + " " + claim.author.last_name
        item['is_author'] = (request.user == claim.author)
        item['highlight_ids'] = ""
        for highlight in claim.source_highlights.all():
            item['highlight_ids'] += (str(highlight.id) + " ")
        item['highlight_ids'].strip(" ")
        context["claims"].append(item)
    context['claims'].sort(key = lambda x: x["created_at_used_for_sort"], reverse=True)
    response['workbench_claims'] = render_to_string("workbench-claims.html", context)
    return HttpResponse(json.dumps(response), mimetype='application/json')   

def api_others(request):  
    response = {}
    action = request.REQUEST.get('action')
    if action == 'create':
        if not request.user.is_authenticated():
            return HttpResponse("Please log in first.", status=403)
        content = request.REQUEST.get('content')
        content_type = request.REQUEST.get('type')
        start = request.REQUEST.get('start')
        end = request.REQUEST.get('end')
        context_id = request.REQUEST.get('contextId')
        # create highlight object
        context = Entry.objects.get(id=context_id)
        highlight = Highlight(start_pos=start, end_pos=end, context=context, author=request.user)
        highlight.save()
        response['highlight_id'] = highlight.id
        # then create the content
        now = timezone.now()
        if 'actual_user_id' in request.session:
            actual_author = User.objects.get(id=request.session['actual_user_id'])
        else:
            actual_author = None
        if content_type == 'comment':
            if actual_author:
                Post.objects.create(forum_id=request.session['forum_id'], author=actual_author, delegator=request.user,
                    content=content, created_at=now, updated_at=now, highlight=highlight, content_type='comment')
            else:
                Post.objects.create(forum_id=request.session['forum_id'], author=request.user, content=content,
                    created_at=now, updated_at=now, highlight=highlight, content_type='comment')
        elif content_type == 'question':
            if actual_author:
                Post.objects.create(forum_id=request.session['forum_id'], author=actual_author, delegator=request.user,
                    content=content, created_at=now, updated_at=now, highlight=highlight, content_type='question')
            else:
                Post.objects.create(forum_id=request.session['forum_id'], author=request.user, content=content,
                    created_at=now, updated_at=now, highlight=highlight, content_type='question')
        elif content_type == 'claim':
            claim_views._add_claim(request, highlight)
        return HttpResponse(json.dumps(response), mimetype='application/json')
    if action == 'load-doc':
        doc_id = request.REQUEST.get('doc_id')
        doc = Doc.objects.get(id=doc_id)
        response['highlights'] = []
        mytags = set()
        alltags = set()
        for section in doc.sections.all():
            highlights = section.highlights.all()
            for highlight in highlights:
                highlight_info = highlight.getAttr()
                response['highlights'].append(highlight_info)
                if highlight_info['type'] == 'tag':
                    if highlight_info['author_id'] == request.user.id:
                        mytags.add(highlight_info['content'])
                    alltags.add(highlight_info['content'])
        response['html'] = render_to_string('doc-tag-area.html', {'mytags': mytags, 'alltags': alltags})
        return HttpResponse(json.dumps(response), mimetype='application/json')
