// Copyright (C) 2018 Marius Schellenberger

package jwt

// Claims is the claim type of the token
// The Claims map is not goroutine safe
type Claims map[string]interface{}

// Get returns a value from the claims map
func (c Claims) Get(key string) (v interface{}, ok bool) {
	v, ok = c[key]
	return
}

// GetString returns a string from the claims map
func (c Claims) GetString(key string) (s string) {
	if v, ok := c.Get(key); ok && v != nil {
		s, _ = v.(string)
	}
	return
}

// GetBool returns a bool from the claims map
func (c Claims) GetBool(key string) (b bool) {
	if v, ok := c.Get(key); ok && v != nil {
		b, _ = v.(bool)
	}
	return
}

// GetInt returns an int from the claims map
func (c Claims) GetInt(key string) (i int) {
	if f, ok := c.getFloat64(key); ok {
		return int(f)
	}
	if v, ok := c.Get(key); ok && v != nil {
		i, _ = v.(int)
	}
	return
}

// GetInt64 returns an int64 from the claims map
func (c Claims) GetInt64(key string) (i int64) {
	if f, ok := c.getFloat64(key); ok {
		return int64(f)
	}
	if v, ok := c.Get(key); ok && v != nil {
		i, _ = v.(int64)
	}
	return
}

// getFloat64 returns a float64 and ok from the claims map
func (c Claims) getFloat64(key string) (f float64, fok bool) {
	if v, ok := c.Get(key); ok && v != nil {
		f, fok = v.(float64)
	}
	return
}

// GetFloat64 returns a float64 from the claims map
func (c Claims) GetFloat64(key string) (f float64) {
	f, _ = c.getFloat64(key)
	return
}

// Set sets the value of key in the claims map, if not nil
func (c Claims) Set(key string, v interface{}) {
	if c == nil {
		return
	}
	c[key] = v
}

// Delete removes the key from the claims map
func (c Claims) Delete(key string) {
	delete(c, key)
}

// Copy returns a new Claims map
func (c Claims) Copy() (n Claims) {
	n = make(Claims)
	for k, v := range c {
		n[k] = v
	}
	return
}
