package main

import (
	"fmt"
	"os"

	keychainlinker "git.aetherial.dev/aeth/keychain-linker/pkg"
	"github.com/godbus/dbus/v5"
	"github.com/godbus/dbus/v5/introspect"
)

func main() {
	conn, err := dbus.ConnectSessionBus()
	if err != nil {
		panic(err)
	}
	defer conn.Close()

	path := "/org/freedesktop/secrets"
	service := keychainlinker.NewService(dbus.ObjectPath(path))

	conn.Export(service, dbus.ObjectPath(path), "org.freedesktop.Secret.Service")
	conn.Export(service.Cache["/org/freedesktop/secrets/collection/default"], "/org/freedesktop/secrets/collection/default", "org.freedesktop.DBus.Properties")

	conn.Export(introspect.Introspectable(keychainlinker.DbusAdv), dbus.ObjectPath(path),
		"org.freedesktop.DBus.Introspectable")
<<<<<<< HEAD
	conn.Export(introspect.Introspectable(keychainlinker.DbusAdv), "/org/freedesktop/secrets/collection/default", "org.freedesktop.DBus.Introspectable")
	reply, err := conn.RequestName("org.freedesktop.secrets",
=======
	node := introspect.Node{
		Interfaces: []introspect.Interface{
			{
				Name: "org.freedesktop.secret.Service",
				Methods: []introspect.Method{
					{Name: "Get", Args: []introspect.Arg{{Name: "interface_name", Type: "s", Direction: "in"}, {Name: "property_name", Type: "s", Direction: "in"}, {Name: "value", Type: "v", Direction: "out"}}},
					{Name: "Set", Args: []introspect.Arg{{Name: "interface_name", Type: "s", Direction: "in"}, {Name: "property_name", Type: "s", Direction: "in"}, {Name: "value", Type: "v", Direction: "in"}}},
					{Name: "GetAll", Args: []introspect.Arg{{Name: "interface_name", Type: "s", Direction: "in"}, {Name: "props", Type: "a{sv}", Direction: "out"}}},
				},
			},
		},
	}
	conn.Export(introspect.Introspectable(introspect.NewIntrospectable(&node)), "/org/freedesktop/secret", "org.freedesktop.DBus.Introspectable")
	node = introspect.Node{
		Interfaces: []introspect.Interface{
			{
				Name: "org.freedesktop.secret.Collection",
				Methods: []introspect.Method{
					{Name: "Get", Args: []introspect.Arg{{Name: "interface_name", Type: "s", Direction: "in"}, {Name: "property_name", Type: "s", Direction: "in"}, {Name: "value", Type: "v", Direction: "out"}}},
					{Name: "Set", Args: []introspect.Arg{{Name: "interface_name", Type: "s", Direction: "in"}, {Name: "property_name", Type: "s", Direction: "in"}, {Name: "value", Type: "v", Direction: "in"}}},
				},
			},
		},
	}
	conn.Export(introspect.Introspectable(introspect.NewIntrospectable(&node)), "/org/freedesktop/secret", "org.freedesktop.DBus.IntrospectData")

	reply, err := conn.RequestName("dev.aetherial.git.KeychainLinker.Service",
>>>>>>> 2946fa107c12a5e2d1c88f3b60f8bea802b19fd7
		dbus.NameFlagDoNotQueue)
	if err != nil {
		panic(err)
	}
	if reply != dbus.RequestNameReplyPrimaryOwner {
		fmt.Fprintln(os.Stderr, "name already taken")
		os.Exit(1)
	}
	fmt.Println("Listening on org.freedesktop.secrets / /org/freedesktop/secrets ...")
	select {}
}
