"""
This module hosts all the basic calculations that can be done, basically
anything that does not require database queries
"""
from django.http import HttpResponse

from rest_framework import viewsets
from rest_framework.decorators import list_route
from rest_framework.response import Response

from PIL import Image
from PIL import ImageDraw

from terrarium.utility.pot import circular_pot_calculation

class CalculationViewSet(viewsets.ViewSet):
    """
    This viewset is responsible for the ``calculation`` endpoint.
    """

    def list(self, request):
        """
        GET method implementation of listing all possible calculations
        """
        functions = {}
        functions['circular_pot'] = "Calculate the number of seed clusters suitable for a certain pot size"
        return Response(functions)

    @list_route(methods=['get'])
    def circular_pot(self, request):
        """
        GET method implementation of calculating pot size

        Get args:
            pot_radius: Radius of a pot
            seed_radius: Optimal radius of a cluster of seeds

        Returns:
            A integer of the optimal number of clusters or an error message if
            not all parameters are given
        """
        pot_radius = request.GET.get('pot_radius', None)
        seed_radius = request.GET.get('seed_radius', None)

        # check to see if all parameters are met
        if pot_radius and seed_radius:
            pot_radius = int(pot_radius)
            seed_radius = int(seed_radius)
        else:
            return Response("Please specify pot_radius, and seed_radius in your parameters")

        # calculate number of clusters
        cluster_count, clusters = circular_pot_calculation(pot_radius,
                seed_radius)
        return Response({"cluster_count": cluster_count,
                         "clusters": clusters})

    @list_route(methods=['get'])
    def circular_pot_image(self, request):
        """
        GET method implementation of calculating pot size

        Similar to circular_pot, but returns an image instead
        """
        pot_radius = request.GET.get('pot_radius', None)
        seed_radius = request.GET.get('seed_radius', None)

        # check to see if all parameters are met
        if pot_radius and seed_radius:
            pot_radius = int(pot_radius)
            seed_radius = int(seed_radius)
        else:
            return Response("Please specify pot_radius, and seed_radius in your parameters")
        
        # variables required for drawing
        canvas_height = 500
        canvas_width = 500
        canvas_center_x = canvas_width / 2
        canvas_center_y = canvas_height / 2
        ratio = canvas_height / 2 / pot_radius

        # draw circles
        image = Image.new('RGBA', (canvas_width, canvas_height), 'white')
        draw = ImageDraw.Draw(image)
        clusters = self.circular_pot(request).data['clusters']
        for cluster in clusters:
            draw.ellipse((canvas_center_x + (cluster['x'] - seed_radius) * ratio,
                           canvas_center_y + (cluster['y'] - seed_radius) * ratio,
                           canvas_center_x + (cluster['x'] + seed_radius) * ratio,
                           canvas_center_y + (cluster['y'] + seed_radius) * ratio),
                           fill="#009933")
        response = HttpResponse(content_type="image/jpeg")
        image.save(response, "JPEG")
        return response

@@ -1426,32 +1426,53 @@
 nd seed_radius:%0A
+            try:%0A    
             pot_
@@ -1472,34 +1472,36 @@
    pot_radius = 
-in
+floa
 t(pot_radius)%0A  
@@ -1502,32 +1502,36 @@
 us)%0A            
+   
+ 
 seed_radius = in
@@ -1520,36 +1520,156 @@
   seed_radius = 
-int(
+float(seed_radius)%0A            except ValueError:%0A                return Response(%22Please enter numbers for pot_radius, and 
 seed_radius)%0A   
@@ -1655,32 +1655,33 @@
  and seed_radius
+%22
 )%0A        else:%0A
@@ -2437,32 +2437,53 @@
 nd seed_radius:%0A
+            try:%0A    
             pot_
@@ -2491,18 +2491,20 @@
 adius = 
-in
+floa
 t(pot_ra
@@ -2513,32 +2513,36 @@
 us)%0A            
+   
+ 
 seed_radius = in
@@ -2543,12 +2543,132 @@
 s = 
-int(
+float(seed_radius)%0A            except ValueError:%0A                return Response(%22Please enter numbers for pot_radius, and 
 seed
@@ -2666,32 +2666,33 @@
  and seed_radius
+%22
 )%0A        else:%0A

Validate input value for circular pot calculation