package parsers

// used with "sh int"

import (
	"fmt"
	"regexp"
	"strings"

	"github.rackspace.com/gorax/tools/util"
)

var (
	intRe             = regexp.MustCompile(`(\S+)\s+is\s+(\S+),\s+line\s+protocol\s+is\s+(\S+)\s*\n((.|\n)*?DMA\s+transmitted\s+\d+\s+packets?)`)
	hardwareRe        = regexp.MustCompile(`Hardware\s+is\s+(\S+),`)
	macAddrRe         = regexp.MustCompile(`address\s+is\s+(\S+)`)
	biaMacAddrRe      = regexp.MustCompile(`\(bia\s+(\S+?)\)`)
	intCfgSpeedRe     = regexp.MustCompile(`Configured\s+speed\s+(\S+)`)
	intActSpeedRe     = regexp.MustCompile(`Configured\s+speed\s+\S+,\s+actual\s+(\S+)`)
	intCfgDuplexRe    = regexp.MustCompile(`configured\s+duplex\s+(\S+)`)
	intActDuplexRe    = regexp.MustCompile(`configured\s+duplex\s+\S+,\s+actual\s+(\S+)`)
	intTaggingRe      = regexp.MustCompile(`port\s+is\s+(tagged|untagged)`)
	intPortStateRe    = regexp.MustCompile(`port\s+state\s+is\s+(\S+)`)
	intMTURe          = regexp.MustCompile(`MTU\s+(\d+)\s+bytes`)
	intInputRateRe    = regexp.MustCompile(`input\s+rate:\s+(\d+\s+bits/sec)`)
	intOutputRateRe   = regexp.MustCompile(`output\s+rate:\s+(\d+\s+bits/sec)`)
	intInputErrorsRe  = regexp.MustCompile(`(\d+)\s+input\s+errors`)
	intCRCErrorsRe    = regexp.MustCompile(`(\d+)\s+CRC`)
	intFrameErrorsRe  = regexp.MustCompile(`(\d+)\s+frame`)
	intRuntsRe        = regexp.MustCompile(`(\d+)\s+runts`)
	intGiantsRe       = regexp.MustCompile(`(\d+)\s+giants`)
	intOutputErrorsRe = regexp.MustCompile(`(\d+)\s+output\s+errors`)
	intCollisionsRe   = regexp.MustCompile(`(\d+)\s+collisions`)
)

type (
	// Interfaces type is an aggregate type to define all the of
	// configured interfaces on an ADX
	Interfaces struct {
		Int []Interface `json:"interfaces"`
	}

	// Interface type defines all the parsed elements of a single interface
	// into a Go struct format.
	Interface struct {
		Name             string `json:"name"`
		State            string `json:"state"`
		LineProtocol     string `json:"line_protocol"`
		Hardware         string `json:"hardware"`
		MacAddress       string `json:"mac_address"`
		BiaMacAddress    string `json:"bia_mac_address"`
		ConfiguredSpeed  string `json:"configured_speed"`
		ActualSpeed      string `json:"actual_speed"`
		ConfiguredDuplex string `json:"configured_duplex"`
		ActualDuplex     string `json:"actual_duplex"`
		Tagging          string `json:"tagging"`
		PortState        string `json:"port_state"`
		MTU              string `json:"mtu"`
		InputRate        string `json:"input_rate"`
		OutputRate       string `json:"output_rate"`
		InputErrors      string `json:"input_errors"`
		CRCErrors        string `json:"crc_errors"`
		FrameErrors      string `json:"frame_errors"`
		Runts            string `json:"runts"`
		Giants           string `json:"giants"`
		OutputErrors     string `json:"output_errors"`
		Collisions       string `json:"collisions"`
	}

	// InterfacesStatus type is an aggregate type to define all the of
	// statistics on the interfaces of an ADX
	InterfacesStatus struct {
		Interfaces []InterfaceStatus `json:"show_interfaces"`
	}

	// InterfaceStatus type defines all the parsed elements of a single interface stats
	// into a Go struct format.
	InterfaceStatus struct {
		Name         string            `json:"interface"`
		IfName       string            `json:"ifname"`
		Admin        bool              `json:"admin_up"`
		Link         bool              `json:"link"`
		Hardware     string            `json:"hardware"`
		MAC          string            `json:"mac_address"`
		IPAddress    string            `json:"ip_address"`
		Netmask      string            `json:"netmask"`
		Number       *int              `json:"number,omitempty"`
		SubInterface []InterfaceStatus `json:"sub_interfaces"`
	}
)

// Parse method is used to implement type population of a given string
// using parsers to populate the method receiver.
func (a *Interfaces) Parse(s *string) {
	for _, v := range intRe.FindAllStringSubmatch(*s, -1) {
		var i Interface
		i.Name = v[1]
		i.State = v[2]
		i.LineProtocol = v[3]
		i.parse(v[4])
		a.Int = append(a.Int, i)
	}
}

func (i *Interface) parse(s string) {
	i.Hardware = util.RegexpString(hardwareRe, s)
	i.MacAddress = util.RegexpString(macAddrRe, s)
	i.BiaMacAddress = util.RegexpString(biaMacAddrRe, s)
	i.ConfiguredSpeed = util.RegexpString(intCfgSpeedRe, s)
	i.ActualSpeed = util.RegexpString(intActSpeedRe, s)
	i.ConfiguredDuplex = util.RegexpString(intCfgDuplexRe, s)
	i.ActualDuplex = util.RegexpString(intActDuplexRe, s)
	i.Tagging = util.RegexpString(intTaggingRe, s)
	i.PortState = util.RegexpString(intPortStateRe, s)
	i.MTU = util.RegexpString(intMTURe, s)
	i.InputRate = util.RegexpString(intInputRateRe, s)
	i.OutputRate = util.RegexpString(intOutputRateRe, s)
	i.InputErrors = util.RegexpString(intInputErrorsRe, s)
	i.CRCErrors = util.RegexpString(intCRCErrorsRe, s)
	i.FrameErrors = util.RegexpString(intFrameErrorsRe, s)
	i.Runts = util.RegexpString(intRuntsRe, s)
	i.Giants = util.RegexpString(intGiantsRe, s)
	i.OutputErrors = util.RegexpString(intOutputErrorsRe, s)
	i.Collisions = util.RegexpString(intCollisionsRe, s)
}

// InterfaceMAC function will take a given interface, and return
// the associated mac-address
func InterfaceMAC(name string, i int, il []Interface) string {
	for _, is := range il {
		if strings.Contains(is.Name, fmt.Sprintf("%s%d", name, i)) {
			return is.MacAddress
		}
	}
	return ""
}
