import csv
import json
import datetime

# 1.NFT总量 （宝箱+赠送）
# 2.NFT mint数量 （卖出的宝箱+赠送）
# 3.NFT宝箱已开和未开数量（总数即可)
# 4.NFT持有人数量排序 (已购的宝箱+赠送)

mintTable = []
nftTable = []
nftTransferTable = []

contracts = lambda : json.load(open('contract.json', 'r'))

stat = {
    'nft_total': 0,
    'nft_mint': 0,
    'bindBox': {
        'hold': 0,
        'open': 0
    },
    'nft_rank': [
    ]
}

EMPTY_ADDRESS = '0x000000000000000000000000000000000'

def exportMintTable():
    rows = []
    with open('mint.raw.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

def exportNftTable():
    rows = []
    with open('nft.raw.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

def exportNftTransferTable():
    rows = []
    with open('nft_transfer.raw.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

def exportNftSumTable():
    rows = []
    for nft in nftTable:
        rows.push(
            (
                nft['token_id'],
                nft['token_type'],
                nft['creator_address'],
                nft['owner_address'],
                nft['tags'],
                datetime.datetime.fromtimestamp(nft['modifytime'])
            )
        )
    with open('nft.sum.csv', 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)

def statData():
    for row in nftTransferTable:
        rawData = json.loads(row['raw_data'])
        if rawData['address'].lower() == '0x8444404bD78089A5a6d5Cc57f7Df8924f2DdACB4'.lower() and \
           rawData['event'] == 'Transfer':
            if rawData['returnValues']['from'] == EMPTY_ADDRESS:
                stat['nft_total'] += 1
            if rawData['returnValues']['to'] == EMPTY_ADDRESS:
                stat['bindBox']['open'] += 1
    #end for
    for row in nftTable:
        try:
            if row['owner_address'] != EMPTY_ADDRESS:
                stat['nft_total'] += 1
                stat['nft_mint'] += 1
        except Exception as e:
            print(row)
    print(json.dumps(stat))

def loadData():
    def rowToObj(row, fields):
        #print(row)
        #print(len(row), len(fields))
        assert(len(row) == len(fields))
        obj = {}
        for idx in range(len(fields)):
            obj[fields[idx]] = row[idx]
        return obj
    def loadCsv(data, fileName, fields):
        print(fileName)
        with open(fileName, "r", encoding="utf-8") as f:
            reader = csv.reader(f, delimiter='#')
            for row in reader:
                data.append(rowToObj(row, fields))
    loadCsv(mintTable, 't_mint.txt',  [
        'idx',
        'unikey',
        'account',
        'game_id',
        'ignore',
        'bc_minted',
        'bc_mint_txhash',
        'bc_mint_itemid',
        'bc_mint_tokenid',
        'bc_mint_token_type',
        'bc_mint_tags',
        'bc_mint_count',
        'bc_mint_time',
        'bc_mint_prepare_block_number',
        'bc_mint_success_block_number',
        'bc_mint_confirm_time',
        'suspend',
        'suspend_reason',
        'done',
        'createtime',
        'modifytime'
    ])
    loadCsv(nftTable, 't_nft.txt',  [
        'idx',
        'owner_address',
        'creator_address',
        'token_id',
        'token_type',
        'token_state',
        'game_id',
        'item_id',
        'deleted',
        'opened',
        'confirm_count',
        'confirm_block_number',
        'rand_attr',
        'tags',
        'createtime',
        'modifytime'
    ])
    loadCsv(nftTransferTable, 't_nft_transfer.txt',  [
        'idx',
        'token_id',
        'txhash',
        'block_number',
        'log_index',
        '_from',
        '_to',
        'raw_data',
        'owner_confirmed',
        'createtime',
        'modifytime'
    ])

loadData()
statData()
