package gupta

import (
	"fmt"
	"strconv"
)

const version = "0.0.1"

func Hello() string {
	return fmt.Sprintf("Welcome to gupta version %s", version)
}

func Run(load, cpu, memory bool, partition, networkInterface string) (cpuLoad, cpuUsage, memoryUsage float64) {
	cpuLoad = 0.2
	cpuUsage = 0.2
	memoryUsage = 70.0
	return cpuLoad, cpuUsage, memoryUsage
}

type GuptaReport struct {
	cpuLoad     float64
	cpuUsage    float64
	memoryUsage float64
}

func (g *GuptaReport) GetCPULoad() string {
	return fmt.Sprintf("%.2f", g.cpuLoad)
}

// collect all CPU stat from /proc/cpustat
type CPUStat struct {
	user       int64
	nice       int64
	system     int64
	idle       int64
	iowait     int64
	irq        int64
	softirq    int64
	steal      int64
	guest      int64
	guest_nice int64
}

// parse the first line of /proc/cpustat
func (c *CPUStat) ReadInfo(rawInfo []string) {
	if s, err := strconv.ParseInt(rawInfo[0], 10, 64); err == nil {
		c.user = s
	}
	if s, err := strconv.ParseInt(rawInfo[1], 10, 64); err == nil {
		c.nice = s
	}
	if s, err := strconv.ParseInt(rawInfo[2], 10, 64); err == nil {
		c.system = s
	}
	if s, err := strconv.ParseInt(rawInfo[3], 10, 64); err == nil {
		c.idle = s
	}
	if s, err := strconv.ParseInt(rawInfo[4], 10, 64); err == nil {
		c.idle = s
	}
	if s, err := strconv.ParseInt(rawInfo[5], 10, 64); err == nil {
		c.iowait = s
	}
	if s, err := strconv.ParseInt(rawInfo[6], 10, 64); err == nil {
		c.irq = s
	}
	if s, err := strconv.ParseInt(rawInfo[7], 10, 64); err == nil {
		c.softirq = s
	}
	if s, err := strconv.ParseInt(rawInfo[8], 10, 64); err == nil {
		c.softirq = s
	}
	if s, err := strconv.ParseInt(rawInfo[9], 10, 64); err == nil {
		c.steal = s
	}
	if s, err := strconv.ParseInt(rawInfo[10], 10, 64); err == nil {
		c.guest = s
	}
	if s, err := strconv.ParseInt(rawInfo[11], 10, 64); err == nil {
		c.guest_nice = s
	}
}