from repositories.repository import Repository
from domain.domain import Expense
import random

class MemoryRepository(Repository):
    def __init__(self):
        super().__init__()
        self.list_of_expenses=self.generate_elements(10)
        self.previous_list_of_expenses=[]
    def insert_element(self,expense):
        """
        :params:
            expense - Expense
        The function takes the current state of the repo and appends it to the stack
        The function appends the expense to the current list of expenses
        """
        self.previous_list_of_expenses.append(self.list_of_expenses)
        self.list_of_expenses.append(expense)
    def get_elements(self):
        return self.list_of_expenses
    def filter_elements(self,value):
        self.previous_list_of_expenses.append(self.list_of_expenses)
        self.list_of_expenses=list(filter(lambda x:x.get_money()>value,self.list_of_expenses))
    def generate_elements(self,number)->list:
        types={1:"Water",2:"Gas",3:"Electricity"}
        return [Expense(random.randint(1,30),random.randint(1,100),types[random.randint(1,3)]) for _ in range(number)]
    def undo(self):
        self.list_of_expenses=self.previous_list_of_expenses.pop()