package resource

import (
	"fmt"

	commM "github.com/pingcap/test-infra/common/model"
)

type PhysicalResource commM.PhysicalResource

func (s PhysicalResource) Sub(o PhysicalResource) PhysicalResource {
	return PhysicalResource{CPU: commM.CPUResource{Core: s.CPU.Core - o.CPU.Core}, Memory: commM.MemoryResource{Byte: s.Memory.Byte - o.Memory.Byte}}
}

func (s PhysicalResource) Add(o PhysicalResource) PhysicalResource {
	return PhysicalResource{CPU: commM.CPUResource{Core: s.CPU.Core + o.CPU.Core}, Memory: commM.MemoryResource{Byte: s.Memory.Byte + o.Memory.Byte}}
}

func (s PhysicalResource) String() string {
	return fmt.Sprintf("{Cpu:%.2fc, mem: %.0fMiB}", s.CPU.Core, float64(s.Memory.Byte)/1024/1024)
}

func (s PhysicalResource) NonNegative() bool {
	return s.CPU.Core >= 0 && s.Memory.Byte >= 0
}

func (s PhysicalResource) IsZero() bool {
	return s.CPU.Core == 0 && s.Memory.Byte == 0
}

func (s PhysicalResource) Max(o PhysicalResource) PhysicalResource {
	cpuMax := s.CPU.Core
	if o.CPU.Core > cpuMax {
		cpuMax = o.CPU.Core
	}
	memMax := s.Memory.Byte
	if o.Memory.Byte > memMax {
		memMax = o.Memory.Byte
	}
	return PhysicalResource{CPU: commM.CPUResource{Core: cpuMax}, Memory: commM.MemoryResource{Byte: memMax}}
}
