#!/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


def resolve_quantity(
    token: mango.Token,
    current_quantity: Decimal,
    target_quantity: mango.TargetBalance,
    price: Decimal,
) -> mango.InstrumentValue:
    current_value: Decimal = current_quantity * price
    resolved_value_to_keep: mango.InstrumentValue = target_quantity.resolve(
        token, price, current_value
    )
    return resolved_value_to_keep


parser = argparse.ArgumentParser(
    description="Balance the value of tokens in a Mango Markets account to specific values or percentages."
)
mango.ContextBuilder.add_command_line_parameters(parser)
mango.Wallet.add_command_line_parameters(parser)
parser.add_argument(
    "--target",
    type=mango.parse_target_balance,
    action="append",
    required=True,
    help="token symbol plus target value or percentage, separated by a colon (e.g. 'ETH:2.5')",
)
parser.add_argument(
    "--max-slippage",
    type=Decimal,
    default=Decimal("0.05"),
    help="maximum slippage allowed for the IOC order price",
)
parser.add_argument(
    "--action-threshold",
    type=Decimal,
    default=Decimal("0.01"),
    help="fraction of total wallet value a trade must be above to be carried out",
)
parser.add_argument(
    "--account-address",
    type=PublicKey,
    help="address of the specific account to use, if more than one available",
)
parser.add_argument(
    "--dry-run",
    action="store_true",
    default=False,
    help="runs as read-only and does not perform any transactions",
)
args: argparse.Namespace = mango.parse_args(parser)

with mango.ContextBuilder.from_command_line_parameters(args) as context:
    wallet = mango.Wallet.from_command_line_parameters_or_raise(args)
    action_threshold = args.action_threshold
    max_slippage = args.max_slippage
    group = mango.Group.load(context, context.group_address)
    account = mango.Account.load_for_owner_by_address(
        context, wallet.address, group, args.account_address
    )

    targets: typing.Sequence[mango.TargetBalance] = args.target
    logging.info(f"Targets: {targets}")

    prices: typing.List[mango.InstrumentValue] = []
    for basket_token in account.slots:
        if basket_token is not None:
            market_symbol: str = f"{basket_token.base_instrument.symbol}/{group.shared_quote_token.symbol}"
            ops = mango.operations(
                context, wallet, account, market_symbol, args.dry_run
            )
            orderbook = ops.load_orderbook()
            price = orderbook.mid_price
            if price is None:
                raise Exception(
                    f"Could not determine mid-price on market {market_symbol}."
                )
            prices += [mango.InstrumentValue(basket_token.base_instrument, price)]
    prices += [mango.InstrumentValue(group.shared_quote_token, Decimal(1))]

    account_balancer = mango.LiveAccountBalancer(
        context,
        wallet,
        account,
        group,
        targets,
        action_threshold,
        max_slippage,
        args.dry_run,
    )
    account_balancer.balance(context, prices)

logging.info("Balancing completed.")
