from getpass import getpass
from helpers import SignatureHelper as Signature
from helpers import UtilityHelper

class User:
    def __init__(self, db, private_ser=None, public_ser=None, username=None, password=None):
        self.db = db
        self.private_ser = private_ser
        self.public_ser = public_ser
        self.username = username
        self.password = password

    def login(self):
        input_username = input("Username: ")
        input_password = getpass("Password: ")

        hashed_password = UtilityHelper.computeHash(input_password)

        user = self.db.loginUser(input_username, hashed_password)

        # check if user exists
        # if user:
        #     private_key, public_key = Signature.bytesToKeys(user[0], user[1])
        #     self.private_key = private_key
        #     self.public_key = public_key
        #     self.username = user[2]
        #     return True    
        if user:
            private_key, public_key = Signature.bytesToKeys(user[0], user[1])
            self.private_ser = user[0]
            self.public_ser = user[1]
            self.username = user[2]
            return True   
        return False

    def register(self):
        input_username = input("Username: ")
        input_password = getpass("Password: ")

        hashed_password = UtilityHelper.computeHash(input_password)

        # check if username is already taken
        if self.db.fetchUserByUsername(input_username):
            print("Username already taken")
            return False

        # create sig for user
        private_key, public_key = Signature.generateKeys()
        private_ser, public_ser = Signature.keysToBytes(private_key, public_key)
        # register user
        if self.db.createUser( private_ser, public_ser, input_username, hashed_password):
            self.private_ser = private_ser
            self.public_ser = public_ser
            self.username = input_username
            return True
        return False
    
    def logout(self):
        self.private_ser = None
        self.public_ser = None
        self.username = None
        return True
    
    def updatePassword(self):
        # Get new password
        new_password = getpass('Enter your new password: ')
        check_new_password = getpass('Enter your new password again: ')
        while new_password != check_new_password:
            print('Passwords do not match')
            new_password = getpass('Enter your new password: ')
            check_new_password = getpass('Enter your new password again: ')

        old_password = getpass('Enter your old password: ')
        hashed_new_password = UtilityHelper.computeHash(new_password)
        hashed_old_password = UtilityHelper.computeHash(old_password)

        private_key_bytes = Signature.privateKeyToBytes(self.private_ser)

        if self.db.changePassword(private_key_bytes, hashed_old_password, hashed_new_password) == True:
            print('Password updated')
        else:
            print('Something went wrong while trying to update password..')

    def updateAccount(self):
        # Get new username
        new_username = input('Enter your new username: ')

        # check if username is already taken
        if self.db.fetchUserByUsername(new_username):
            print("Username already taken")
            return False

        private_key_bytes = Signature.privateKeyToBytes(self.private_ser)

        if self.db.changeUsername(private_key_bytes, new_username) == True:
            print('Username updated')
        else:
            print('Something went wrong while trying to update username..')


    def deleteAccount(self):

        while True:
            print('Are you sure you want to delete this account? (y/n)\n(this will burn all coins available in this account) (y/n)')
            confirm = input('>>: ')
            if confirm == 'y' or confirm == 'Y':
                print('Deleting account..')
                break

            elif confirm == 'n' or confirm == 'N':
                return False
            
            else:
                print('Invalid input')

        private_key_bytes = Signature.privateKeyToBytes(self.private_ser)

        if self.db.deleteUser(private_key_bytes) == True:
            print('Account deleted')
            self.logout()
            return True
        else:
            print('Something went wrong while trying to delete account..')
            return False

    def printAccountInfo(self):
        private_ser, public_ser = Signature.keysToBytes(self.private_ser, self.public_ser)
        print('Username: ' + self.username)
        print(public_ser)
        print(private_ser)


