import { rpc } from '../lib/rpcapi'
import { parseString } from '../lib/convert'

/*
 * json.result should return a list of tokens. 
 * Each token should have name, contract address, and ABI
*/
export function loadTokenList() {
    return function (dispatch) {
        dispatch({
            type: 'TOKEN/LOADING'
        });
        rpc("emerald_contracts", []).then((json) => {
            const tokens = json.result.filter((contract) => {
                contract.features = contract.features || [];
                return contract.features.indexOf('erc20') >= 0
            });
            dispatch({
                type: 'TOKEN/SET_LIST',
                tokens: tokens
            });
            tokens.map((token) => dispatch(loadTokenDetails(token)))
        });
    }
}

export function loadTokenDetails(token) {
    return function (dispatch) {
        const tokenSupplyId = "0x18160ddd";
        const decimalsId =    "0x313ce567";
        const symbolId =      "0x95d89b41";
        const nameId =        "0x06fdde03";
        rpc("eth_call", [{to: token.address, data: tokenSupplyId}, "latest"]).then((resp) => {
            dispatch({
                type: 'TOKEN/SET_TOTAL_SUPPLY',
                address: token.address,
                value: resp.result
            })
        });
        rpc("eth_call", [{to: token.address, data: decimalsId}, "latest"]).then((resp) => {
            dispatch({
                type: 'TOKEN/SET_DECIMALS',
                address: token.address,
                value: resp.result
            })
        });
        rpc("eth_call", [{to: token.address, data: symbolId}, "latest"]).then((resp) => {
            dispatch({
                type: 'TOKEN/SET_SYMBOL',
                address: token.address,
                value: parseString(resp.result)
            })
        });
    }
}

export function addToken(address, name) {
    return function (dispatch) {
        return rpc("emerald_addContract", [{
                    "address": address,
                    "name": name
                    }]).then((json) => {
                        dispatch({
                            type: 'TOKEN/ADD_TOKEN',
                            address: address,
                            name: name
                        })
                        dispatch(loadTokenDetails({address: address}))
        });
    }
}

@@ -838,36 +838,46 @@
 nction (dispatch
+, getState
 ) %7B%0A
-
         const to
@@ -1827,32 +1827,120 @@
  %7D)%0A        %7D);%0A
+        getState().accounts.map((acct) =%3E dispatch(loadTokenBalanceOf(token, acct.id)))%0A
     %7D%0A%7D%0A%0Aexport 
@@ -2435,30 +2435,489 @@
 ddress%7D))%0A        %7D);%0A    %7D%0A
-
 %7D%0A
+%0Aexport function loadTokenBalanceOf(token, accountId) %7B%0A    return function (dispatch) %7B%0A        const balanceOfId = %220x70a08231%22;%0A        rpc(%22eth_call%22, %5B%7Bto: token.address, data: balanceOfId, address: accountId%7D, %22latest%22%5D)%0A            .then((resp) =%3E %7B dispatch(%7B%0A                type: 'TOKEN/SET_BALANCE_OF',%0A                account: accountId,%0A                token: token.address,%0A                value: resp.result%0A            %7D)%0A        %7D);%0A    %7D%0A%7D%0A%0A

add balanceOf action