package websocket

import (
	"primesys_command_producer/src/storage"
	"primesys_command_producer/src/types"
	"strconv"
	"strings"
)

func getServerFlagCount(flag string, num int) string {
	numStr := strconv.Itoa(num)
	var paddedNum string

	if len(numStr) >= 4 {
		paddedNum = numStr
	} else {
		count := 4 - len(numStr)
		var sb strings.Builder
		for range count {
			sb.WriteString("0")
		}
		sb.WriteString(numStr)
		paddedNum = sb.String()
	}

	return flag + paddedNum
}

// GetUserQueue returns the user's commands from data/command.json.
func GetUserQueue(userId string) ([]types.QueueCommand, error) {
	commands, err := storage.GetCommands()
	if err != nil {
		return nil, err
	}

	userCommands := make([]types.QueueCommand, 0)
	for _, cmd := range commands {
		if cmd.UserId == userId {
			userCommands = append(userCommands, cmd)
		}
	}

	return userCommands, nil
}

// withHash makes sure the device command ends with #
func withHash(command string) string {
	if strings.HasSuffix(command, "#") {
		return command
	}
	return command + "#"
}
