from classes.savings import Savings
from classes.expenses import Expenses
import os
import csv
class Budget:
    def __init__(self, name, amount):
        self.name = name
        self.amount = amount
        self.spendings = []
        self.deposits = []
    
    def list_expenses(self):
        for object in self.spendings:
            print(f'{object.date}: {object.amount} spent for {object.type}')
        
    def list_savings(self):
        for object in self.deposits:
            print(f'{object.date}: {object.amount} deposited for {object.type}')
        
    def record_expense(self, expense_data):
        self.spendings.append(Expenses(**expense_data))
        self.amount -= expense_data.amount_spent
        self.save_expense()
    
    def record_savings(self, savings_data):
        self.deposits.append(Savings(**savings_data))
        self.amount += savings_data.amount_deposited
        self.save_deposit()
    
    def save_expense(self):
        my_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(my_path, "../data/expenses.csv")

        with open(path, 'w') as csvfile:
            expense_csv = csv.writer(csvfile, delimiter=',')
            expense_csv.writerow(['date','type','amount'])
            for expense in self.spendings:
                expense_csv.writerow([expense.date_spent, expense.type, expense.amount])

    def save_deposit(self):
        my_path = os.path.abspath(os.path.dirname(__file__))
        path = os.path.join(my_path, "../data/savings.csv")

        with open(path, 'w') as csvfile:
            deposit_csv = csv.writer(csvfile, delimiter=',')
            deposit_csv.writerow(['date','type','amount'])
            for deposit in self.deposits:
                deposit_csv.writerow([deposit.date_deposited, deposit.type, deposit.amount])
        
    def view_amount_left(self):
        print(self.amount)