//go:build !windows && !darwin

package upower

import (
	"context"
	"errors"
	"fmt"
	"slices"
	"time"

	"github.com/godbus/dbus/v5"

	"github.com/influxdata/telegraf"
	"github.com/influxdata/telegraf/internal"
)

// Enumerations as defined in
// https://upower.freedesktop.org/docs/Device.html
var (
	deviceTypes = []string{
		"unknown", "line_power", "battery", "ups", "monitor", "mouse", "keyboard",
		"pda", "phone", "media_player", "tablet", "computer", "gaming_input", "pen",
		"touchpad", "modem", "network", "headset", "speakers", "headphones", "video",
		"other_audio", "remote_control", "printer", "scanner", "camera", "wearable",
		"toy", "bluetooth_generic",
	}
	deviceStates = []string{
		"unknown", "charging", "discharging", "empty", "fully_charged",
		"pending_charge", "pending_discharge",
	}
	technologies = []string{
		"unknown", "lithium_ion", "lithium_polymer", "lithium_iron_phosphate",
		"lead_acid", "nickel_cadmium", "nickel_metal_hydride",
	}
	warningLevels = []string{"unknown", "none", "discharging", "low", "critical", "action"}
)

func (u *UPower) Init() error {
	if u.Timeout < 0 {
		return errors.New("timeout must not be negative")
	}

	// An empty list disables the device type filter in addDevice. Do not fill it
	// with all known types as this would drop devices with types added in newer
	// UPower versions.
	for _, t := range u.DeviceTypes {
		if !slices.Contains(deviceTypes, t) {
			return fmt.Errorf("invalid device type %q", t)
		}
	}

	return nil
}

func (u *UPower) Start(telegraf.Accumulator) error {
	conn, err := dbus.ConnectSystemBus()
	if err != nil {
		return &internal.StartupError{
			Err:   fmt.Errorf("connecting to system bus failed: %w", err),
			Retry: true,
		}
	}
	u.client = &dbusClient{conn: conn}

	return nil
}

func (u *UPower) Stop() {
	if u.client == nil {
		return
	}
	if err := u.client.close(); err != nil {
		u.Log.Errorf("Closing system bus connection failed: %v", err)
	}
	u.client = nil
}

func (u *UPower) Gather(acc telegraf.Accumulator) error {
	if u.client == nil || !u.client.connected() {
		u.Log.Debug("Connection to system bus lost, trying to reconnect...")
		u.Stop()
		if err := u.Start(acc); err != nil {
			return err
		}
	}

	ctx := context.Background()
	if u.Timeout > 0 {
		var cancel context.CancelFunc
		ctx, cancel = context.WithTimeout(ctx, time.Duration(u.Timeout))
		defer cancel()
	}

	paths, err := u.client.enumerateDevices(ctx)
	if err != nil {
		return fmt.Errorf("enumerating devices failed: %w", err)
	}

	for _, path := range paths {
		props, err := u.client.deviceProperties(ctx, path)
		if err != nil {
			acc.AddError(fmt.Errorf("getting properties of device %q failed: %w", path, err))
			continue
		}
		u.addDevice(acc, props)
	}

	return nil
}

func (u *UPower) addDevice(acc telegraf.Accumulator, props map[string]dbus.Variant) {
	deviceType := lookup(deviceTypes, property[uint32](props, "Type"))
	if len(u.DeviceTypes) > 0 && !slices.Contains(u.DeviceTypes, deviceType) {
		return
	}

	tags := map[string]string{
		"native_path": property[string](props, "NativePath"),
		"type":        deviceType,
	}
	for tag, name := range map[string]string{"vendor": "Vendor", "model": "Model", "serial": "Serial"} {
		if v := property[string](props, name); v != "" {
			tags[tag] = v
		}
	}

	// Line-power devices (e.g. AC adapters) only report their online state
	if deviceType == "line_power" {
		fields := map[string]any{
			"online": property[bool](props, "Online"),
		}
		acc.AddGauge("upower", fields, tags)
		return
	}

	tags["state"] = lookup(deviceStates, property[uint32](props, "State"))
	tags["technology"] = lookup(technologies, property[uint32](props, "Technology"))
	tags["warning_level"] = lookup(warningLevels, property[uint32](props, "WarningLevel"))

	fields := map[string]any{
		"percentage":      property[float64](props, "Percentage"),
		"energy_wh":       property[float64](props, "Energy"),
		"energy_empty_wh": property[float64](props, "EnergyEmpty"),
		"energy_rate_w":   property[float64](props, "EnergyRate"),
		"charge_cycles":   property[int32](props, "ChargeCycles"),
		"is_present":      property[bool](props, "IsPresent"),
		"is_rechargeable": property[bool](props, "IsRechargeable"),
		"power_supply":    property[bool](props, "PowerSupply"),
	}

	// UPower reports zero if the following properties are unknown
	for field, name := range map[string]string{
		"energy_full_wh":        "EnergyFull",
		"energy_full_design_wh": "EnergyFullDesign",
		"voltage_v":             "Voltage",
		"temperature_c":         "Temperature",
		"capacity_percent":      "Capacity",
	} {
		if v := property[float64](props, name); v != 0 {
			fields[field] = v
		}
	}
	for field, name := range map[string]string{
		"time_to_empty_sec": "TimeToEmpty",
		"time_to_full_sec":  "TimeToFull",
	} {
		if v := property[int64](props, name); v != 0 {
			fields[field] = v
		}
	}

	acc.AddGauge("upower", fields, tags)
}

func property[T any](props map[string]dbus.Variant, name string) T {
	var zero T
	v, ok := props[name]
	if !ok {
		return zero
	}
	value, ok := v.Value().(T)
	if !ok {
		return zero
	}
	return value
}

func lookup(names []string, code uint32) string {
	if int(code) < len(names) {
		return names[code]
	}
	return fmt.Sprintf("unknown-%d", code)
}
