package formatter

import (
	"fmt"

	"github.com/SigNoz/signoz/pkg/query-service/converter"
	"github.com/dustin/go-humanize"
)

type dataFormatter struct {
}

func NewDataFormatter() Formatter {
	return &dataFormatter{}
}

func (*dataFormatter) Name() string {
	return "data"
}

func (f *dataFormatter) Format(value float64, unit string) string {
	switch unit {
	case "bytes", "By":
		return humanize.IBytes(uint64(value))
	case "decbytes":
		return humanize.Bytes(uint64(value))
	case "bits":
		return humanize.IBytes(uint64(value * converter.Bit))
	case "decbits":
		return humanize.Bytes(uint64(value * converter.Bit))
	case "kbytes", "kBy":
		return humanize.IBytes(uint64(value * converter.Kibibit))
	case "decKbytes", "deckbytes":
		return humanize.IBytes(uint64(value * converter.Kilobit))
	case "mbytes", "MBy":
		return humanize.IBytes(uint64(value * converter.Mebibit))
	case "decMbytes", "decmbytes":
		return humanize.Bytes(uint64(value * converter.Megabit))
	case "gbytes", "GBy":
		return humanize.IBytes(uint64(value * converter.Gibibit))
	case "decGbytes", "decgbytes":
		return humanize.Bytes(uint64(value * converter.Gigabit))
	case "tbytes", "TBy":
		return humanize.IBytes(uint64(value * converter.Tebibit))
	case "decTbytes", "dectbytes":
		return humanize.Bytes(uint64(value * converter.Terabit))
	case "pbytes", "PBy":
		return humanize.IBytes(uint64(value * converter.Pebibit))
	case "decPbytes", "decpbytes":
		return humanize.Bytes(uint64(value * converter.Petabit))
	}
	// When unit is not matched, return the value as it is.
	return fmt.Sprintf("%v", value)
}
