package utils

import "time"

func CostTime(start time.Time) int64 {
	duration := time.Now().UnixNano() - start.UnixNano()

	return duration / 1e6
}

type FeatureInfo struct {
	Value interface{} `json "value"`
	Type  string      `json "type"`
}

func ConvertFeatures(features map[string]interface{}) map[string]*FeatureInfo {
	ret := make(map[string]*FeatureInfo, len(features))
	for name, value := range features {
		info := &FeatureInfo{
			Value: value,
			Type:  GetTypeOf(value),
		}

		ret[name] = info
	}
	return ret
}

func GetTypeOf(value interface{}) string {
	switch value.(type) {
	case int:
		return "int"
	case int32:
		return "int"
	case int64:
		return "bigint"
	case string:
		return "string"
	case float64:
		return "float64"
	default:
		return "none"
	}
}
func GetValueByType(value interface{}, vtype string) interface{} {
	switch vtype {
	case "int":
		return ToInt(value, 0)
	case "string":
		return ToString(value, "")
	case "float64":
		return ToFloat(value, float64(0))
	case "int64":
		return ToInt64(value, 0)
	case "bigint":
		return ToInt64(value, 0)
	default:
		return value
	}
}

func IndexOf(a []string, e string) int {
	n := len(a)
	var i = 0
	for ; i < n; i++ {
		if e == a[i] {
			return i
		}
	}
	return -1
}
