# -*- coding: utf-8 -*-
import json

from dajaxice.decorators import dajaxice_register
from django.template.loader import render_to_string
from tasks.models import ApprenticeTaskAnswer, TaskMarkCriteria
from users.decorators import require_apprentice


@require_apprentice
@dajaxice_register
def submit_likes(request, task_mark_criteria_ids, task_answer_id):
    try:
        task_answer = ApprenticeTaskAnswer.objects.completed().get(id=task_answer_id)
        apprentice_video = task_answer.video_2
        apprentice = request.user
        task_mark_criteria = TaskMarkCriteria.objects.filter(task=task_answer.task, id__in=task_mark_criteria_ids)
        for tmc in task_mark_criteria:
            tmc.apprentice_scores.create(apprentice=apprentice, apprentice_video=apprentice_video)
        apprentice_video.apprentices.add(apprentice)
        status = 'ok'
        atmcav = task_answer.video_2.get_likes()
        social_vote_list_html = render_to_string('tasks/modules/_social_vote_list.html', {'atmcav': atmcav})
    except ApprenticeTaskAnswer.DoesNotExist:
        status = 'error'
        social_vote_list_html = ''

    return json.dumps({'status': status, 'social_vote_list_html': social_vote_list_html})
