package websocket

import (
	"net/http"
	"strings"
)

// ParseDBCFromRequest retrieves DBC version from the "fisker-dbc" field
//
//	located in header of request
func ParseDBCFromRequest(r *http.Request) string {
	return r.Header.Get("Fisker-Dbc-Sha256")
}

func ParseICCIDFromRequest(r *http.Request) (string, error) {
	iccid := strings.TrimSpace(r.Header.Get("X-ICCID"))

	// ok, err := validator.ValidateICCIDSimple(iccid)
	// if err != nil {
	// 	return iccid, err
	// } else if !ok {
	// 	return iccid, errors.Errorf("%s failed to pass ICCID validation", iccid)
	// }

	return iccid, nil
}

// ParseDeviceAndVersionFromRequest parses device type and version
//
//	of client from User-Agent field in header
func ParseDeviceAndVersionFromRequest(r *http.Request) (string, string) {
	var device string
	var version string

	userAgent := r.Header.Get("User-Agent")
	specs := strings.Split(userAgent, " ")

	device = strings.ToLower(specs[0])

	switch len(specs) {
	case 5:
		version = specs[3]
	case 4:
		version = specs[2]
	case 2:
		version = specs[1]
	}

	return device, version
}
