package bankAccount;

import java.util.ArrayList;
import java.util.Collections;
import java.time.LocalDateTime;

public class PendingBalance
{
   private ArrayList<PendingItem> items;

   /**
    * Constructs empty PendingBalance object
    */
   public PendingBalance()
   {
      items = new ArrayList<PendingItem>(10);
   }

   /**
    * Returns total amount of this PendingBalance
    */
   public double getPendingBalance()
   {
      double total = 0;
      for (int i = 0; i < items.size(); i++)
      {
         total+=items.get(i).getAmount();
      }
      return total;
   }

   /**
    * Returns amount from this PendingBalance that is no longer pending
    * removes non-pending amount from this PendingBalance
    */
   public double transferPendingToActive()
   {
      double total = 0;
      for (int i = 0; i < items.size(); i++)
      {
         PendingItem item = items.get(i);
         if (!item.isStillPending())
         {
            total+=item.getAmount();
            items.remove(i);
         }
      }
      return total;
   }


   /**
    * Adds specified amount to this PendingBalance
    * @param amount the amount to add to this PendingBalance
    * @param availability the time when the given amount will no longer be pending
    */

   public void add(double amount, LocalDateTime availability)
   {
      PendingItem pendingItem = new PendingItem(amount, availability);
      items.add(pendingItem);
      Collections.sort(items);
   }

   /**
    * Class encapsulating one pending item
    */
   protected class PendingItem implements Comparable<PendingItem>
   {
      protected double amount;
      protected LocalDateTime time;

      /**
       * Constructs a PendingIem with the specified amount and availability time
       */
      protected PendingItem(double amount, LocalDateTime time)
      {
         this.amount = amount;
         this.time = time;
      }

      /**
       * Compares if this pending item to the passed item based on availability time
       * @param pendingItem - the PendingItem to compare this item to
       * @return -1 if this is less than item, 1 if this is greater than item, 0 if this and item have the same availability time
       */
      public int compareTo(PendingItem pendingItem)
      {
         int result;
         // BUG
         if (this.time.isAfter(pendingItem.time))
         {
            result = -1;
         }
         else if (pendingItem.time.isAfter(this.time))
         {
            result = 1;
         }
         else
         {
            result = 0;
         }
         return result;
      }

      /**
       * Returns the amount of this PendingItem
       */
      double getAmount()
      {
         return amount;
      }

      /**
       * Determines if this PendingItem is still pending
       * @return true if the availability time of this PendingItem is in the future, false otherwise
       */
      boolean isStillPending()
      {
         boolean stillPending = false;
         LocalDateTime now = LocalDateTime.now();
         if (time.isAfter(now))
         {
           stillPending = true;
         }
         return stillPending;
      } 
   }

}
