""" Resource used for import/export roleplays data.
A documentation about Resource is available in import_export.resources module.
"""

from mtp.apps.import_export import resources
from mtp.apps.roleplays.models import Criterion, Roleplay, Step


class CriterionResource(resources.Resource):
    fields = (
        ('criterion_type', 'Criterion type'),
        ('name', 'Criterion name'),
        ('description', 'Criterion description'),
        'dimensions__code 1', 'dimensions__code 2', 'dimensions__code 3',
        'dimensions__code 4', 'dimensions__code 5', 'dimensions__code 6',
        'dimensions__code 7', 'dimensions__code 8', 'dimensions__code 9',
        'dimensions__code 10',
    )
    fields_to_get = ('name',)
    fields_to_translate = ('name', 'description')
    fk_fields_to_translate = ('step',)


class StepResource(resources.Resource):
    fields = (
        ('name', 'Step name'),
        ('description', 'Step description'),
        ('duration', 'Step duration')
    )
    fields_to_get = ('name',)
    fields_to_translate = ('name', 'description')
    fk_fields_to_translate = ('roleplay',)
    nested_resource = (('criteria', CriterionResource),)

    @staticmethod
    def get_duration(obj):
        if obj.duration:
            return obj.duration.seconds / 60
        else:
            return 0


class RoleplayResource(resources.Resource):
    fields = (
        ('name', 'Roleplay name'),
        ('weight', 'Roleplay weight'),
        ('description', 'Roleplay description'),
        ('criteria_rating_type', 'Criteria rating type'),
        ('criteria_rating_scale', 'Criteria rating scale')
    )
    fields_to_get = ('name', 'training')
    fields_to_translate = ('name', 'description')
    nested_resource = (('steps', StepResource),)


resources.register(Roleplay, RoleplayResource)
resources.register(Step, StepResource)
resources.register(Criterion, CriterionResource)
