package com.veggie.src.java.database.prototype;

import java.util.List;
import java.util.ArrayList;

import com.veggie.src.java.Transaction;
import com.veggie.src.java.ReservationTransaction;
import com.veggie.src.java.RentalTransaction;
import com.veggie.src.java.Account;
import com.veggie.src.java.database.TransactionDatabaseManager;
import com.veggie.src.java.MediaItem;


public class PrototypeTransactionDatabaseManager extends PrototypeDatabaseManager implements TransactionDatabaseManager {

    private PrototypeDB db;

    public PrototypeTransactionDatabaseManager(PrototypeDB db) {
      this.db = db;
    }

    public void addTransaction(Transaction transaction) {
        int transactionId = db.getTransactionId() + 1;
        transaction.setId(transactionId);
        db.getTransactionTable().put(transactionId, transaction);
        db.setTransactionId(transactionId);
    }

    public boolean checkReserved(int itemId) {
        return db.getItemTable().get(itemId).getStatus() == MediaItem.RESERVED;
    }

    public int getNumberReservations(int userId) {
        int count = 0;
        for (Integer transactionId : db.getTransactionTable().keySet()) {
            Transaction transaction = db.getTransactionTable().get(transactionId);
            if (transaction instanceof ReservationTransaction) {
                if (getTransactionUser(transactionId).getId() == userId) {
                    count += transaction.getStatus() == Transaction.ACTIVE ? 1 : 0;
                }
            }
        }
        return count;
    }

    public void finalizeTransaction(int transactionId) {
        Transaction transaction = db.getTransactionTable().get(transactionId);
        transaction.resolve();
    }

    public Account getTransactionUser(int transactionId) {
        return db.getTransactionTable().get(transactionId).getPatronAccount();
    }

    public Account getCurrentRenter(int itemId) {
        Account out = null;
        for (Integer transactionId : db.getTransactionTable().keySet()) {
            Transaction transaction = db.getTransactionTable().get(transactionId);
            if (transaction instanceof RentalTransaction && transaction.getStatus() == Transaction.ACTIVE) {
                RentalTransaction rental = (RentalTransaction) transaction;
                if (rental.getItem().getId() == itemId) {
                    out = rental.getPatronAccount();
                }
            }
        }
        return out;
    }

    public List<Transaction> getUserTransactions(int userId) {
        List<Transaction> out = new ArrayList<>();

        for (Integer transactionId : db.getTransactionTable().keySet()) {
            Transaction transaction = db.getTransactionTable().get(transactionId);
            if (transaction.getPatronAccount().getId() == userId) {
                out.add(transaction);
            }
        }

        return out;
    }

    public List<ReservationTransaction> getUserReservations(int userId) {
        List<ReservationTransaction> out = new ArrayList<>();

        for (Integer transactionId : db.getTransactionTable().keySet()) {
            Transaction transaction = db.getTransactionTable().get(transactionId);
            if (transaction.getPatronAccount().getId() == userId) {
                if (transaction instanceof ReservationTransaction)
                out.add((ReservationTransaction)transaction);
            }
        }
        return out;
    }

    public void finalizeReservation(int id, int itemId) {
        for(Integer transactionId : db.getTransactionTable.keySet()) {
            Transaction transaction = db.getTransactionTable().get(transactionId);
            if(transaction.getPatronAccount().getId() == userId && transaction.getId() == itemId){
                if(transaction instanceof ReservationTransaction)
                    transaction.resolve();
            }
        }
    }
}

@@ -3478,17 +3478,21 @@
 ion(int 
-i
+userI
 d, int i
@@ -3558,16 +3558,18 @@
 ionTable
+()
 .keySet(

i'm sorry.