__author__ = 'JBreman'
__project__ = 'WaterDemandModel'

import numpy as np
import C_ApplianceType as Event
import calcAndSampleCumData as CD
import sampleDataBin as SB
import sampling_db as DB

class WashMachine(Event.ApplianceType):
    """

    """

    # Objects used/manipulated by class
    __fraction_top_loader = 0                           # percentage (as a fraction) of toploader washing machine's for an area e.g. Australia or Victoria. --> i.e. represents that 70# of Aus homes have a top loader.
    __start_time = 0                                    # The time a wash event begines
    __wash_volume = 0                                   # the chosen volume of a wash
    __machine_type = ""                                 # the type of WM (front/top loader)

    def __init__(self,start_time,unit_type):

        Event.ApplianceType.__init__(self,"washing_machine")
        self.__wash_volume = 0
        self.__machine_type = ""

         # Attach the start time to a object:
        self.__start_time = start_time
        self.__unit_type = unit_type

        #Call class methods:
        self.washMachineType()
        self.calcWashVolume()
        self.getVolume()
        self.getVolume()

    def washMachineType(self):
        """
        This method determines if the washing machine (WM) is a front loader or a top loader.
        INPUT: a fraction that represents the % of houses that have a top loader for an area (e.g. Australia). Assigned to self.__fraction_top_loader.

        It does this by sampling a random number between 0-1. If rand < top loader fraction, the WM becomes a top loader. Otherwise if rand > top loader fraction, the WM becomes a front loader.

        OUTPUT: the method assigns the correct wash volume distribution input data file to the object self.__machine_type.
        """
        prob_top_loader = DB.SamplingDatabase.Instance().getDB("FRACTION_TOP_LOADERS_IN_AUST")               # get probability of house have a top loader over a front loader from config
        rand = np.random.sample()                                   # generate a random number between 0-1

        if rand < prob_top_loader:                                  # rand < probability: (the bigger the probability, the more likely it is this will be true)
            self.__machine_type = DB.SamplingDatabase.Instance().getDB("TOP_LOADER_WM_VOLUME_DATABASE")            # WM is a top loader --> assign relevant volume distribution file for top loader to object

        else:                                                       # else if rand > probability:
            self.__machine_type = DB.SamplingDatabase.Instance().getDB("FRONT_LOADER_WM_VOLUME_DATABASE")      # WM is a front loader  --> assign relevant volume distribution file for front loader to object

    def calcWashVolume(self):
        """
        This method determines the volume of water used by washing one load of clothes.
        INPUTS: the volume distribution file for the relevant WM type (front/top loader) --> determined in def washMachineType

        OUTPUTS: A float number of volume used for one wash. Assigned to the object self.__wash_volume
        """
        self.__wash_volume = CD.getSampledCumValue(self.__machine_type)         # sample wash volume from a distribution.

        if type(self.__wash_volume) == str:                                         # if the bin selected represents a range (e.g 30-40L):

            self.__wash_volume = SB.sampleDataBin(self.__wash_volume)                     # then randomly sample between this range.


    def getVolume(self):
        """
        :return: the volume of water generated by the WM
        """
        return self.__wash_volume

    def getStartTime(self):
        """
        :return: the time the wash event begins (hrs). Passed from the actor to the washing machine through the __init__ method.
        """
        return self.__start_time

    def getUnitType(self):

        return self.__unit_type


if __name__ == "__main__":

    output = WashMachine(3)
    output.washMachineType()
    # output.washMachineType()

    # print output.getVolume

    print("h")


