package hotkey

import (
	"golang.design/x/hotkey"
	"log"
)

func Register(mod []hotkey.Modifier, key hotkey.Key, keyDown func(), keyUp func()) *hotkey.Hotkey {
	hk := hotkey.New(mod, key)
	go func() {
		defer func() {
			hk.Unregister()
			log.Printf("hotkey: %v is unregistered\n", hk)
		}()
		err := hk.Register()
		if err != nil {
			log.Printf("hotkey: failed to register hotkey: %v", err)
		}

		log.Printf("hotkey: %v is registered\n", hk)

		for {
			select {
			case <-hk.Keydown():
				log.Printf("hotkey: %v is down\n", hk)
				if keyDown != nil {
					keyDown()
				}
			case <-hk.Keyup():
				log.Printf("hotkey: %v is up\n", hk)
				if keyUp != nil {
					keyUp()
				}
			}
		}
	}()
	return hk
}
