#!/usr/env groovy
import groovy.swing.SwingBuilder
import java.util.Random
import javax.swing.JFrame
import net.miginfocom.swing.MigLayout

Scanner sysin = new Scanner(System.in)

def version = "0.1"
def final teamNames = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def swing = new SwingBuilder()

def allPlayers = []
def teams = [:]
def teamQueue = []
def rand = new Random(System.currentTimeMillis())
def go = true

def addPlayers
def makeTeams
def awardTeam1
def awardTeam2

/*/////// GUI Definitions \\\\\\\\*\
\*\\\\\\\ --------------- ////////*/
def frame = swing.frame(title: "JDB Team Maker v$version",
    defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {

    //tabbedPane {
    panel(layout: new MigLayout('insets 5 5 5 5', 'fill', '')) {
        textArea(constraints: '')
        button("Add Players", actionPerformed: { addPlayers() })
        button("Make Team", actionPerformed: { makeTeams() })

        button("Team 1 Wins", actionPerformed: { awardTeam1() })
        button("Team 2 Wins", actionPerformed: { awardTeam2() })
    }
    //}
}
frame.pack()

def playersDialog = swing.dialog(title: "Manage Players",
    layout: new MigLayout('insets 5 5 5 5', '', ''),
    modal: true) {
        scrollPane(constraints: '') {
            playerList = list()
        }
        button("Add Players", constraints: '',
            actionPerformed: { addPlayers() })
        button("Make Team", constraints: '',
            actionPerformed: { makeTeams() })
        button("Done", constraints: '',
            actionPerformed: { playersDialog.visible = false; })
}
playersDialog.pack()

// show main frame
frame.show()

/*/////// Action Methods \\\\\\\\*\
\*\\\\\\\ -------------- ////////*/


addPlayers = {
    playersDialog.show()
}

makeTeams = {

}

/*while(true) {

    print "Name: "
    player = sysin.nextLine()

    if (player == null || player == "DONE")
        break
    
    allPlayers << player

}

allPlayers.sort({ return rand.nextInt() })

println "\nTeam Assignments:"
println "-------------------"

for (int i = 0; i < allPlayers.size(); i++) {
    if (i % teamSize == 0)
        println "\nTeam ${++teamNum}"

    println "    ${allPlayers[i]}"
}*/
