package parsers

import (
	"regexp"
	"strings"
)

var (
	rgxSNMPContact       = regexp.MustCompile(`(?i)snmp-server contact (\S+)`)
	rgxSNMPLocation      = regexp.MustCompile(`(?i)snmp-server location (\S+)`)
	rgxSNMPCommunity1    = regexp.MustCompile(`(?i)snmp-server community (\d+) (\S+) (\S+)`)
	rgxSNMPCommunity     = regexp.MustCompile(`(?i)snmp-server community (\S+) (\S+)`)
	rgxSNMPGroup         = regexp.MustCompile(`(?i)snmp-server group (\S+) (v\S+)`)
	rgxSNMPGroupSecurity = regexp.MustCompile(`(?i)v3 (auth|noauth|priv)`)
	rgxSNMPGroupAccess   = regexp.MustCompile(`(?i)access (\S+)`)
	rgxSNMPGroupNotify   = regexp.MustCompile(`(?i)notify (\S+)`)
	rgxSNMPGroupRead     = regexp.MustCompile(`(?i)read (\S+)`)
	rgxSNMPGroupWrite    = regexp.MustCompile(`(?i)wrrite (\S+)`)
	rgxSNMPUser          = regexp.MustCompile(`(?i)snmp-server user (\S+) (\S+) (v3)`)
	rgxSNMPUserEncrypted = regexp.MustCompile(`(?i)encrypted`)
	rgxSNMPUserAccess    = regexp.MustCompile(`(?i)access (\S+)`)
	rgxSNMPUserAuth      = regexp.MustCompile(`(?i)auth (md5|sha) (\S+)`)
	rgxSNMPUserPriv      = regexp.MustCompile(`(?i)priv (?:encrypted\s*)(aes|des) (\S+)`)
	rgxSNMPHost          = regexp.MustCompile(`(?i)snmp-server host (\S+)`)
	rgxSNMPHostVersion   = regexp.MustCompile(`(?i)version (\S+)`)
	rgxSNMPHostCommunity = regexp.MustCompile(`(?i)(?:v1|v2c) (?:\d\s*)(\S+)`)
	rgxSNMPHostUser      = regexp.MustCompile(`(?i)v3 (auth|noauth|priv) (\S+)`)
	rgxSNMPHostPort      = regexp.MustCompile(`(?i)port (\S+)`)
	rgxSNMPEngineId      = regexp.MustCompile(`(?i)Engine ID:\s+(800007c703\S{12})`)
)

type (
	// SNMPConfig type is an aggregate structure to define all the configuration
	// details around an ADX SNMP config.
	SNMPConfig struct {
		Contact     string          `json:"contact"`
		Location    string          `json:"location"`
		Communities []SNMPCommunity `json:"communities"`
		Groups      []SNMPGroup     `json:"groups"`
		Users       []SNMPUser      `json:"users"`
		Hosts       []SNMPHost      `json:"hosts"`
		EngineId    string          `json:"engine_id"`
		RawText     string          `json:"raw_text"`
	}

	// SNMPCommunity type defines the parsed attributes of community configuration
	SNMPCommunity struct {
		Name         string `json:"name"`
		RoRw         string `json:"ro_rw"`
		PwDisplayNum string `json:"pw_display_num"`
	}

	// SNMPGroup type defines the parsed attributes of group configuration
	SNMPGroup struct {
		Name     string `json:"name"`
		Version  string `json:"version"`
		Security string `json:"security_level"` //auth|noauth|priv
		Access   string `json:"access"`
		Read     string `json:"read"`
		Notify   string `json:"notify"`
		Write    string `json:"write"`
	}

	// SNMPUser type defines the parsed attributes of user configuration
	SNMPUser struct {
		Name      string `json:"name"`
		Group     string `json:"group"`
		Version   string `json:"version"`
		Access    string `json:"access"`
		Encrypted bool   `json:"encrypted"`
		AuthType  string `json:"auth_type"`
		AuthPass  string `json:"auth_pass"`
		PrivType  string `json:"priv_type"`
		PrivPass  string `json:"priv_pass"`
	}

	// SNMPHost type defines the parsed attributes of host configuration
	SNMPHost struct {
		Address   string `json:"address"`
		Version   string `json:"version"`
		Community string `json:"community"`
		Security  string `json:"security_level"`
		User      string `json:"user"`
		Port      string `json:"port"`
	}
)

