package winrm

import (
	
	
	
	
	

	
	
	
	
)

type ExecuteCommandError struct {
	Inner error
	Body  string
}

func ( *ExecuteCommandError) () string {
	if .Inner == nil {
		return "error"
	}

	return .Inner.Error()
}

func ( *ExecuteCommandError) ( error) bool {
	,  := .(*ExecuteCommandError)
	return 
}

func ( *ExecuteCommandError) () error {
	return .Inner
}

func first( tree.Node,  string) (string, error) {
	,  := xPath(, )
	if  != nil {
		return "", 
	}
	if len() < 1 {
		return "", 
	}
	return [0].ResValue(), nil
}

func any( tree.Node,  string) (bool, error) {
	,  := xPath(, )
	if  != nil {
		return false, 
	}
	if len() > 0 {
		return true, nil
	}
	return false, nil
}

func xPath( tree.Node,  string) (tree.NodeSet, error) {
	 := goxpath.MustParse()
	,  := .ExecNode(, soap.GetAllXPathNamespaces())
	if  != nil {
		return nil, 
	}
	return , nil
}

// ParseOpenShellResponse ParseOpenShellResponse
func ( string) (string, error) {
	,  := xmltree.ParseXML(strings.NewReader())
	if  != nil {
		return "", 
	}
	return first(, "//w:Selector[@Name='ShellId']")
}

// ParseExecuteCommandResponse ParseExecuteCommandResponse
func ( string) ( string,  error) {
	defer func() {
		if  != nil {
			 = &ExecuteCommandError{Inner: , Body: }
		}
	}()

	,  := xmltree.ParseXML(strings.NewReader())
	if  != nil {
		return "", fmt.Errorf("parsing xml response: %w", )
	}

	,  := first(, "//a:Action")
	if  != nil {
		return "", fmt.Errorf("getting response action: %w", )
	}

	switch  {
	case "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandResponse":
		,  = first(, "//rsp:CommandId")
		if  != nil {
			return "", fmt.Errorf("finding command id: %w", )
		}

		return , nil

	default:
		return "", fmt.Errorf("unsupported action: %v", )
	}
}

// ParseSlurpOutputErrResponse ParseSlurpOutputErrResponse
func ( string, ,  io.Writer) (bool, int, error) {
	var (
		 bool
		 int
	)

	,  := xmltree.ParseXML(strings.NewReader())

	,  := xPath(, "//rsp:Stream[@Name='stdout']")
	for ,  := range  {
		,  := base64.StdEncoding.DecodeString(.ResValue())
		.Write()
	}
	,  := xPath(, "//rsp:Stream[@Name='stderr']")
	for ,  := range  {
		,  := base64.StdEncoding.DecodeString(.ResValue())
		.Write()
	}

	,  := any(, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")

	if  {
		 = 
		if ,  := any(, "//rsp:ExitCode");  {
			,  := first(, "//rsp:ExitCode")
			, _ = strconv.Atoi()
		}
	} else {
		 = false
	}

	return , , 
}

// ParseSlurpOutputResponse ParseSlurpOutputResponse
func ( string,  io.Writer,  string) (bool, int, error) {
	var (
		 bool
		 int
	)

	,  := xmltree.ParseXML(strings.NewReader())

	,  := xPath(, fmt.Sprintf("//rsp:Stream[@Name='%s']", ))
	for ,  := range  {
		,  := base64.StdEncoding.DecodeString(.ResValue())
		_, _ = .Write()
	}

	,  := any(, "//*[@State='http://schemas.microsoft.com/wbem/wsman/1/windows/shell/CommandState/Done']")

	if  {
		 = 
		if ,  := any(, "//rsp:ExitCode");  {
			,  := first(, "//rsp:ExitCode")
			, _ = strconv.Atoi()
		}
	} else {
		 = false
	}

	return , , 
}