#!/usr/bin python3
from datetime import *
import math

class TempBase:
    """Defines the base class for calculating the temperature of a substance that is cooling
    at a fixed rate"""

    AMBIENT_TEMPERATURE = 21.0


    def __init__(self, start_temp, cooling_rate, ambient_temp=AMBIENT_TEMPERATURE):
        """Constructor for the temperature base class"""
        self.refresh(start_temp, cooling_rate, ambient_temp)

    def refresh(self, start_temp, cooling_rate, ambient_temp=AMBIENT_TEMPERATURE):
        """Re-starts the cooling process"""
        self.start_time = datetime.now()
        self.t_0 = start_temp
        self.t_a = ambient_temp
        self.rate = cooling_rate

    def get_current_temp(self):
        """Gets the current temperature"""
        return self.get_temp_after(self.get_minutes_since_start())

    def get_temp_after(self, minutes):
        """Gets the temperature after a given number of minutes"""
        exponent = math.exp(self.rate * minutes)
        return (self.t_a + ((self.t_0 - self.t_a) * exponent))

    def get_minutes_at(self, time):
        """Calculates and returns how many minutes have passed at a given datetime"""
        return (time - self.start_time).seconds / 60.0

    def get_minutes_since_start(self):
        """Gets the number of minutes passed since the time was started"""
        return self.get_minutes_at(datetime.now())

    def get_minutes_to_temperature(self, temp, start_time=datetime.now()):
        """Gets the time until a temperature is reached. Returns None if it is unreachable"""
        minutes = 0
        temp_0 = self.get_temp_after(minutes)
        if temp < TempBase.AMBIENT_TEMPERATURE:
            raise ValueError(
                "Temperature (%4.2f°C) cannot be less than the ambient temperature (%4.2f°C)" % \
                (temp, TempBase.AMBIENT_TEMPERATURE))

        return math.log((temp - TempBase.AMBIENT_TEMPERATURE) / (temp_0 - TempBase.AMBIENT_TEMPERATURE)) / self.rate

