package public

import(
    "os/exec"
	"github.com/lxn/walk"
	"strings"
	registry "github.com/golang/sys/windows/registry"

)

//
//
//  local exec
//
func execcmd_base(cmdargs string) (string,[]string,error){
	if cmdargs == "" {
		return "",nil,Error_NullCommand
	}

	var app string
	var appargs []string

	spaceindex:=strings.Index(cmdargs," ")
	if spaceindex != -1{
		app 		= cmdargs[:spaceindex]
		appargs 	= strings.Split(cmdargs[spaceindex+1:]," ")
	}else{
		app 		= cmdargs
		appargs[0] 	= ""
	}
	PrintLog(app,appargs)

	return app,appargs,nil
}
func Execcmd_nowait(cmdargs  string){
	app,appargs,err :=execcmd_base(cmdargs)
	if err !=nil{
		PrintLog(err)
		return
	}
	if err := exec.Command(app,appargs...).Start();err !=nil {
		PrintLog(err)
	}
}
func Execcmd_wait(cmdargs  string){
	app,appargs,err :=execcmd_base(cmdargs)
	if err !=nil{
		PrintLog(err)
		return
	}
	cmd := exec.Command(app,appargs...)
	if err := cmd.Start(); err != nil {
		PrintLog(err)
	}
	PrintLog("Waiting for command to finish...")
	err = cmd.Wait()
	PrintLog("Command finished with error: ",err)
}
func Execcmd_output(cmdargs  string)[]byte{
	app,appargs,err :=execcmd_base(cmdargs)
	if err !=nil{
		PrintLog(err)
		return nil
	}
	if out,err2 := exec.Command(app,appargs...).CombinedOutput();err2 !=nil {
		PrintLog(err2)
	}else{
		return out
	}
	return nil
}


//
//
//  local 
//
func ChooseFolder(explain, initDir * string) string {
	fg := new(walk.FileDialog)
	fg.Title = *explain
	fg.InitialDirPath = *initDir
	fg.ShowBrowseFolder(*new(walk.Form))
	return fg.FilePath
}
func OpenWindow(s string){
	PrintLog(s)
	Execcmd_nowait("explorer "+s)
}

//
//
//  remote exec
//
func PsExec_sd(pc ,cmd string) {
	psExecCommand := `psexec \\` + pc + " -s -d "+cmd
	Execcmd_nowait(psExecCommand)
}
func PsExec_sdi(pc ,cmd ,sessionid string) {
	psExecCommand := `psexec \\` + pc + " -s -d -i "+sessionid +" "+cmd 
	Execcmd_nowait(psExecCommand)
}
func PsExec_sdic(pc ,cmd ,sessionid string) {
	psExecCommand := `psexec \\` + pc + " -s -d -i "+sessionid + " -c "+cmd 
	Execcmd_nowait(psExecCommand)
}


//
//
//  msg show
//

// UI：显示带确定的消息框
func Msg(message  string)  {
	walk.MsgBox(*new(walk.Form), "提示", message, walk.MsgBoxIconInformation)	
}
// UI:显示带确定和取消的消息框
// 点击确定按钮：返回1，
// 点击取消按钮：返回2
func Msg_YesNo(message  string) int {
	return walk.MsgBox(*new(walk.Form), "提示", message, walk.MsgBoxOKCancel)	
}
func MsgBox(message string){
	Execcmd_nowait("msg * /TIME:60 "+ message)
}
func SysMsgBox(pc,message string){
	Execcmd_nowait("msg * /SERVER:"+pc+" /TIME:60 "+ message)
}


//
//
//  regedit
//
func reg_prase(root * int) registry.Key{
	switch *root{
	case HKCU:
		return registry.CURRENT_USER
	case HKU:
		return registry.USERS
	case HKLM:
		return registry.LOCAL_MACHINE
	}
	return registry.LOCAL_MACHINE
}
func Reg_Exist_item(root int,path string ) {
	_,exist,err := registry.CreateKey(reg_prase(&root),path,registry.ALL_ACCESS)
	if err !=nil {
		PrintLog(err)
	}
	if !exist {
		PrintLog("Create ",path)
	}
}
func Reg_query_item_one(root int ,path string) string {
	if k, kerr := registry.OpenKey(reg_prase(&root),path, registry.READ);kerr==nil{
		defer k.Close()
		if sk,err := k.ReadSubKeyNames(1); err ==nil{
			return sk[0]
		} else{
			PrintLog(err)
			return ""
		}
	} else {
		PrintLog(kerr)
		return ""
	}
}
func Reg_query_key_one(root  int, path, key string) string {
	if k, kerr := registry.OpenKey(reg_prase(&root),path, registry.READ);kerr==nil{
		defer k.Close()
		if sk,_,err := k.GetStringValue(key); err ==nil{
			PrintLog(root,path,key,` -> `,sk)
			return sk
		} else{
			PrintLog(err)
			return ""
		}
	} else {
		PrintLog(kerr)
		return ""
	}
}
func Reg_Write_key_one(root int, path, name, value string) {
	if k, kerr := registry.OpenKey(reg_prase(&root),path, registry.WRITE);kerr==nil{
		defer k.Close()
		if err := k.SetStringValue(name, value); err !=nil{
			PrintLog(err)
		}
	} else {
		PrintLog(kerr)
	}
}