from Car import Car

import copy
from random import randint

class Flow:
    def __init__(self,ID,route,start,vph,randomVal, burstInterval, burstTime, dynSpeed, IA,parentMap,parentController):
        self.route = route
        self.id = ID
        self.startTime = start
        self.vph = float(vph)
        self.randomVal = int(randomVal)
        self.adjVPH = self.vph + randint(0,self.randomVal)
        self.carModel = Car("model",self.route,start,dynSpeed,IA,parentMap,parentController,None)
        self.carsSpawned = 0
        self.priority = 0
        self.burstInterval = float(burstInterval)
        self.burstTime = float(burstTime)
        self.cc = parentController

    def prepareRoute(self):
        self.carModel.prepareRoute()
    
    def shouldSpawn(self, t):
        f = self.carsSpawned / (t/3600)
        return f < self.adjVPH and t%self.burstInterval < self.burstTime

    def spawnCar(self):
        newCar = copy.copy(self.carModel)
        newCar.id = self.id + str(self.carsSpawned)
        self.carsSpawned += 1
        self.adjVPH = self.vph + randint(0,self.randomVal)
        self.priority = 0
        return newCar

    def addCar2Counter(self):
        self.carsSpawned += 1

    def backlog(self, t):
        carSpawnedTh = self.adjVPH * (t/3600)
        return carSpawnedTh - self.carsSpawned
