package parsers

import (
	"regexp"
	"time"

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

var (
	verBlockRe        = regexp.MustCompile(`Type:\s+(.*?)\n((.|\n)*?)==`)
	backPlaneSerialRe = regexp.MustCompile(`(?i)backplane\s+serial\s+#:\s+(\S+)`)
	chassisSerial     = regexp.MustCompile(`(?i)chassis\s+serial\s+#:\s+(\S+)`)
	lidRe             = regexp.MustCompile(`(?i)lid\s+#:\s+(\S+)`)
	licenseRe         = regexp.MustCompile(`(?i)license:\s+(\S+)`)
	partNumberRe      = regexp.MustCompile(`(?i)part\s+#:\s+(\S+)`)
	sslCardSerialRe   = regexp.MustCompile(`(?i)ssl\s+card\s+-\s+serial\s+#:\s+(\S+)`)
	versionRe         = regexp.MustCompile(`(?i)version\s+#:\s+(\S+)`)
	uptimeDayRe       = regexp.MustCompile(`system\s+uptime.*?(\d+)\s+days`)
	uptimeHoursRe     = regexp.MustCompile(`system\s+uptime.*?(\d+)\s+hours?`)
	uptimeMinutesRe   = regexp.MustCompile(`system\s+uptime.*?(\d+)\s+minutes?`)
	uptimeSecondsRe   = regexp.MustCompile(`system\s+uptime.*?(\d+)\s+seconds?`)
	bootSourceRe      = regexp.MustCompile(`boot\s+source:\s+(\S+),`)
	modeSourceRe      = regexp.MustCompile(`mode:\s+(.*?),`)
	totalResetsRe     = regexp.MustCompile(`total\s+resets:(\d+)`)
	bootVersionRe     = regexp.MustCompile(`(?i)Boot\s+Version\s+(\S+)\s+(.*?)\s+label:\s+(\S+)`)
	monitorVersionRe  = regexp.MustCompile(`(?i)Monitor\s+Version\s+(\S+)\s+(.*?)\s+label:\s+(\S+)`)
	systemVersionRe   = regexp.MustCompile(`(?i)System\s+Version\s+(\S+)\s+(.*?)\s+label:\s+(\S+)`)
)

type (
	// AdxVersion is the parent type containing all the version information
	// derived from an ADX
	AdxVersion struct {
		Type           AdxType     `json:"type"`
		Uptime         AdxUptime   `json:"uptime"`
		Version        string      `json:"system_version"`
		BootSource     string      `json:"boot_source"`
		Mode           string      `json:"mode"`
		TotalResets    string      `json:"total_resets"`
		BootVersion    VersionType `json:"boot_version"`
		MonitorVersion VersionType `json:"monitor_version"`
		SystemVersion  VersionType `json:"sys_version"`
	}

	// AdxType type defines all the attributes that describe the platform
	// within the version info.
	AdxType struct {
		Platform        string `json:"platform"`
		BackPlaneSerial string `json:"backplane_serial"`
		ChassisSerial   string `json:"chassis_serial"`
		LiD             string `json:"lid"`
		Lic             string `json:"licences"`
		Part            string `json:"part"`
		SslCardSerial   string `json:"ssl_card_serial"`
		SslCardPart     string `json:"ssl_card_part"`
		Version         string `json:"version"`
	}

	// AdxUptime type defines all the attributes from the version info regarding uptime
	AdxUptime struct {
		Days    string `json:"days"`
		Hours   string `json:"hours"`
		Minutes string `json:"minutes"`
		Seconds string `json:"seconds"`
	}
	// VersionType type defines all the attributes from the version info regarding version type
	VersionType struct {
		Name  string `json:"name"`
		Date  int64  `json:"date"`
		Label string `json:"label"`
	}
)

// Parse method is used to implement type population of a given string
// using parsers to populate the method receiver.
func (a *AdxVersion) Parse(s string) {
	var t AdxType
	for _, v := range verBlockRe.FindAllStringSubmatch(s, -1) {
		t.Platform = v[1]
		t.BackPlaneSerial = util.RegexpFindString(backPlaneSerialRe, v[2])
		t.ChassisSerial = util.RegexpFindString(chassisSerial, v[2])
		t.LiD = util.RegexpFindString(lidRe, v[2])
		t.Lic = util.RegexpFindString(licenseRe, v[2])
		t.Part = util.RegexpFindString(partNumberRe, v[2])
		t.SslCardSerial = util.RegexpFindString(sslCardSerialRe, v[2])
		t.SslCardPart = util.RegexpFindString(sslCardSerialRe, v[2])
		t.Version = util.RegexpFindString(versionRe, v[2])
	}
	a.Type = t

	var u AdxUptime
	u.Days = util.RegexpFindString(uptimeDayRe, s)
	if u.Days == "" {
		u.Days = "0"
	}
	u.Hours = util.RegexpFindString(uptimeHoursRe, s)
	if u.Hours == "" {
		u.Hours = "0"
	}
	u.Minutes = util.RegexpFindString(uptimeMinutesRe, s)
	if u.Minutes == "" {
		u.Minutes = "0"
	}
	u.Seconds = util.RegexpFindString(uptimeSecondsRe, s)
	if u.Seconds == "" {
		u.Seconds = "0"
	}
	a.Uptime = u

	a.BootSource = util.RegexpFindString(bootSourceRe, s)
	a.Mode = util.RegexpFindString(modeSourceRe, s)
	a.TotalResets = util.RegexpFindString(totalResetsRe, s)
	bv := bootVersionRe.FindStringSubmatch(s)

	sv := systemVersionRe.FindStringSubmatch(s)
	a.Version = sv[3]
	a.SystemVersion = VersionType{
		Name:  sv[1],
		Date:  formatTime(sv[2]),
		Label: sv[3],
	}
	a.BootVersion = VersionType{
		Name:  bv[1],
		Date:  formatTime(bv[2]),
		Label: bv[3],
	}
	mv := monitorVersionRe.FindStringSubmatch(s)
	a.MonitorVersion = VersionType{
		Name:  mv[1],
		Date:  formatTime(mv[2]),
		Label: mv[3],
	}
}

func formatTime(timestamp string) int64 {
	t, err := time.Parse("Jan 02 2006 15:04:05 MST", timestamp)
	if err != nil {
		return 0
	}
	return t.Unix()
}
