#!/usr/bin/env python3

import argparse
import os
import os.path
import sys

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="Shows the on-chain data of a particular account."
)
mango.ContextBuilder.add_command_line_parameters(parser)
parser.add_argument(
    "--address",
    type=PublicKey,
    action="append",
    default=[],
    help="address of the account",
)
parser.add_argument(
    "--filename",
    type=str,
    action="append",
    default=[],
    help="filename for saving the JSON-formatted AccountInfo data",
)
args: argparse.Namespace = mango.parse_args(parser)

if len(args.address) == 0:
    raise Exception("Must specify at least one address to fetch")

if (len(args.filename) != 0) and (len(args.filename) != len(args.address)):
    raise Exception(
        f"If specifying filenames, number of filenames ({len(args.filename)} provided) must match the number of addresses ({len(args.address)} provided)."
    )

with mango.ContextBuilder.from_command_line_parameters(args) as context:
    account_infos = mango.AccountInfo.load_multiple(context, args.address)
    for index, account_info in enumerate(account_infos):
        if account_info is None:
            mango.output(f"No account at '{args.address[index]}'")
        else:
            if len(args.filename) != 0:
                account_info.save_json(args.filename[index])
            else:
                mango.output(account_info)
                mango.output("Account Data:", account_info.encoded_data())
