package dns

import (
	
	
	
)

// NumField returns the number of rdata fields r has.
func ( RR) int {
	return reflect.ValueOf().Elem().NumField() - 1 // Remove RR_Header
}

// Field returns the rdata field i as a string. Fields are indexed starting from 1.
// RR types that holds slice data, for instance the NSEC type bitmap will return a single
// string where the types are concatenated using a space.
// Accessing non existing fields will cause a panic.
func ( RR,  int) string {
	if  == 0 {
		return ""
	}
	 := reflect.ValueOf().Elem().Field()
	switch .Kind() {
	case reflect.String:
		return .String()
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return strconv.FormatInt(.Int(), 10)
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return strconv.FormatUint(.Uint(), 10)
	case reflect.Slice:
		switch reflect.ValueOf().Elem().Type().Field().Tag {
		case `dns:"a"`:
			// TODO(miek): Hmm store this as 16 bytes
			if .Len() < net.IPv4len {
				return ""
			}
			if .Len() < net.IPv6len {
				return net.IPv4(byte(.Index(0).Uint()),
					byte(.Index(1).Uint()),
					byte(.Index(2).Uint()),
					byte(.Index(3).Uint())).String()
			}
			return net.IPv4(byte(.Index(12).Uint()),
				byte(.Index(13).Uint()),
				byte(.Index(14).Uint()),
				byte(.Index(15).Uint())).String()
		case `dns:"aaaa"`:
			if .Len() < net.IPv6len {
				return ""
			}
			return net.IP{
				byte(.Index(0).Uint()),
				byte(.Index(1).Uint()),
				byte(.Index(2).Uint()),
				byte(.Index(3).Uint()),
				byte(.Index(4).Uint()),
				byte(.Index(5).Uint()),
				byte(.Index(6).Uint()),
				byte(.Index(7).Uint()),
				byte(.Index(8).Uint()),
				byte(.Index(9).Uint()),
				byte(.Index(10).Uint()),
				byte(.Index(11).Uint()),
				byte(.Index(12).Uint()),
				byte(.Index(13).Uint()),
				byte(.Index(14).Uint()),
				byte(.Index(15).Uint()),
			}.String()
		case `dns:"nsec"`:
			if .Len() == 0 {
				return ""
			}
			 := Type(.Index(0).Uint()).String()
			for  := 1;  < .Len(); ++ {
				 += " " + Type(.Index().Uint()).String()
			}
			return 
		default:
			// if it does not have a tag its a string slice
			fallthrough
		case `dns:"txt"`:
			if .Len() == 0 {
				return ""
			}
			 := .Index(0).String()
			for  := 1;  < .Len(); ++ {
				 += " " + .Index().String()
			}
			return 
		}
	}
	return ""
}