

class PhaseCalc():
    
    def __init__(self, cummulative = False):
        
        self.cummulative = cummulative
        self.phases = []
        self.tims = []
        
    def add_phase(self, t, func):
        self.phases.append(func)
        self.tims.append(t)
        
    def __call__(self, t):
        if self.cummulative:
            i = 0
            cumt = 0
            val = 0
            while self.tims[i] < t - cumt:
                curt = min(t - cumt, self.tims[i]) 
                val += self.phases[i](curt)
                cumt += self.tims[i]
                i += 1
            val += self.phases[i](t - cumt)
            return val
        else:
            i = 0
            cumt = 0
            val = 0
            while self.tims[i] < t - cumt:
                curt = max(t - cumt, self.tims[i])
                cumt += self.tims[i]
                i += 1
            val = self.phases[i](t - cumt)
            return val
        
    