from .base_agent import BaseAgent
class AutoProductionAgent(BaseAgent):
    """
    Automaticaly produces commodity if in business inventory
    """    


    def __init__(self,sim,business,worker=-1,employment_rate=0) -> None:
        super().__init__(sim)
        self.business=business
        self.prod=business.production
        self.worker=worker
        self.employment_rate=employment_rate
        self.employment_index=worker
        self.target=0
        self.prod_counter=0

    def set_worker(self,workers):
        if workers>1:
            self.worker=workers
        elif workers==-1:
            self.worker=-1
        else:
            self.worker=1
    def set_target(self,qty):
        self.target=qty
    def can_produce(self):
        # how much can we produce
        minprod=[]
        
        for k,v in self.prod["prod"].items():
            minprod.append(self.business.inventory[k]/v)
        possibleProds=int(min(minprod))
       
        return possibleProds*self.prod["amount"]
    
    def should_produce(self):
        # check if we should produce
        if self.business.resource_in_possesion()>=self.target:
            return 0
       
        return self.can_produce()
        
    def tick(self,step,epi):
        can=self.should_produce()
        if can==0:
            return
        
        run=True
        i=self.worker
        while run:
            if self.should_produce()==0:
                return
            if i==0:
                return
            # remove cost from inventory
            
            for k,cost in self.prod["prod"].items():
                self.business.inventory[k]-=cost
            # add commodity
            self.business.inventory[self.prod['name']]+=self.prod["amount"]
            self.prod_counter+=1
            i-=1

    def reset_qty(self):
        self.prod_counter=0
    def qty(self):
        return self.prod_counter