// Parse method is used to implement type population of a given string
// using parsers to populate the method receiver.
func (config *SNMPConfig) Parse(raw string, engineRaw string) error {
	config.RawText = raw
	for _, line := range strings.Split(raw, "\n") {
		//Contact
		matchContact := rgxSNMPContact.FindStringSubmatch(line)
		if len(matchContact) == 2 {
			config.Contact = matchContact[1]
			continue
		}
		//Location
		matchLocation := rgxSNMPLocation.FindStringSubmatch(line)
		if len(matchLocation) == 2 {
			config.Location = matchLocation[1]
			continue
		}
		//Communities
		//  note: if you do 'enable password-display' the community is hashed and prefixed with '1 '
		matchCommunity1 := rgxSNMPCommunity1.FindStringSubmatch(line)
		if len(matchCommunity1) == 4 {
			config.Communities = append(config.Communities, SNMPCommunity{
				PwDisplayNum: matchCommunity1[1],
				Name:         matchCommunity1[2],
				RoRw:         matchCommunity1[3],
			})
			continue
		}
		matchCommunity := rgxSNMPCommunity.FindStringSubmatch(line)
		if len(matchCommunity) == 3 {
			config.Communities = append(config.Communities, SNMPCommunity{
				PwDisplayNum: matchCommunity1[1],
				RoRw:         matchCommunity1[2],
			})
			continue
		}
		//Groups
		matchGroup := rgxSNMPGroup.FindStringSubmatch(line)
		if len(matchGroup) == 3 {
			group := SNMPGroup{
				Name:    matchGroup[1],
				Version: matchGroup[2],
			}
			matchSecurity := rgxSNMPGroupSecurity.FindStringSubmatch(line)
			if len(matchSecurity) == 2 {
				group.Security = matchSecurity[1]
			}
			matchAccess := rgxSNMPGroupAccess.FindStringSubmatch(line)
			if len(matchAccess) == 2 {
				group.Access = matchAccess[1]
			}
			matchNotify := rgxSNMPGroupNotify.FindStringSubmatch(line)
			if len(matchNotify) == 2 {
				group.Notify = matchNotify[1]
			}
			matchRead := rgxSNMPGroupRead.FindStringSubmatch(line)
			if len(matchRead) == 2 {
				group.Read = matchRead[1]
			}
			matchWrite := rgxSNMPGroupWrite.FindStringSubmatch(line)
			if len(matchWrite) == 2 {
				group.Write = matchWrite[1]
			}
			config.Groups = append(config.Groups, group)
			continue
		}
		//Users
		matchUser := rgxSNMPUser.FindStringSubmatch(line)
		if len(matchUser) == 4 {
			snmpUser := SNMPUser{
				Name:    matchUser[1],
				Group:   matchUser[2],
				Version: matchUser[3],
			}
			if rgxSNMPUserEncrypted.MatchString(line) {
				snmpUser.Encrypted = true
			}
			matchAccess := rgxSNMPUserAccess.FindStringSubmatch(line)
			if len(matchAccess) == 2 {
				snmpUser.Access = matchAccess[1]
			}
			matchAuth := rgxSNMPUserAuth.FindStringSubmatch(line)
			if len(matchAuth) == 3 {
				snmpUser.AuthType = matchAuth[1]
				snmpUser.AuthPass = matchAuth[2]
			}
			matchPriv := rgxSNMPUserPriv.FindStringSubmatch(line)
			if len(matchPriv) == 3 {
				snmpUser.PrivType = matchPriv[1]
				snmpUser.PrivPass = matchPriv[2]
			}
			config.Users = append(config.Users, snmpUser)
			continue
		}
		//Hosts
		matchHost := rgxSNMPHost.FindStringSubmatch(line)
		if len(matchHost) == 2 {
			host := SNMPHost{
				Address: matchHost[1],
			}
			matchVersion := rgxSNMPHostVersion.FindStringSubmatch(line)
			if len(matchVersion) == 2 {
				host.Version = matchVersion[1]
			}
			matchCommunity = rgxSNMPHostCommunity.FindStringSubmatch(line)
			if len(matchCommunity) == 2 {
				host.Community = matchCommunity[1]
			}
			matchUser := rgxSNMPHostUser.FindStringSubmatch(line)
			if len(matchUser) == 3 {
				host.Security = matchUser[1]
				host.User = matchUser[2]
			}
			matchPort := rgxSNMPHostPort.FindStringSubmatch(line)
			if len(matchPort) == 2 {
				host.Port = matchPort[1]
			}
			config.Hosts = append(config.Hosts, host)
			continue
		}
	}
	for _, line := range strings.Split(engineRaw, "\n") {
		//engine ID
		matchEngineId := rgxSNMPEngineId.FindStringSubmatch(line)
		if len(matchEngineId) == 2 {
			config.EngineId = matchEngineId[1]
			continue
		}
	}

	return nil
}
