package utils

import (
	"net/http"
	"strconv"
	"strings"
)

// 客户端类型
const (
	ClientAdmin  = "client-admin"
	ClientWechat = "client-wechat"
	ClientMini   = "client-mini"
)

// 一些 UA 列表
const (
	UAWechat      = "micromessenger"
	UAMiniProgram = "miniprogram"
)

// GetClientType 获取客户端类型
// 默认返回是 管理端
func GetClientType(r *http.Request) string {
	ua := strings.ToLower(r.Header.Get("User-Agent"))

	if strings.Index(ua, UAWechat) > -1 {
		if strings.Index(ua, UAMiniProgram) > -1 {
			return ClientMini
		}

		return ClientWechat
	}

	return ClientAdmin
}

// StrSliceIndexOf slice indexof
func StrSliceIndexOf(s []string, t string) int64 {
	if s == nil || len(s) == 0 {
		return -1
	}

	for k, v := range s {
		if v == t {
			return int64(k)
		}
	}

	return -1
}

// GUIID2IntString guid 转 int 字符串
func GUIID2IntString(id string) []string {
	if id == "" {
		return nil
	}

	res := make([]string, 0)
	for _, part := range strings.Split(id, "-") {
		sum := 0
		for _, r := range []rune(part) {
			sum += int(r)
		}

		res = append(res, strconv.Itoa(sum))
	}

	return res
}
