//go:build linux

package main

import (
	"fmt"
	"os/exec"
	"sync"
)

type dialogToolType int

const (
	toolNone dialogToolType = iota
	toolZenity
	toolKdialog
	toolNotifySend
	toolXmessage
)

var (
	dialogTool     dialogToolType
	dialogToolOnce sync.Once
)

func init() {
	dialogToolOnce.Do(detectDialogTool)
}

func detectDialogTool() {
	tools := []struct {
		name string
		typ  dialogToolType
	}{
		{"zenity", toolZenity},
		{"kdialog", toolKdialog},
		{"notify-send", toolNotifySend},
		{"xmessage", toolXmessage},
	}

	for _, tool := range tools {
		if _, err := exec.LookPath(tool.name); err == nil {
			dialogTool = tool.typ
			return
		}
	}

	dialogTool = toolNone
}

func showError(title, message string) {
	switch dialogTool {
	case toolZenity:
		exec.Command("zenity", "--error",
			fmt.Sprintf("--title=%s", title),
			fmt.Sprintf("--text=%s", message)).Run()
	case toolKdialog:
		exec.Command("kdialog", "--error", message, "--title", title).Run()
	case toolNotifySend:
		exec.Command("notify-send", "-u", "critical", title, message).Run()
	case toolXmessage:
		exec.Command("xmessage", "-center",
			fmt.Sprintf("%s: %s", title, message)).Run()
	default:
		dialogLogger().Error("无法显示错误对话框")
	}
}
