package ui

import (
	"fmt"
	"strings"
)

type UsageInputs struct {
	Id            string
	SshKeys       []string
	ErrorMessages []string

	RemoteShells          map[Shell]bool
	DownloadCommand       DownloadMethod
	CertificateValidation bool
}

func NewUsageInputs(id string, sshPublicKeys []string, remoteShells []string, localShells []string,
	downloadCommand string,
	certificateValidation bool) UsageInputs {
	inputs := UsageInputs{
		Id:                    id,
		SshKeys:               sshPublicKeys,
		RemoteShells:          make(map[Shell]bool),
		DownloadCommand:       DownloadMethod(downloadCommand),
		CertificateValidation: certificateValidation,
	}
	for _, remoteShell := range remoteShells {
		inputs.RemoteShells[Shell(remoteShell)] = true
	}
	return inputs
}

func addSshKeys(shell Shell, keys []string) string {

	quote := `"`
	if shell == CMD {
		quote = ""
	}
	res := ""
	for index, key := range keys {
		key = strings.TrimSpace(key)
		if key == "" {
			continue
		}
		operator := ">>"
		if index == 0 {
			operator = ">"
		}
		if shell == POWERSHELL {
			append := ""
			if index > 0 {
				append = "-Append "
			}
			res += fmt.Sprintf(`"%s" | Out-File %s-FilePath ".authorized_keys" -Encoding ASCII<br/>`,
				key, append)
		} else {
			res += fmt.Sprintf("        echo %s%s%s %s .authorized_keys<br/>", quote, key, quote,
				operator)
		}
	}
	return res + "       "
}

func formControlTextClass(message string) string {
	if message == "" {
		return "form-control"
	}
	return "form-control is-invalid"
}

func GetDownloadSecureOption(inputs UsageInputs) string {
	switch inputs.DownloadCommand {
	case CURL:
		if !inputs.CertificateValidation {
			return "-k"
		}
	case WGET:
		if !inputs.CertificateValidation {
			return "--no-check-certificate"
		}
	}
	return ""
}

func GetAgentSecureOption(inputs UsageInputs) string {
	if inputs.CertificateValidation {
		return ""
	}
	return "--insecure"
}