# L-36 MCS 260 Mon 11 Apr 2016 : draw_frogs.py
"""
GUI allows user to place frogs on canvas.
"""
from tkinter import Tk, StringVar, Label, Canvas
from tkinter import Button, W, E, ALL
from class_threadfrog import ThreadFrog

class DrawFrogs(object):
    """
    GUI to hopping frogs on canvas
    """
    def __init__(self, wdw, d):
        """
        Defines a square canvas of d pixels, label,
        start, stop, and clear buttons.
        """
        wdw.title("drawing hopping frogs")
        self.frogs = []
        self.gohop = False
        self.dim = d
        self.msg = StringVar()
        self.msg.set("put mouse inside box to place frogs")
        self.lab = Label(wdw, textvariable=self.msg)
        self.lab.grid(row=0, column=0, columnspan=3)
        self.cnv = Canvas(wdw, width=d, height=d, bg='white')
        self.cnv.grid(row=1, columnspan=3)
        self.cnv.bind("<Button-1>", self.button_pressed)
        self.cnv.bind("<ButtonRelease-1>", self.button_released)
        self.bt0 = Button(wdw, text='start', command=self.start)
        self.bt0.grid(row=2, column=0, sticky=W+E)
        self.bt1 = Button(wdw, text='stop', command=self.stop)
        self.bt1.grid(row=2, column=1, sticky=W+E)
        self.bt2 = Button(wdw, text='clear', command=self.clear)
        self.bt2.grid(row=2, column=2, sticky=W+E)

    def new_frog(self, xps, yps):
        """
        Defines a new frog with coordinates
        given in (xps, yps) via the mouse.
        """
        nbr = len(self.frogs)
        name = chr(ord('a')+nbr)
        frog = ThreadFrog(name, xps, yps, 20, 5, 100)
        self.frogs.append(frog)

    def draw_frogs(self):
        """
        Draws frogs on canvas, eventually
        after fixing their coordinates.
        """
        dim = self.dim
        for frog in self.frogs:
            (xps, yps) = frog.position
            name = frog.getName()
            self.cnv.delete(name)
            if xps < 0:
                xps = dim - xps
            if yps < 0:
                yps = dim - yps
            if xps > dim:
                xps = xps - dim
            if yps > dim:
                yps = yps - dim
            frog.position = (xps, yps)
            self.cnv.create_text(xps, yps, text=name, tags=name)

    def start(self):
        """
        Starts all frogs and the animation.
        """
        self.gohop = True
        for frog in self.frogs:
            frog.start()
        while self.gohop:
            self.draw_frogs()
            self.cnv.after(10)
            self.cnv.update()

    def stop(self):
        """
        Sets hop limits of frogs to zero.
        """
        self.gohop = False
        print('setting moves of frogs to zero ...')
        for frog in self.frogs:
            frog.hop_limit = 0
        print('resetting all frogs ...')
        newfrogs = []
        while len(self.frogs) > 0:
            frog = self.frogs.pop(0)
            pos = frog.position
            step = frog.step_size
            rest = frog.rest_time
            newf = ThreadFrog(frog.getName(), pos[0], pos[1], \
                step, rest, 100)
            newfrogs.append(newf)
        self.frogs = newfrogs

    def clear(self):
        """
        Deletes all frogs from canvas.
        """
        print('killing all frogs ...')
        for frog in self.frogs:
            frog.hop_limit = 0
        while len(self.frogs) > 0:
            self.frogs.pop(0)
        self.cnv.delete(ALL) # cleaning canvas

    def button_pressed(self, event):
        """
        Displays coordinates of button pressed.
        """
        self.msg.set("currently at [ " + \
            str(event.x) + ", " + str(event.y) + " ]" +\
            " release to place frog")

    def button_released(self, event):
        """
        At release of button, a frog is created at
        the current location of the mouse pointer.
        """
        self.msg.set("placed frog at [ " + \
            str(event.x) + ", " + str(event.y) + " ]")
        self.new_frog(event.x, event.y)
        self.draw_frogs()

def main():
    """
    Instantiates the GUI and
    starts the main event loop.
    """
    top = Tk()
    DrawFrogs(top, 400)
    top.mainloop()

if __name__ == "__main__":
    main()
