#!/usr/bin/env python3

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

from decimal import Decimal
from spl.token.client import Token as SolanaSPLToken
from spl.token.constants import TOKEN_PROGRAM_ID
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="mint SPL tokens to your wallet")
mango.ContextBuilder.add_command_line_parameters(parser)
mango.Wallet.add_command_line_parameters(parser)
parser.add_argument(
    "--symbol", type=str, required=True, help="token symbol to mint (e.g. USDC)"
)
parser.add_argument(
    "--quantity", type=Decimal, required=True, help="quantity token to deposit"
)
parser.add_argument(
    "--address",
    type=PublicKey,
    help="Destination address for the minted token - can be either the actual token address or the address of the owner of the token address",
)
parser.add_argument(
    "--wait",
    action="store_true",
    default=False,
    help="wait until the transactions are confirmed",
)
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)
    token: mango.Token = mango.token(context, args.symbol)

    spl_token = SolanaSPLToken(
        context.client.compatible_client, token.mint, TOKEN_PROGRAM_ID, wallet.keypair
    )

    # Is the address an actual token account? Or is it the SOL address of the owner?
    account_info: typing.Optional[mango.AccountInfo] = mango.AccountInfo.load(
        context, args.address
    )
    if account_info is None:
        raise Exception(f"Could not find account at address {args.address}.")

    if account_info.owner == mango.SYSTEM_PROGRAM_ADDRESS:
        # This is a root wallet account - get the associated token account
        destination: PublicKey = mango.TokenAccount.find_or_create_token_address_to_use(
            context, wallet, args.address, token
        )

    quantity = token.shift_to_native(args.quantity)

    mango.output(f"Minting {args.quantity} {args.symbol} to {destination}")
    response = spl_token.mint_to(
        destination, wallet.address, int(quantity), multi_signers=[wallet.keypair]
    )
    signatures = [response["result"]]

    if args.wait:
        mango.output("Waiting on transaction signatures:")
        mango.output(mango.indent_collection_as_str(signatures, 1))
        results = mango.WebSocketTransactionMonitor.wait_for_all(
            context.client.cluster_ws_url, signatures
        )
        mango.output("Transaction results:")
        mango.output(mango.indent_collection_as_str(results, 1))
    else:
        mango.output("Transaction signatures:")
        mango.output(mango.indent_collection_as_str(signatures, 1))
