"""
Sorting examples.

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

from random import *
from graphics import *
from board import *
from sys import *

def show_help_and_exit(message):
    print message
    print
    print "Usage: python %s SORT-TYPE" % argv[0]
    print
    print "Valid sort types are:"
    print "    sel (for selection sort)"
    print "    bub (for bubble sort)"
    print
    exit(1)

def main():
  if len(argv) < 2:
    show_help_and_exit("Must specify sort type!")
  elif argv[1] == "sel":
    sort_name = "Selection Sort"
    sort_function = selectionSort
  elif argv[1] == "bub":
    sort_name = "Bubble Sort"
    sort_function = bubbleSort
  else:
    show_help_and_exit("Invalid sort type!")

  N = 8
  L = []
  for i in range(N):
    num = randrange(1,101)
    L.append(num)
  print L

  w = 1200
  h = 900
  gw = GraphWin(sort_name, w, h)
  gw.setBackground("grey")

  title = Text(Point(w*.5, h*.2), sort_name)
  title.setSize(36)
  title.draw(gw)

  board = Board(L, gw)

  t = Text(Point(gw.getWidth()/2, gw.getHeight()*.4), " ")
  t.setSize(36)
  t.setTextColor("yellow")
  t.draw(gw)

  wait_for_input(gw)
  # sort it here...show animation...
  
  sort_function(L, t, board, gw)
  t.setText("DONE!")
  t.setTextColor("black")
  wait_for_input(gw)

def bubbleSort(L, t, board, gw):
  """do bubble sort with animation"""
  for j in range(len(L)):
    for i in range(len(L)-j-1):
      t.setText("Consider swapping %d and %d" % (i,i+1))
      board.highlight_considering(i)
      board.highlight_considering(i+1)
      wait_for_input(gw)
      if L[i] > L[i+1]:
        t.setText("Swap %d and %d" % (i,i+1))
        board.highlight_replacing(i)
        board.highlight_replacing(i+1)
        wait_for_input(gw)
        L[i],L[i+1] = L[i+1],L[i]
        board.swap(i,i+1)
        wait_for_input(gw)
      board.unhighlight(i)
      board.unhighlight(i+1)
    t.setText("Now we know that %d is in the right spot" % (len(L) - j - 1))
    wait_for_input(gw)
    board.highlight_sorted(len(L)-j-1 )
    wait_for_input(gw)

def selectionSort(L, t, board, gw):
  """do selection sort, show it happening, one step at a time"""
  for i in range(len(L)):
    ismallest = i
    t.setText("Find smallest from %d to end..." % i)
    # Consider moving i
    board.highlight_best(i)
    wait_for_input(gw)
    # Look at each element after it
    for j in range(i+1, len(L)):
      board.highlight_considering(j)
      wait_for_input(gw)
      if L[j] < L[ismallest]:
        # We found a new record; make a big deal out of this.
        board.unhighlight(ismallest)
        ismallest = j
        board.highlight_best(ismallest)
        wait_for_input(gw)
      else:
        board.unhighlight(j)
    # Now swap
    t.setText("** Swap position %d and position %d **" % (i, ismallest))
    board.highlight_replacing(i)
    board.highlight_replacing(ismallest)
    wait_for_input(gw)
    board.swap(i,ismallest)
    L[i],L[ismallest] = L[ismallest],L[i]
    wait_for_input(gw)
    board.unhighlight(ismallest)
    board.highlight_sorted(i)
    wait_for_input(gw)

main()
