"""
board/array of boxes for sort demos

Originally by Jeff Knerr, Fall 2015
Modified by Zachary Palmer, Fall 2015
"""

from graphics import *
from time import sleep

class Board(object):

  def __init__(self, L, graphwin):
    """given list, make board with items in it"""

    self.L = L
    self.graphwin = graphwin
    self.w = graphwin.getWidth()
    self.h = graphwin.getHeight()
    cx = self.w/2
    cy = self.h/2
    self.slots = len(L)
    dx = self.w/len(L)
    dy = self.h*.2
    cp = Point(cx,cy)
    p1 = Point(0,0)
    p2 = Point(dx,dy)
    template = Rectangle(p1,p2)
    template.setFill(color_rgb(64,64,64))
    template.setOutline("white")
    self.rects = []
    self.indecies = []
    self.texts = []
    for i in range(len(L)):
      b = template.clone()
      b.move(i*dx, cy)
      b.draw(graphwin)
      self.rects.append(b)
      rcp = b.getCenter()
      t = Text(rcp, str(L[i]))
      t.setTextColor("white")
      t.setSize(36)
      t.draw(graphwin)
      self.texts.append(t)
      icp = rcp.clone()
      icp.move(0,dy*.8)
      index = Text(icp, str(i))
      index.setTextColor("white")
      index.setSize(36)
      index.draw(graphwin)
      self.indecies.append

  def highlight_best(self, i):
    self.rects[i].setFill(color_rgb(160,80,0))
  
  def highlight_considering(self, i):
    self.rects[i].setFill(color_rgb(0,160,160))

  def highlight_replacing(self, i):
    self.rects[i].setFill(color_rgb(120,0,120))

  def highlight_sorted(self, i):
    self.rects[i].setFill(color_rgb(0,160,0))
    self.texts[i].setStyle("bold")

  def unhighlight(self, i):
    self.rects[i].setFill(color_rgb(64,64,64))

  def swap(self, i, j):
    t1,t2 = self.texts[i].getText(),self.texts[j].getText()
    self.texts[i].setText(t2)
    self.texts[j].setText(t1)

def wait_for_input(w):
    w.getKey()

