from helpers import UtilityHelper as utilityHelper
import time

def displayUnreadLogs(self):
    logs = self.db.getUreandLogs(self.user.public_ser)

    if len(logs) == 0:
        return False
    
    printLogs(logs)

    for log in logs:
        self.db.updateLogStatus(log[0])

def displayLogs(self):
    logs = self.db.getLatestLogs(self.user.public_ser)
    if len(logs) == 0:
        print(f"{utilityHelper.warningMessage('No logs found')}")
        return False
    
    printLogs(logs)

def printLogs(logs):
    for log in logs:
        print(f"------------------{log[0]}---------------------")
        print(f"Log Date:")
        print(f"{time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(log[2])))}")
        print(f"Log Message:")
        print(f"{log[3]}")

    print(f"------------------END---------------------")

def getBalance(self):
    balance = 0
    blocks = utilityHelper.loadFile("../data/ledger.dat")
    for block in blocks:
        for transaction in block.data:
            if transaction.outputs[0][0] == self.user.public_ser:
                balance += transaction.outputs[0][1]
            elif transaction.type != 1 and transaction.inputs[0][0] == self.user.public_ser:
                balance -= transaction.inputs[0][1]
    return balance

def getBalanceWithOutgoingPool(self, balance):
    transactions = utilityHelper.loadFile("../data/transaction_pool.dat")
    for transaction in transactions:
        if transaction.type != 1 and transaction.inputs[0][0] == self.user.public_ser:
            balance -= transaction.inputs[0][1]
    return balance

def getPersonalTransactions(self):
    blocks = utilityHelper.loadFile("../data/ledger.dat")
    total = 0
    total_input = 0
    total_output = 0
    for block in blocks:
        for transaction in block.data:
            if transaction.outputs[0][0] == self.user.public_ser or (transaction.type == 0 and transaction.inputs[0][0] == self.user.public_ser):
                total += 1
                if transaction.type == 1:
                    input_amount = 0
                    input_user = "Reward"
                    fee = 0
                else:
                    input_user = transaction.inputs[0][0]
                    input_amount = transaction.inputs[0][1]
                    fee = input_amount - transaction.outputs[0][1]
                print(f"---------------------------------------{total}-------------------------------------------")
                print(f"Transaction input: {input_amount}")
                print(f"Transaction output: {transaction.outputs[0][1]}")
                print(f"Transaction fees: {fee}")
                print(f"Transaction sender: \n{input_user}")
                print(f"Transaction recipient: \n{transaction.outputs[0][0]}")

                if transaction.outputs[0][0] == self.user.public_ser:
                    total_output += transaction.outputs[0][1]
                elif transaction.inputs[0][0] == self.user.public_ser:
                    total_input += transaction.inputs[0][1]
    print("-----------------------------------------------------------------------------------")
    print(f"Total transactions: {total}")
    print(f"Total spent: {total_input}")
    print(f"Total received: {total_output}")
    print(f"Total balance: {total_output - total_input}")
    print("-----------------------------------------------------------------------------------")
    
