package com.jdbernard.teammaker

import groovy.beans.Bindable
import groovy.swing.SwingBuilder
import java.awt.GridBagConstraints as GBC
import java.awt.GridBagLayout
import java.awt.BorderLayout
import javax.swing.DefaultListModel
import javax.swing.JFrame
import javax.swing.JOptionPane

public class TeamMaker {

    /* ======== MODEL ======== */

    public static def swing = new SwingBuilder()
    public static final String version = "0.2"

    def frame
    def team1List
    def team2List
    def team1WinsButton
    def team2WinsButton
    def newGameButton
    def sittingList

    PlayerChooser chooser = new RandomChooser()

    @Bindable boolean inGame = false
    @Bindable int teamSize = 4



    /* ======== VIEW ======== */

        /*
           +--------------------------------------------------+
           | JDB TeamMaker v0.2                          _[]X |
           +--------------------------------------------------+
           |                                                  |
           | +-Current-Game--------------+ +-Sitting-Players+ |
           | |                           | |                | |
           | | Team 1          Team 2    | | Jeptha       3 | |
           | |   Derrick         Eric    | | Jack         2 | |
           | |   Shane           Brandon | | Sam          2 | |
           | |   Shane           Darren  | | Greg         2 | |
           | |   Christian       Brett   | | Frank        1 | |
           | +---------------------------+ | Chris D      1 | |
           |                               | Joshua       1 | |
           |                               |                | |
           |                               |                | |
           |                               |                | |
           |                               |                | |
           |                               |                | |
           |                               +----------------+ |
           +--------------------------------------------------+

         */

    private void init() {
        frame = swing.frame(title: "JDB TeamMaker v$version",
            size: [600, 400], locationRelativeTo: null,
            defaultCloseOperation: JFrame.EXIT_ON_CLOSE) {

            panel() {
                gridBagLayout()

                panel(constraints: gbc(gridx: 0, gridy: 0, fill: GBC.BOTH,
                        insets: [5, 5, 5, 0], weightx: 3, weighty: 1),
                    border: titledBorder(title: 'Current Game')) {
                    
                    gridBagLayout()

                    label('Team A', constraints: gbc(gridx: 0, gridy: 0,
                        fill: GBC.BOTH, insets: [5, 5, 0, 0]))

                    label('Team B', constraints: gbc(gridx: 1, gridy: 0, 
                        fill: GBC.BOTH, insets: [5, 5, 0, 5]))
                    
                    def teamListRenderer = new PlayerListCellRenderer(showStats: false,
                        colored: false)

                    team1List = list(cellRenderer: teamListRenderer,
                        constraints: gbc(gridx: 0, gridy: 1, fill: GBC.BOTH,
                        insets: [5, 5, 0, 0], weightx: 2, weighty: 2),
                        model: new DefaultListModel())

                    team1WinsButton = button('Team A Wins',
                        constraints: gbc(gridx: 0, gridy: 2, anchor:
                            GBC.CENTER, insets: [5, 5, 5, 0]),
                        enabled: false,
                        actionPerformed: {
                            sittingList.model.each { it.gamesSat++ }
                            team1List.model.each { player ->
                                player.gamesSat = 0
                                sittingList.model.addElement(player)
                            }
                            team1List.model.clear()
                            team1List.repaint()
                            sittingList.repaint()
                            inGame = false
                            team1WinsButton.enabled = false
                            team2WinsButton.enabled = false
                        })

                    team2List = list(cellRenderer: teamListRenderer,
                        constraints: gbc(gridx: 1, gridy: 1, fill: GBC.BOTH,
                        insets: [5, 5, 0, 5], weightx: 2, weighty: 2),
                        model: new DefaultListModel())

                    team2WinsButton = button('Team B Wins',
                        constraints: gbc(gridx: 1, gridy: 2,
                            anchor: GBC.CENTER, insets: [5, 5, 5, 5]),
                        enabled: false,
                        actionPerformed: {
                            sittingList.model.each { it.gamesSat++ }
                            team2List.model.each { player ->
                                player.gamesSat = 0
                                sittingList.model.addElement(player)
                            }
                            team2List.model.clear()
                            team2List.repaint()
                            sittingList.repaint()
                            inGame = false
                            team1WinsButton.enabled = false
                            team2WinsButton.enabled = false
                        })

                }

                scrollPane(constraints: gbc(gridx: 1, gridy: 0, gridwidth: 2,
                        fill: GBC.BOTH, insets: [5, 5, 0, 5], weighty: 1),
                    border: titledBorder(title: 'Sitting Players')) {

                    sittingList = list(cellRenderer: new PlayerListCellRenderer(
                            showStats: true, colored: true),
                            model: new SortedPlayerModel())
                }

                button('Next Game', constraints: gbc(gridx: 0, gridy: 1,
                    anchor: GBC.CENTER, insets: [5, 5, 5, 0]),
                    actionPerformed: { newGame() })

                button('Add Player', constraints: gbc(gridx: 1, gridy: 1,
                    anchor: GBC.CENTER, insets: [5, 5, 5, 0]),
                    actionPerformed: {
                        def name = JOptionPane.showInputDialog(frame,
                            "Enter the new player's name: ", "New Player...",
                            JOptionPane.QUESTION_MESSAGE)
                        def player = new Player()
                        player.name = name
                        player.gamesSat = 0
                        sittingList.model.addElement(player)
                    })

                newGameButton = button('Delete Player',
                    constraints: gbc(gridx: 2, gridy: 1, anchor: GBC.CENTER,
                        insets: [5, 5, 5, 5]),
                    actionPerformed: {
                        sittingList.model.removeElement(
                            sittingList.selectedValue) })

            }
        }
    }


    

    /* ======== CONTROLLER ======== */

    public static void main(String[] args) {
        def inst = new TeamMaker()
        inst.frame.show()
    }

    TeamMaker() {
        init()
    }

    private void newGame() {
        if (team1List.model.size() < teamSize)
            populate(team1List, sittingList, teamSize)

        if (team2List.model.size() < teamSize)
            populate(team2List, sittingList, teamSize)

        team1WinsButton.enabled = true
        team2WinsButton.enabled = true
        newGameButton.enabled = false
    }

    private static void populate(def teamList, def sittingList, int teamSize) {
        while (teamList.model.size() < teamSize) {
            def player = choosePlayer(sittingList)
            teamList.model.addElement(player)
            sittingList.model.removeElement(player)
        }
    }

    private static Player choosePlayer(def list) {
        def choices = []
        list.model.each {  player ->
            player.gamesSat.times { choices << player }
        }

        // if no players have sat at least one game, add all once
        if (choices.size() == 0) list.model.each { player -> choices << player }

        choices.sort { rand.nextInt() }
        println choices
        return choices[0]
    }

    private float calculatePercentage(Player p, def sittingList) {

    }
}
