package miner

import (
	"github.com/pkg/errors"
	"github.com/33cn/chain33/types"
	"fmt"
)

var execAddress map[string]string



func accountConvert(acc *types.Account) *RMinerStat {
	return &RMinerStat{
		Frozen:  acc.Frozen,
		Balance: acc.Balance,
		Total:   acc.Frozen + acc.Balance,

	}
}

func fromAccount(l *types.ReceiptAccountTransfer) *RMinerStat {

	return accountConvert(l.Current)
}

// coins/ticket-addr
func fromExecAccount(l *types.ReceiptExecAccountTransfer) *RMinerStat {

	detail := accountConvert(l.Current)
	return detail
}

// LogFeeConvert LogFeeConvert
func LogFeeConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogTransferConvert LogTransferConvert
func LogTransferConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogDepositConvert LogDepositConvert
func LogDepositConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogExecTransferConvert LogExecTransferConvert
func LogExecTransferConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptExecAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromExecAccount(&l), nil
	}
	return nil, err
}

// LogExecWithdrawConvert LogExecWithdrawConvert
func LogExecWithdrawConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptExecAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromExecAccount(&l), nil
	}
	return nil, err
}

// LogExecDepositConvert LogExecDepositConvert
func LogExecDepositConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptExecAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromExecAccount(&l), nil
	}
	return nil, err
}

// LogExecFrozenConvert LogExecFrozenConvert
func LogExecFrozenConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptExecAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromExecAccount(&l), nil
	}
	return nil, err
}

// LogExecActiveConvert LogExecActiveConvert
func LogExecActiveConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptExecAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromExecAccount(&l), nil
	}
	return nil, err
}

// LogGenesisTransferConvert LogGenesisTransferConvert
func LogGenesisTransferConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogGenesisDepositConvert LogGenesisDepositConvert
func LogGenesisDepositConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogMintConvert LogMintConvert
func LogMintConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// LogBurnConvert LogBurnConvert
func LogBurnConvert(v []byte) (*RMinerStat, error) {
	var l types.ReceiptAccountTransfer
	err := types.Decode(v, &l)
	if err == nil {
		return fromAccount(&l), nil
	}
	return nil, err
}

// AssetLogConvert  convert asset log
func AssetLogConvert(ty int32, v []byte) (*RMinerStat, error) {
	detail, err := assetLogConvert(ty, v)
	if err != nil {
		return nil, errors.Wrapf(err, "convert asset log: %d", ty)
	}
	return detail, nil
}

func assetLogConvert(ty int32, v []byte) (*RMinerStat, error) {
	if ty == types.TyLogFee {
		return LogFeeConvert(v)
	} else if ty == types.TyLogTransfer {
		return LogTransferConvert(v)
	} else if ty == types.TyLogDeposit {
		return LogDepositConvert(v)
	} else if ty == types.TyLogExecTransfer {
		return LogExecTransferConvert(v)
	} else if ty == types.TyLogExecWithdraw {
		return LogExecWithdrawConvert(v)
	} else if ty == types.TyLogExecDeposit {
		return LogExecDepositConvert(v)
	} else if ty == types.TyLogExecFrozen {
		return LogExecFrozenConvert(v)
	} else if ty == types.TyLogExecActive {
		return LogExecActiveConvert(v)
	} else if ty == types.TyLogGenesisTransfer {
		return LogGenesisTransferConvert(v)
	} else if ty == types.TyLogGenesisDeposit {
		return LogGenesisDepositConvert(v)
	} else if ty == types.TyLogMint {
		return LogMintConvert(v)
	} else if ty == types.TyLogBurn {
		return LogBurnConvert(v)
	}
	return nil, notSupport(ty, v)
}

func notSupport(logType int32, json []byte) (err error) {
	return errors.New("notSupport" + fmt.Sprintf("-log:%d", logType))
}