package system

import (
	"github.com/shirou/gopsutil/process"
	"os"
)

func (i *InfoUtils) GetCurrentPid() int {
	return os.Getpid()
}

func (i *InfoUtils) GetAllProcessPid() []int32 {
	pidList, _ := process.Pids()
	return pidList
}

type ProcessDTO struct {
	Pid        int32  `json:"pid"`
	Name       string `json:"name"`
	Parent     int32  `json:"parent"`
	Status     string `json:"status"`
	CreateTime int64  `json:"createTime"`
}

func (i *InfoUtils) process2ProcessDTO(p *process.Process) *ProcessDTO {
	d := &ProcessDTO{
		Pid:        p.Pid,
		Name:       "",
		Parent:     0,
		Status:     "",
		CreateTime: 0,
	}

	d.Name, _ = p.Name()
	parent, _ := p.Parent()
	if parent != nil {
		d.Parent = parent.Pid
	}
	d.CreateTime, _ = p.CreateTime()
	d.Status, _ = p.Status()
	return d
}

func (i *InfoUtils) GetProcessInfo(pid int32) *ProcessDTO {
	info, _ := process.NewProcess(pid)
	return i.process2ProcessDTO(info)
}

func (i *InfoUtils) GetProcesses() []*ProcessDTO {
	info, _ := process.Processes()
	dto := make([]*ProcessDTO, 0)
	for _, p := range info {
		dto = append(dto, i.process2ProcessDTO(p))
	}
	return dto
}
