from django.db import models
from .utils import get_filtered_image
from PIL import Image
from io import BytesIO
from django.core.files.base import ContentFile
import numpy as np

ACTION_CHOICES = (
    ('NO_FILTER', 'no_filter'),
    ('COLORIZED', 'colorized'),
    ('GRAYSCALE', 'grayscale'),
    ('BLURRED', 'blurred'),
    ('BINARY', 'binary'),
    ('INVERT', 'invert')
)

# Create your models here.
class Upload(models.Model):
    image = models.ImageField(upload_to = 'image')
    action =  models.CharField(max_length= 50, choices = ACTION_CHOICES)
    updated = models.DateTimeField(auto_now = True)
    created = models.DateTimeField(auto_now_add = True)
    
    def __str___(self):
        return str(self.id)
    
    def save(self, *args, **kwargs):
        # open image
        pil_img = Image.open(self.image)
        
        cv_img = np.array(pil_img)
        
        img =get_filtered_image(cv_img, self.action)
        
        im_pil = Image.fromarray(img)
        
        buffer = BytesIO()
        im_pil.save(buffer, format="png")
        image_png = buffer.getvalue()
        
        self.image.save(str(self.image), ContentFile(image_png), save=False)
        
        super().save(*args, **kwargs) 