from django.db import models
from django.contrib import admin

import datetime, time
from mptt.models import MPTTModel, TreeForeignKey


class Location(MPTTModel):
    name = models.CharField(max_length=45)
    description = models.TextField(blank=True)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):
        return self.name

    class MPTTMeta:
        order_insertion_by = ['name'] 

class LoadType(MPTTModel):
    name = models.CharField(max_length=45)
    abbreviation = models.CharField(max_length=10, blank=True)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):
        return self.name

    class MPTTMeta:
        order_insertion_by = ['name']



class Eguage(models.Model):
    URL = models.CharField(max_length=200,unique=True,default="http://",help_text="Must start with http:// and end with /. \n Example: http://egauge2880.egaug.es/")
    username = models.CharField(max_length=50,null=True,blank=True)
    password = models.CharField(max_length=50,null=True,blank=True)
    name = models.CharField(max_length=100,unique=True)
    checked_at = models.IntegerField(blank=True,null=True,default="0",editable=False)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    def __unicode__(self):
        return self.name


class Eport(models.Model):
    name = models.CharField(max_length=45)
    description = models.CharField(max_length=1024, blank=True)
    eguage = models.ForeignKey(Eguage)
    location = models.ForeignKey(Location)
    load_type = models.ForeignKey(LoadType)
    voltage = models.IntegerField(default=480,null=True,blank=True)
    phases = models.IntegerField(default=3,null=True,blank=True)
    amperage = models.IntegerField(default=600,null=True,blank=True)
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)



class WeatherData(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    condition = models.CharField(max_length=25)
    temp_f = models.DecimalField(max_digits=5, decimal_places=1)
    feelslike_f = models.DecimalField(max_digits=5, decimal_places=1, blank=True)
    relative_humidity = models.DecimalField(max_digits=5, decimal_places=1, blank=True)
    wind_mph = models.IntegerField(blank=True)
    wind_dir = models.CharField(max_length=10, blank=True)
    pressure_mb = models.IntegerField(blank=True)
    uv = models.FloatField(blank=True)
    solarradiation = models.DecimalField(max_digits=5, decimal_places=1, blank=True)
    precip_1hr_in = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
    precip_today_in = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
    
    def created_at_utc(self):
        return self.created_at.mktime(self.created_at.timetuple())


class EnergyData(models.Model):
    created_at = models.DateTimeField(auto_now_add = True)
    time_stamp = models.DateTimeField()
    eguage_id = models.ForeignKey(Eguage)
    eport_id = models.ForeignKey(Eport)
    watts = models.BigIntegerField()
