"""This module holds the serializers of the ``workshops`` app."""

import random

from rest_framework import serializers

from mtp.apps.accounts.constants import INTERNAL_GROUP_TRAINEE, INTERNAL_GROUP_TRAINER

from .models import (
    Workshop,
    WorkshopStep,
    WorkshopStepQuestion,
    WorkshopStepImage,
    WorkshopStepQuestionAnswer,
)

class CompatibilityWorkshopStepQuestionAnswerSerializer(serializers.ModelSerializer):
    """Serializer for the ``mtp.apps.workshop.models.WorkshopStepQuestionAnswer`` model."""

    good = serializers.SerializerMethodField()
    order = serializers.SerializerMethodField()

    class Meta:
        model = WorkshopStepQuestionAnswer
        fields = [
            'id',
            'label',
            'good',
            'points',
            'order'
        ]

    def get_good(self, answer):
        return answer.is_correct

    def get_order(self, answer):
        return answer.sort_order


class CompatibilityWorkshopStepQuestionSerializer(serializers.ModelSerializer):
    """Serializer for the ``mtp.apps.workshop.models.WorkshopStepQuestion`` model."""

    label = serializers.SerializerMethodField()
    type = serializers.SerializerMethodField()
    options = serializers.SerializerMethodField()
    order = serializers.SerializerMethodField()
    answers = serializers.SerializerMethodField()

    class Meta:
        model = WorkshopStepQuestion
        fields = [
            'id',
            'label',
            'description',
            'is_multiple_choices',
            'type',
            'options',
            'order',
            'answers',
        ]

    def get_label(self, question):
        return question.title

    def get_type(self, question):
        return 'quizz'

    def get_options(self, question):
        return []

    def get_order(self, question):
        return question.sort_order

    def get_answers(self, question):
        answers_qs = WorkshopStepQuestionAnswer.objects.filter(
            question=question).order_by('sort_order').non_deleted()
        answers = CompatibilityWorkshopStepQuestionAnswerSerializer(
            answers_qs, many=True, read_only=True)

        answers_list = answers.data

        if (question.workshop.is_questions_answers_order_random
            or question.is_answers_order_random):
            random.shuffle(answers_list)

        return answers_list


class CompatibilityWorkshopStepImageSerializer(serializers.ModelSerializer):
    """Serializer for the ``mtp.apps.workshop.models.WorkshopStepImage`` model."""

    label = serializers.CharField(source='title')
    type = serializers.SerializerMethodField()
    options = serializers.SerializerMethodField()
    order = serializers.IntegerField(source='sort_order')
    description = serializers.ImageField(source='image')
    answers = serializers.SerializerMethodField()

    class Meta:
        model = WorkshopStepImage
        fields = [
            'id',
            'label',
            'description',
            'type',
            'options',
            'order',
            'answers',
        ]

    @staticmethod
    def get_type(question):
        return 'img'

    @staticmethod
    def get_options(question):
        return []

    @staticmethod
    def get_answers(question):
        return []


class CompatibilityWorkshopSerializer(serializers.ModelSerializer):
    """Serializer for the ``mtp.apps.workshop.models.Workshop`` model."""

    title = serializers.SerializerMethodField()
    type = serializers.SerializerMethodField()
    order = serializers.SerializerMethodField()
    groups = serializers.SerializerMethodField()
    has_score = serializers.SerializerMethodField()
    answers_required = serializers.SerializerMethodField()
    module = serializers.SerializerMethodField()
    preview = serializers.SerializerMethodField()
    questions = serializers.SerializerMethodField()

    class Meta:
        model = Workshop
        fields = [
            'id',
            'original_version',
            'title',
            'type',
            'order',
            'groups',
            'has_score',
            'needs_approval',
            'answers_required',
            'is_assessing',
            'module',
            'preview',
            'questions'
        ]

    def get_title(self, workshop):
        return workshop.name

    def get_type(self, workshop):
        return 'livevoting' if workshop.is_broadcastable else 'workshop'

    def get_order(self, workshop):
        return workshop.sort_order

    def get_groups(self, workshop):
        """Get the allowed user's groups for this workshop

        A "broadcastable" workshop can only be launched by the trainers.

        """

        if workshop.is_broadcastable:
            return [INTERNAL_GROUP_TRAINER]
        else:
            return [INTERNAL_GROUP_TRAINER, INTERNAL_GROUP_TRAINEE]

    def get_has_score(self, workshop):
        return workshop.enable_score

    def get_answers_required(self, workshop):
        return workshop.are_questions_mandatory

    def get_module(self, workshop):
        return 'workshop'

    def get_preview(self, workshop):
        return 'workshop-preview.png'

    @staticmethod
    def get_questions(workshop):
        """Get the steps of current workshop.

        Notes
        -----
        We can't name this method `get_steps` because of compatibility with iOS client.

        Returns
        -------
        list
            Return the steps list.

        """
        steps_queryset = WorkshopStep.objects.non_deleted().filter(workshop=workshop).order_by('sort_order')

        steps_list = []
        for step in steps_queryset:
            if isinstance(step, WorkshopStepQuestion):
                steps_list.append(CompatibilityWorkshopStepQuestionSerializer(step, read_only=True).data)
            elif isinstance(step, WorkshopStepImage):
                steps_list.append(CompatibilityWorkshopStepImageSerializer(step, read_only=True).data)

        if workshop.is_steps_order_random:
            random.shuffle(steps_list)

        return steps_list
