package winrm

import (
	

	
	
)

func genUUID() string {
	 := uuid.Must(uuid.NewV4())
	return "uuid:" + .String()
}

func defaultHeaders( *soap.SoapMessage,  string,  *Parameters) *soap.SoapHeader {
	return .
		Header().
		To().
		ReplyTo("http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous").
		MaxEnvelopeSize(.EnvelopeSize).
		Id(genUUID()).
		Locale(.Locale).
		Timeout(.Timeout)
}

//NewOpenShellRequest makes a new soap request
func ( string,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}

	 := soap.NewMessage()
	defaultHeaders(, , ).
		Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Create").
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		AddOption(soap.NewHeaderOption("WINRS_NOPROFILE", "FALSE")).
		AddOption(soap.NewHeaderOption("WINRS_CODEPAGE", "65001")).
		Build()

	 := .CreateBodyElement("Shell", soap.DOM_NS_WIN_SHELL)
	 := .CreateElement(, "InputStreams", soap.DOM_NS_WIN_SHELL)
	.SetContent("stdin")
	 := .CreateElement(, "OutputStreams", soap.DOM_NS_WIN_SHELL)
	.SetContent("stdout stderr")

	return 
}

// NewDeleteShellRequest ...
func (,  string,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}
	 := soap.NewMessage()
	defaultHeaders(, , ).
		Action("http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete").
		ShellId().
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		Build()

	.NewBody()

	return 
}

// NewExecuteCommandRequest exec command on specific shellID
func (, ,  string,  []string,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}
	 := soap.NewMessage()
	defaultHeaders(, , ).
		Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command").
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		ShellId().
		AddOption(soap.NewHeaderOption("WINRS_CONSOLEMODE_STDIN", "TRUE")).
		AddOption(soap.NewHeaderOption("WINRS_SKIP_CMD_SHELL", "FALSE")).
		Build()

	 := .CreateBodyElement("CommandLine", soap.DOM_NS_WIN_SHELL)

	// ensure special characters like & don't mangle the request XML
	 = "<![CDATA[" +  + "]]>"
	 := .CreateElement(, "Command", soap.DOM_NS_WIN_SHELL)
	.SetContent()

	for ,  := range  {
		 = "<![CDATA[" +  + "]]>"
		 := .CreateElement(, "Arguments", soap.DOM_NS_WIN_SHELL)
		.SetContent()
	}

	return 
}

//NewGetOutputRequest NewGetOutputRequest
func (, , ,  string,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}
	 := soap.NewMessage()
	defaultHeaders(, , ).
		Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive").
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		ShellId().
		Build()

	 := .CreateBodyElement("Receive", soap.DOM_NS_WIN_SHELL)
	 := .CreateElement(, "DesiredStream", soap.DOM_NS_WIN_SHELL)
	.SetAttr("CommandId", )
	.SetContent()

	return 
}

//NewSendInputRequest NewSendInputRequest
func (, ,  string,  []byte,  bool,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}
	 := soap.NewMessage()

	defaultHeaders(, , ).
		Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send").
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		ShellId().
		Build()

	 := base64.StdEncoding.EncodeToString()

	 := .CreateBodyElement("Send", soap.DOM_NS_WIN_SHELL)
	 := .CreateElement(, "Stream", soap.DOM_NS_WIN_SHELL)
	.SetAttr("Name", "stdin")
	.SetAttr("CommandId", )
	.SetContent()
	if  {
		.SetAttr("End", "true")
	}
	return 
}

//NewSignalRequest NewSignalRequest
func ( string,  string,  string,  *Parameters) *soap.SoapMessage {
	if  == nil {
		 = DefaultParameters
	}
	 := soap.NewMessage()

	defaultHeaders(, , ).
		Action("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal").
		ResourceURI("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/cmd").
		ShellId().
		Build()

	 := .CreateBodyElement("Signal", soap.DOM_NS_WIN_SHELL)
	.SetAttr("CommandId", )
	 := .CreateElement(, "Code", soap.DOM_NS_WIN_SHELL)
	.SetContent("http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")

	return 
}