# -*- coding: utf-8 -*-

# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# wger Workout Manager is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Workout Manager.  If not, see <http://www.gnu.org/licenses/>.

from rest_framework import viewsets
from rest_framework.decorators import link
from rest_framework.response import Response
from easy_thumbnails.alias import aliases
from easy_thumbnails.files import get_thumbnailer

from wger.exercises.models import Exercise
from wger.exercises.models import Equipment
from wger.exercises.models import ExerciseCategory
from wger.exercises.models import ExerciseImage
from wger.exercises.models import ExerciseComment
from wger.exercises.models import Muscle


class ExerciseViewSet(viewsets.ModelViewSet):
    '''
    API endpoint for workout objects
    '''
    model = Exercise


class EquipmentViewSet(viewsets.ReadOnlyModelViewSet):
    '''
    API endpoint for workout objects
    '''
    model = Equipment


class ExerciseCategoryViewSet(viewsets.ReadOnlyModelViewSet):
    '''
    API endpoint for workout objects
    '''
    model = ExerciseCategory


class ExerciseImageViewSet(viewsets.ModelViewSet):
    '''
    API endpoint for exercise image objects
    '''
    model = ExerciseImage

    @link()
    def thumbnails(self, request, pk):
        '''
        Return a list of the image's thumbnails
        '''
        image = ExerciseImage.objects.get(pk=pk)
        thumbnails = {}
        for alias in aliases.all():
            t = get_thumbnailer(image.image)
            thumbnails[alias] = {'url': t.get_thumbnail(aliases.get(alias)).url,
                                 'settings': aliases.get(alias)}
        return Response(thumbnails)


class ExerciseCommentViewSet(viewsets.ReadOnlyModelViewSet):
    '''
    API endpoint for workout objects
    '''
    model = ExerciseComment


class MuscleViewSet(viewsets.ReadOnlyModelViewSet):
    '''
    API endpoint for workout objects
    '''
    model = Muscle
@@ -1296,39 +1296,40 @@
 PI endpoint for 
-workout
+exercise
  objects%0A    '''
@@ -1351,16 +1351,194 @@
 ercise%0A%0A
+    def pre_save(self, obj):%0A        '''%0A        Set the license data%0A        '''%0A        if not obj.license_author:%0A            obj.license_author = self.request.user.username%0A%0A
 %0Aclass E
@@ -1606,38 +1606,40 @@
 PI endpoint for 
-workou
+equipmen
 t objects%0A    ''
@@ -1747,39 +1747,51 @@
 PI endpoint for 
-workout
+exercise categories
  objects%0A    '''
@@ -2420,16 +2420,194 @@
 nails)%0A%0A
+    def pre_save(self, obj):%0A        '''%0A        Set the license data%0A        '''%0A        if not obj.license_author:%0A            obj.license_author = self.request.user.username%0A%0A
 %0Aclass E
@@ -2681,38 +2681,47 @@
 PI endpoint for 
-workou
+exercise commen
 t objects%0A    ''
@@ -2837,15 +2837,14 @@
 for 
-workout
+muscle
  obj

Set the license author when saving exercises or their images