"""
Client Side Phonebook Application
Developed by: Sam Munhall and David Stowell
"""

from socket import *
from codecs import decode
from breezypythongui import EasyFrame
# from phonebookclienthandler import PhonebookClientHandler
# from threadsafetranscript import ThreadSafeTranscript
# import PhonebookServer

HOST = "localhost"
PORT = 5002
ADDRESS = (HOST, PORT)
BUFSIZE = 1024
CODE = "ascii"


class PhonebookClient(EasyFrame):
    """Represents an chat window for a client.
    The window waits for users to connect, then
    sends requests to the server."""

    def __init__(self):
        """Initialize the window."""
        EasyFrame.__init__(self, title="Chat Room")
        # Create and add the widgets to the window."""
        self.outputLabel = self.addLabel(text="People found: ",
                                         row=0, column=0,
                                         columnspan=2,
                                         sticky="NSEW")
        self.outputArea = self.addTextArea("", row=1, column=0,
                                           columnspan=2,
                                           width=50, height=4)
        self.inputLabel = self.addLabel(text="Who are you searching for?",
                                        row=2, column=0,
                                        columnspan=2,
                                        sticky="NSEW")
        self.inputArea = self.addTextArea("", row=3, column=0,
                                          columnspan=2,
                                          width=50, height=4)
        self.sendButton = self.addButton(text="Search",
                                         row=4, column=0,
                                         command=self.send,
                                         state="disabled")
        self.connectButton = self.addButton(text="Load Phonebook",
                                            row=4, column=1,
                                            command=self.load)
        # Connect to server and confirm connection

    def load(self):
        """Attempts to load the phonebook."""
        name = self.prompterBox(title="Input Dialog", promptString="Your name:")
        if name == "": return
        self.server = socket(AF_INET, SOCK_STREAM)
        self.server.connect(ADDRESS)
        self.server.send(bytes(name, CODE))
        reply = decode(self.server.recv(BUFSIZE), CODE)
        if reply:
            x = 10
            while x > 0:
                print("print people here")  # print the first ten people here
                x = x-1
            self.outputArea.setText(reply)
            self.inputLabel["text"] = "Enter your message below:"
            self.sendButton["state"] = "normal"
            self.connectButton["text"] = "Disconnect"
            self.connectButton["command"] = self.disconnect
        else:
            self.outputArea.setText("Could not connect")

    def disconnect(self):
        """Disconnects the client, clears the text areas,
        disables the send button, and enables connect."""
        self.server.send(bytes("DISCONNECT", CODE))
        self.server.close()
        self.inputArea.setText("")
        self.outputArea.setText("")
        self.inputLabel["text"] = "Want to connect?"
        self.sendButton["state"] = "disabled"
        self.connectButton["text"] = "Connect"
        self.connectButton["command"] = self.connect

    def send(self):
        """Sends a message to the server and waits for a
        reply."""
        message = self.inputArea.getText()
        if message == "": return
        self.server.send(bytes(message, CODE))
        reply = decode(self.server.recv(BUFSIZE), CODE)
        self.outputArea.setText(reply)


def main(fileName=None):
    """Creates the bank with the optional file name,
    wraps the window around it, and opens the window.
    Saves the bank when the window closes."""
    PhonebookClient().mainloop()


if __name__ == "__main__":
    main()
