package poker.domain

import scala.collection.jcl.ArrayList
import scala.util.Random
import scala.RandomAccessSeq.MutableProjection

class PilhaDeCartas(val qtdeMaxima:Int) extends ArrayList[Carta] {
  
//  override def toString = super.toString().replaceFirst("List", "")
  
  def push(cartas:Carta*) {
    if(quantidadeDeCartas() == qtdeMaxima) 
      throw new RuntimeException("Ops: Quantidade máxima excedida.")
    this ++ cartas
  }
  
  def pop(): Carta = {
    this.remove(this.length-1)
  }
  
  def swap(i:Int, j:Int) {
    val tmp = this(i)
    this(i) = this(j)
    this(j) = tmp
  }
  
  // fisherYatesS
  def shuffle() {
    val random = new Random()
    (0 to length-1).reverse.foreach { i:Int => 
      swap(i, random.nextInt(i + 1))
    }
  }
  
  // selection sort n^2 (max 5 => 25): just for simplicity and tests
  def sort() {
    var i = 0
    var j = -1
    while(i < this.length) {
      j = i + 1
      while(j < this.length) {
        if(this(i) > this(j))
          swap(i, j)
        j += 1
      }
      i += 1
    }
  }
  
  def eachCons[B](n:Int, f: Seq[Carta] => B) {
    var ref = 0
    while(ref <= (length-n)) {
      f(this.slice(ref, ref+n))
      ref += 1
    }
  }
  
  def embaralha() {
    shuffle()
  }
  
  def quantidadeDeCartas(): Int = {
    this.length
  }
  
}
