#!/usr/bin/env python3

import argparse
import logging
import os
import os.path
import sys
import typing

from decimal import Decimal
from solana.publickey import PublicKey

sys.path.insert(0, os.path.abspath(
    os.path.join(os.path.dirname(__file__), "..")))
import mango  # nopep8

parser = argparse.ArgumentParser(description="Display the balances of all group tokens in the current wallet.")
mango.ContextBuilder.add_command_line_parameters(parser)
mango.Wallet.add_command_line_parameters(parser)
parser.add_argument("--address", type=PublicKey,
                    help="Root address to check (if not provided, the wallet address is used)")
args: argparse.Namespace = mango.parse_args(parser)

address: typing.Optional[PublicKey] = args.address
if address is None:
    wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
    address = wallet.address

context: mango.Context = mango.ContextBuilder.from_command_line_parameters(args)
group: mango.Group = mango.Group.load(context)
cache: mango.Cache = mango.Cache.load(context, group.cache)

logging.info(f"Address: {address}")

balances: typing.List[mango.InstrumentValue] = []
sol_balance = context.client.get_balance(address)
balances += [mango.InstrumentValue(mango.SolToken, sol_balance)]

for slot_token_info in [*group.tokens, group.shared_quote]:
    if isinstance(slot_token_info.token, mango.Token):
        balance = mango.InstrumentValue.fetch_total_value(context, address, slot_token_info.token)
        balances += [balance]

print(f"\nToken Balances [{address}]:")
total_in_wallet: mango.InstrumentValue = mango.InstrumentValue(group.shared_quote_token, Decimal(0))
for balance in balances:
    if balance.value != 0:
        balance_text: str = f"{balance} "
        value_text: str = ""
        if balance.token == group.shared_quote_token:
            total_in_wallet = total_in_wallet + balance
            value_text = f" worth {balance}"
        else:
            slot: typing.Optional[mango.GroupSlot] = group.slot_by_instrument_or_none(balance.token)
            if slot is not None:
                cached_token_price: mango.InstrumentValue = group.token_price_from_cache(cache, slot.base_instrument)
                balance_value: mango.InstrumentValue = balance * cached_token_price
                total_in_wallet += balance_value
                value_text = f" worth {balance_value}"
        print(f"               {balance_text:<45}{value_text}")
print(
    f"Total Value:   {total_in_wallet}")


mango_accounts = mango.Account.load_all_for_owner(context, address, group)
account_value: mango.InstrumentValue = mango.InstrumentValue(group.shared_quote_token, Decimal(0))
quote_token_free_in_open_orders: mango.InstrumentValue = mango.InstrumentValue(group.shared_quote_token, Decimal(0))
quote_token_total_in_open_orders: mango.InstrumentValue = mango.InstrumentValue(group.shared_quote_token, Decimal(0))
grand_total: mango.InstrumentValue = total_in_wallet
for account in mango_accounts:
    print("\n⚠ WARNING! ⚠ This is a work-in-progress and these figures may be wrong!\n")
    print(f"\nAccount Balances [{account.address}]:")
    at_least_one_output: bool = False
    open_orders: typing.Dict[str, mango.OpenOrders] = account.load_all_spot_open_orders(context)
    for asset in account.base_slots:
        if (asset.deposit.value != 0) or (asset.borrow.value != 0) or (asset.net_value.value != 0) or ((asset.perp_account is not None) and not asset.perp_account.empty):
            at_least_one_output = True
            report: mango.AccountInstrumentValues = mango.AccountInstrumentValues.from_account_basket_base_token(
                asset, open_orders, group)
            # print(report)
            market_cache: mango.MarketCache = group.market_cache_from_cache(cache, report.base_token)
            price_from_cache: mango.InstrumentValue = group.token_price_from_cache(cache, report.base_token)
            priced_report: mango.AccountInstrumentValues = report.priced(market_cache)
            account_value += priced_report.net_value
            quote_token_free_in_open_orders += priced_report.quote_token_free
            quote_token_total_in_open_orders += priced_report.quote_token_total
            print(priced_report)

    quote_report = mango.AccountInstrumentValues(account.shared_quote_token,
                                                 account.shared_quote_token,
                                                 account.shared_quote.raw_deposit,
                                                 account.shared_quote.deposit,
                                                 account.shared_quote.raw_borrow,
                                                 account.shared_quote.borrow,
                                                 mango.InstrumentValue(group.shared_quote_token, Decimal(0)),
                                                 mango.InstrumentValue(group.shared_quote_token, Decimal(0)),
                                                 quote_token_free_in_open_orders,
                                                 quote_token_total_in_open_orders,
                                                 mango.InstrumentValue(group.shared_quote_token, Decimal(0)),
                                                 Decimal(0), Decimal(0),
                                                 mango.InstrumentValue(group.shared_quote_token, Decimal(0)),
                                                 mango.InstrumentValue(group.shared_quote_token, Decimal(0)),
                                                 Decimal(0), Decimal(0),
                                                 mango.NullLotSizeConverter())
    account_value += quote_report.net_value + quote_token_total_in_open_orders
    print(quote_report)

    print(f"Account Total: {account_value}")
    grand_total += account_value

print(f"\nGrand Total:   {grand_total}")
