from hw7 import *

def login (chatclient, username, password):
    return chatclient.login(username,password)

def getUsers(chatclient):
    return chatclient.getUsers()

def getFriends(chatclient):
    return chatclient.getFriends()

def sendFriendRequest(chatclient,friend):
    return chatclient.sendFriendRequest(friend)

def acceptFriendRequest(chatclient,friend):
    return chatclient.acceptFriendRequest(friend)

def sendMessage(chatclient,friend, message):
    return chatclient.sendMessage(friend, message)

def sendFile(chatclient,friend, filename):
    return chatclient.sendFile(friend, filename)

def getRequests(chatclient):
    return chatclient.getRequests()

def getMail(chatclient):
    return chatclient.getMail()

########## CLIENT PROGRAM HELPER FUNCTIONS: CHANGE ONLY IF NEEDED ##########
def PrintUsage(s):
    print (">> Menu:")
    print ("     Menu            Shows a Menu of acceptable commands")
    print ("     Users           List all active users")
    print ("     Friends         Show your current friends")
    print ("     Add Friend      Send another friend a friend request")
    print ("     Send Message    Send a message to a friend")
    print ("     Send File       Send a file to a friend")
    print ("     Requests        See your friend requests")
    print ("     Messages        See the new messages you recieved")
    print ("     Exit            Exits the chat client")
    
def ShowUsers(s):
    Users = getUsers(s)
    if Users == []:
        print (">> There are currently no active users")
    else:
        print (">> Active users:")
        for u in Users:
            print ("     " + u)
    
def ShowFriends(s):
    Friends = getFriends(s)
    if Friends == []:
        print (">> You currently have no friends")
    else:
        print (">> Your friends:")
        for f in Friends:
            print ("     " + f)
    
def AddFriend(s):
    friend = input("Please insert the username of the user you would like to add as a friend: ")
    if sendFriendRequest(s, friend): print (friend, "added succesfully")
    else: print ("Error adding " + friend + ". Please try again.")
    
def AcceptFriend(s):
    friend = input("Please insert the username of the user you would like to accept as a friend: ")
    if acceptFriendRequest(s, friend): print ("Request from " + friend + " accepted succesfully")
    else: print ("Error accepting request from " + friend + ". Please try again." )
    
def SendMessage(s):
    friend = input("Please insert the username of the friend you would like to message: ")
    message = input("Please insert the message that you would like to send: ")
    if friend in getFriends(s):
        if sendMessage(s, friend, message): print ("Mesage sent to " + friend + " succesfully")
        else: print ("Error sending message to " + friend + ". Please try again.")
    else: print (friend, "is not a Friend. You must add them as a friend before you can message them.")

def SendFile(s):
    friend = input("Please insert the username of the friend you would like to mail a file: ")
    filename = input("Please insert the name of the file you'd like to send: ")
    if friend in getFriends(s):
        if sendFile(s, friend, filename): print ("File sent to " + friend + " succesfully")
        else: print ("Error sending file to " + friend + ". Please try again.")
    else: print (friend, "is not a Friend. You must add them as a friend before you can send them a file.")

    
def ShowRequests(s):
    Requests = getRequests(s)
    if Requests == []:
        print (">> You currently have no friend requests")
    else:
        print (">> The following users have asked to be your friends:")
        for r in Requests:
            print ("     " + r)
    
def ShowMessages(s):
    (Messages, Files) = getMail(s)
    if Messages == []:
        print (">> You have no new messages")
    else:
        print (">> You have recieved the following messages:")
        for (u, m) in Messages:
            print ("     " + u + " says: " + m)
    if Files == []:
        print (">> You have no new files")
    else:
       print (">> You also recieved the following Users:")
       for (u, f) in Files:
            print ("File " + f +" recieved from: " + u + " and downloaded successfully.")

##########  MAIN CODE, CHANGE ONLY IF ABSOLUTELY NECCESSARY  ##########
# Connect to the server at IP Address 86.36.46.10
# and port number 15112
comm = chatComm("86.36.46.10", 15112)
comm.startConnection()

# Ask the user for their login name and password
username = input(">> Login as: ")
if ("Exit" == username) : exit()

password = input(">> Password: ")
if ("Exit" == password) : exit()

# Run authentication
# Ask for username and password again if incorrect
while not login (comm,username, password):
    print (">> Incorrect Username/Password Combination!")
    print (">> Please try again, or type 'Exit' to close the application.")
    username = input(">> Login as: ")
    if ("Exit" == username) : exit()
    password = input(">> Password: ")
    if ("Exit" == password) : exit()

# Now user is logged in

# Set up your commands options
menu = {
        "Menu": PrintUsage,
        "Users" : ShowUsers,
        "Friends": ShowFriends,
        "Add Friend": AddFriend,
        "Accept Friend": AcceptFriend,
        "Send Message": SendMessage,
        "Send File": SendFile,
        "Requests": ShowRequests,
        "Messages": ShowMessages,
    }

# Prompt the user for a command
print (">> Welcome", username, "!")
print (">> Insert command or type Menu to see a list of possible commands")
prompt = "[" + username + "]>>"
command = input(prompt)

while (command != "Exit"):
    if not command in menu.keys():
        print (">> Unidentified command " + command + ". Please insert valid command or type Menu to see a list of possible commands.")
        prompt = "[" + username + "]>>"
        command = input(prompt)
    else:
        menu[command](comm)
        command = input(prompt)
