package diag

import (
	"fmt"

	"github.com/duffy/usb-server/internal/usb"
)

// speedName renders a USB/IP speed code.
func speedName(speed uint32) string {
	switch speed {
	case 1:
		return "low"
	case 2:
		return "full"
	case 3:
		return "high"
	case 5:
		return "super"
	case 6:
		return "super+"
	default:
		return "unknown"
	}
}

// transferTypeName renders an endpoint transfer type.
//
// This is the field to look at when a device attaches but produces no data:
// an interrupt endpoint reported as bulk is exactly that symptom, because the
// kernel rejects the transfer.
func transferTypeName(t uint8) string {
	switch t {
	case usb.TransferTypeControl:
		return "control"
	case usb.TransferTypeIsochronous:
		return "isochronous"
	case usb.TransferTypeBulk:
		return "bulk"
	case usb.TransferTypeInterrupt:
		return "interrupt"
	default:
		return fmt.Sprintf("unknown(%d)", t)
	}
}

// endpointInfo renders one endpoint for the report.
func endpointInfo(ep usb.Endpoint) EndpointInfo {
	direction := "OUT"
	if ep.IsIn() {
		direction = "IN"
	}

	return EndpointInfo{
		Address:      fmt.Sprintf("0x%02x", ep.Address),
		Direction:    direction,
		TransferType: transferTypeName(ep.TransferType),
		MaxPacket:    ep.MaxPacketSize,
		Interval:     ep.Interval,
	}
}
