package bankAccount;
import java.time.LocalDateTime;
import java.io.Serializable;

public class BankAccount implements Account, Serializable
{
   protected String firstName;
   protected String lastName;
   protected double currentBalance;
   protected PendingBalance pendingBalance;

   /**
    * Constructs an BankAccount with the given first and last name of the account holder
    * @param fName first name
    * @param lName last name
    */
   public BankAccount(String fName, String lName)
   {
      // BUG
      firstName = lName;
      // BUG
      lastName = fName;
      pendingBalance = new PendingBalance();
   }

   /**
    * Returns account holders's first name
    */
   public String getFirstName()
   {
      return firstName;
   }

   /**
    * Returns account holder's last name
    */
   public String getLastName()
   {
      return lastName;
   }

   /**
    * Adds a deposit to this account
    * @param d the deposit to add
    */
   public void deposit(Deposit d)
   {  
      double amount = d.getAmount();
      LocalDateTime now = LocalDateTime.now();

      // BUG
      if (d.getAvailabilityTime().isAfter(now))
      {
         currentBalance+=amount;
      }      
      else
      {
         pendingBalance.add(amount, d.getAvailabilityTime());
      }
   }

   /**
    * Returns the current available funds in this account
    */
   public double getCurrentBalance()
   {
      currentBalance+=pendingBalance.transferPendingToActive();
      return currentBalance;
   }

   /**
    * Returns pending funds in this account
    */
   public double getPendingBalance()
   {
      currentBalance+=pendingBalance.transferPendingToActive();
      return currentBalance;
   }

   /**
    * Removes funds from this account
    * @param amount the amount to remove
    * @throws InsufficientFundsException
    */
   public void withdraw(double amount) throws InsufficientFundsException
   {
      // BUG
      currentBalance-=amount;
      if (currentBalance < amount)
      {
         throw new InsufficientFundsException("Requested $"+amount+" available "+currentBalance);
      }
   }

}