package events

import (
	"log"
	"os"

	"libvirt.org/go/libvirt"
)

func agentLifecycle(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventAgentLifecycle) {
	println(event.State)
	println(event.Reason)

}

func graphics(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventGraphics) {
	println(event.String())
}

func reboot(c *libvirt.Connect, d *libvirt.Domain) {

}

func watchdog(c *libvirt.Connect, d *libvirt.Domain, event *libvirt.DomainEventWatchdog) {
	println(event.String())
}

func Server() {
	err := libvirt.EventRegisterDefaultImpl()
	if err != nil {
		log.Fatalf("Échec d'EventRegisterDefaultImpl: %v", err)
		os.Exit(0)
	}

	conn, err := libvirt.NewConnect("qemu:///system")
	if err != nil {
		log.Println("Connexion Error")
	}
	defer conn.Close()

	conn.SetKeepAlive(5, 3)

	conn.DomainEventAgentLifecycleRegister(nil, agentLifecycle)
	conn.DomainEventGraphicsRegister(nil, graphics)
	conn.DomainEventLifecycleRegister(nil, Lifecyle)
	conn.DomainEventRebootRegister(nil, reboot)
	conn.DomainEventWatchdogRegister(nil, watchdog)

	for {
		libvirt.EventRunDefaultImpl()
	}
}
