# Path: ngcccbase/address.py
# def coloraddress_to_bitcoinaddress(coloraddress):
#     if "@" not in coloraddress:
#         return coloraddress # already btcaddress
#     colorid, btcaddress = coloraddress.split("@")
#     return btcaddress


import re
import json
import csv
from decimal import Decimal
from ngcccbase.address import coloraddress_to_bitcoinaddress


def sendmanyjson(model, data):
    sendmany_entries = []
    for entry in json.loads(data):
        _asset = asset(model, entry['moniker'])
        _amount = assetamount(_asset, entry['amount'])
        _coloraddress = coloraddress(model, _asset, entry['coloraddress'])
        _address = coloraddress_to_bitcoinaddress(_coloraddress)
        sendmany_entries.append((_asset, _address, _amount))
    return sendmany_entries


def sendmanycsv(model, path):
    entries = []
    with open(path, 'rb') as csvfile:
        for index, csvvalues in enumerate(csv.reader(csvfile)):
            entries.append(_sanitize_csv_input(model, csvvalues, index + 1))
    return entries


def _sanitize_csv_input(model, csvvalues, row):
    if len(csvvalues) != 3: # must have three entries
        msg = ("CSV entry must have three values 'moniker,address,amount'. "
               "Row %s has %s values!")
        raise InvalidInput(msg % (row, len(csvvalues)))
    _moniker, _coloraddress, _amount = csvvalues
    _asset = asset(model, _moniker)
    _coloraddress = coloraddress(model, _asset, _coloraddress)
    _amount = assetamount(_asset, _amount)

# Path: ngcccbase/address.py
# def coloraddress_to_bitcoinaddress(coloraddress):
#     if "@" not in coloraddress:
#         return coloraddress # already btcaddress
#     colorid, btcaddress = coloraddress.split("@")
#     return btcaddress


# Path: ngcccbase/sanitize.py
import re
import json
import csv
from decimal import Decimal
from ngcccbase.address import coloraddress_to_bitcoinaddress



def sendmanyjson(model, data):
    sendmany_entries = []
    for entry in json.loads(data):
        _asset = asset(model, entry['moniker'])
        _amount = assetamount(_asset, entry['amount'])
        _coloraddress = coloraddress(model, _asset, entry['coloraddress'])
        _address = coloraddress_to_bitcoinaddress(_coloraddress)
        sendmany_entries.append((_asset, _address, _amount))
    return sendmany_entries


def sendmanycsv(model, path):
    entries = []
    with open(path, 'rb') as csvfile:
        for index, csvvalues in enumerate(csv.reader(csvfile)):
            entries.append(_sanitize_csv_input(model, csvvalues, index + 1))
    return entries


def _sanitize_csv_input(model, csvvalues, row):
    if len(csvvalues) != 3: # must have three entries
        msg = ("CSV entry must have three values 'moniker,address,amount'. "
               "Row %s has %s values!")
        raise InvalidInput(msg % (row, len(csvvalues)))
    _moniker, _coloraddress, _amount = csvvalues
    _asset = asset(model, _moniker)
    _coloraddress = coloraddress(model, _asset, _coloraddress)
    _amount = assetamount(_asset, _amount)

    _address = coloraddress_to_bitcoinaddress(_coloraddress)