package util

import (
	"github.com/ethereum/go-ethereum/common/math"
	"math/big"
	"strconv"
	"strings"
	"time"
)

var Ether = math.BigPow(10, 18)
var DGC = math.BigPow(10, 16)
var Shannon = math.BigPow(10, 9)

type JsonRpcResponseGetter struct {
	m map[string]interface{}
}

func MakeTimestamp() int64 {
	return time.Now().UnixNano() / int64(time.Millisecond)
}

func MakeTimestampZero(t uint64) uint64{
	gap := t % 86400
	t = t - gap
	return t
}

func MakeTimestampToDate(t int64) string{
	date := time.UnixMicro(t*1000000)
	const layout = "2006-01-02"
	return date.Format(layout)
}

func NewJsonRpcResponseGetter(result interface{}) JsonRpcResponseGetter {
	return JsonRpcResponseGetter{m: result.(map[string]interface{})}
}

func (this JsonRpcResponseGetter) GetString(key string) string {
	return this.m[key].(string)
}

func (this JsonRpcResponseGetter) GetUint64(key string) uint64 {
	str := this.m[key].(string)
	if strings.HasPrefix(str, "0x") {
		str = str[2:]
	}
	ret, err := strconv.ParseUint(str, 16, 64)
	if err != nil {
		panic(err)
	}
	return ret
}

func MustParseDuration(s string) time.Duration {
	value, err := time.ParseDuration(s)
	if err != nil {
		panic("util: Can't parse duration `" + s + "`: " + err.Error())
	}
	return value
}


func RewardInShannon(dgc *big.Int) int64 {
	reward := new(big.Int).Div(dgc, Shannon)
	return reward.Int64()
}

func RewardInShannonByBig(dgc *big.Int) *big.Int {
	reward := new(big.Int).Div(dgc, Shannon)
	return reward
}

func StringInSlice(a string, list []string) bool {
	for _, b := range list {
		if b == a {
			return true
		}
	}
	return false
}

