package exporter

import (
	"git.entr0py.de/garionion/pac3100-exporter/scraper"
	"github.com/prometheus/client_golang/prometheus"
)

type Exporter struct {
	Voltage       *prometheus.GaugeVec
	Current       *prometheus.GaugeVec
	ApparantPower *prometheus.GaugeVec
	ActivePower   *prometheus.GaugeVec
	Frequency     *prometheus.GaugeVec
}

func New() Exporter {
	var exporter Exporter

	exporter.Voltage = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: "siemens",
		Subsystem: "pac3100",
		Name:      "voltage",
		Help:      "Voltage in V",
	}, []string{"device", "phases"})

	exporter.Current = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: "siemens",
		Subsystem: "pac3100",
		Name:      "current",
		Help:      "Current in A",
	}, []string{"device", "phase"})

	exporter.ApparantPower = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: "siemens",
		Subsystem: "pac3100",
		Name:      "apparent_power",
		Help:      "Apparent Power in VA",
	}, []string{"device", "phase"})

	exporter.ActivePower = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: "siemens",
		Subsystem: "pac3100",
		Name:      "active_power",
		Help:      "Active Power in W",
	}, []string{"device", "phase"})

	exporter.Frequency = prometheus.NewGaugeVec(prometheus.GaugeOpts{
		Namespace: "siemens",
		Subsystem: "pac3100",
		Name:      "frequency",
		Help:      "Frequency in Hz",
	}, []string{"device"})

	prometheus.MustRegister(exporter.Voltage)
	prometheus.MustRegister(exporter.Current)
	prometheus.MustRegister(exporter.ApparantPower)
	prometheus.MustRegister(exporter.ActivePower)
	prometheus.MustRegister(exporter.Frequency)

	return exporter
}

func (e Exporter) SetMeasurements(measurement scraper.Measurements, deviceName string) {
	e.Voltage.WithLabelValues(deviceName, "A_N").Set(float64(measurement.VoltageAN))
	e.Voltage.WithLabelValues(deviceName, "B_N").Set(float64(measurement.VoltageBN))
	e.Voltage.WithLabelValues(deviceName, "C_N").Set(float64(measurement.VoltageCN))

	e.Voltage.WithLabelValues(deviceName, "A_B").Set(float64(measurement.VoltageAB))
	e.Voltage.WithLabelValues(deviceName, "B_C").Set(float64(measurement.VoltageBC))
	e.Voltage.WithLabelValues(deviceName, "C_A").Set(float64(measurement.VoltageCA))

	e.Current.WithLabelValues(deviceName, "A").Set(float64(measurement.CurrentA))
	e.Current.WithLabelValues(deviceName, "B").Set(float64(measurement.CurrentB))
	e.Current.WithLabelValues(deviceName, "C").Set(float64(measurement.CurrentC))

	e.ApparantPower.WithLabelValues(deviceName, "A").Set(float64(measurement.ApparantPowerA))
	e.ApparantPower.WithLabelValues(deviceName, "B").Set(float64(measurement.ApparantPowerB))
	e.ApparantPower.WithLabelValues(deviceName, "C").Set(float64(measurement.ApparantPowerC))

	e.ActivePower.WithLabelValues(deviceName, "A").Set(float64(measurement.ActivePowerA))
	e.ActivePower.WithLabelValues(deviceName, "B").Set(float64(measurement.ActivePowerB))
	e.ActivePower.WithLabelValues(deviceName, "C").Set(float64(measurement.ActivePowerC))

	e.Frequency.WithLabelValues(deviceName).Set(float64(measurement.Frequency))
}
