from django.db import models
from PIL import Image
from lrntkr.managers import CourseManager


class University(models.Model):
    name = models.CharField(max_length=70)
    address = models.CharField(max_length=120, blank=True, null=True)
    country = models.CharField(max_length=30)
    website = models.URLField()

    def __unicode__(self):
        return (self.name)


class Instructor(models.Model):
    name = models.CharField(max_length=70)
    specilist = models.CharField(max_length=120)
    about = models.CharField(max_length=200)
    email = models.EmailField()
    universities = models.ManyToManyField(University)

    def __unicode__(self):
        return self.name


class Course(models.Model):
    title = models.CharField(max_length=70)
    stream = models.CharField(max_length=70)
    universities = models.ManyToManyField(University)
    instruct = models.ManyToManyField(Instructor)
    description = models.CharField(max_length=70)
    start_date = models.CharField(max_length=30)
    work_load = models.CharField(max_length=30)
    logo = models.ImageField(upload_to='logos', blank=True, null=True)
    intro = models.URLField(verify_exists=False, max_length=225)
    initiative = models.URLField(verify_exists=False, max_length=225)
    initiator = models.CharField(max_length=50)
    courseinfo = models.BooleanField(db_index=True, default=True)

    objects = CourseManager()

    def save(self, size=(200, 200)):
        if not self.id and not self.logo:
            return

        super(Course, self).save()

        pw = self.logo.width
        ph = self.logo.height
        nw = size[0]
        nh = size[1]

        # only do this if the image needs resizing
        if (pw, ph) != (nw, nh):
            filename = str(self.logo.path)
            image = Image.open(filename)
            pr = float(pw) / float(ph)
            nr = float(nw) / float(nh)

            if pr > nr:
                # photo aspect is wider than destination ratio
                tw = int(round(nh * pr))
                image = image.resize((tw, nh), Image.ANTIALIAS)
                l = int(round(( tw - nw ) / 2.0))
                image = image.crop((l, 0, l + nw, nh))
            elif pr < nr:
                # photo aspect is taller than destination ratio
                th = int(round(nw / pr))
                image = image.resize((nw, th), Image.ANTIALIAS)
                t = int(round(( th - nh ) / 2.0))
                print((0, t, nw, t + nh))
                image = image.crop((0, t, nw, t + nh))
            else:
                # photo aspect matches the destination ratio
                image = image.resize(size, Image.ANTIALIAS)

            image.save(filename)

    def __unicode__(self):
        return self.title





class Student(models.Model):
    name = models.CharField(max_length=70)
    email_id = models.CharField(max_length=60)
    user_name = models.CharField(max_length=50)
    user_pass = models.CharField(max_length=30)
    user_repass = models.CharField(max_length=30)
    birth_date = models.DateField(blank=True, null=True)
    gender = models.CharField(max_length=5)
    address = models.CharField(max_length=120)
    contact_no = models.IntegerField(max_length=11)

    def __str__(self):
        return self.name

