from django.core.management.base import BaseCommand
from SecureDoomDB.models import Resident
from SecureDoomDB.models import Street
from SecureDoomDB.models import House
from SecureDoomDB.models import Car
from SecureDoomDB.models import InOutRegister
from SecureDoomDB.models import Sensor
from SecureDoomDB.models import Incident
from random import uniform
from random import randint
import datetime
import time
import schedule

class Command(BaseCommand):

    def handle(self, *args, **options):

        def probByHour(): #calcula la probabilidad de robo dependiendo de la hora
            probPerMinute = 0.017361111 #valor de probabilidad por minuto
            probPerHour = probPerMinute * 60 #valor de probabilidad por hora
            d=datetime.datetime.now().time() #momento exacto de ejecucion del comando
            probByMinute=probPerMinute*d.minute #probabilidad calculada en base a la los minutos actuales transcurridos en la actual hora
            #probTotal=probByHour+probByMinute #suma de probaabilidad por hora y probabilidad por minutos
            if d.hour<12: #si la hora actual es antes del medio dia
                probByHour=probPerHour*d.hour #probabilidad calculada en base a la hora actual
                probTotal=probByHour+probByMinute #suma de probaabilidad por hora y probabilidad por minutos
                return 12.5-probTotal
            else:
                probByHour=probPerHour*(d.hour - 12) #probabilidad calculada en base a la hora actual
                probTotal=probByHour+probByMinute #suma de probaabilidad por hora y probabilidad por minutos
                return probTotal

        def probByPeopleIn():
            cars=Car.objects.filter(isIn=False).count()
            percentage=cars*100/6
            realPercentage=percentage*12.5/100
            return realPercentage

        def makeIncident():
            random=randint(1,100)
            now = datetime.datetime.now()
            print(probByHour() + probByPeopleIn())
            if random <= probByHour() + probByPeopleIn():
                print("Generated at " + now.strftime("%Y-%m-%d %H:%M:%S"))
                streets = Street.objects.all()
                cstreets = Street.objects.all().count()
                rstreet = randint(1,cstreets) - 1
                sstreet = streets[rstreet]
                houses = House.objects.filter(private_street=sstreet)
                chouses = House.objects.filter(private_street=sstreet).count()
                rhouse = randint(1,chouses) - 1 
                shouse = houses[rhouse]
                r=randint(1,2)
                if r==1:
                    t=randint(1,2)
                    if t==1:
                        s=Sensor.objects.get(type="Magnetic")
                        Incident.objects.create(house=shouse, street=sstreet, sensor=s)                        
                    elif t==2:
                        s=Sensor.objects.get(type="Movment")
                        Incident.objects.create(house=shouse, street=sstreet, sensor=s)
                elif r==2:
                    s=Sensor.objects.get(type="Movment")
                    Incident.objects.create(street=sstreet, sensor=s)
            else:
                print("Nothing Happend")

        schedule.every(15).seconds.do(makeIncident)
        while True:
            schedule.run_pending()
            time.sleep(15)