func Sleep(duration time.Duration) {
	<-time.NewTimer(duration).C
}
Sleep pauses the current goroutine for the specified duration
package utils
import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"html/template"
	"log"
	"math/rand"
	"net/http"
	"net/url"
	"strings"
	"time"
)
var scopeList = []string{
	"Fly to Mars", "Travel back in time", "Ride a dragon",
	"Go the center of the Earth", "Be Batman", "Run faster than light",
	"Live forever", "Be invisible", "Summon a genie",
	"Discover a lost city", "Stop global warming", "See into the future",
}
func getRandomUniqueScopes(count int) []string {
	if count >= len(scopeList) {
		return scopeList
	}
	var indices = make(map[int]struct{})
	for {
		r := rand.Int() % len(scopeList)
		if _, found := indices[r]; !found {
			indices[r] = struct{}{}
			if len(indices) >= count {
				break
			}
		}
	}
	var scopes = make([]string, count)
	i := 0
	for j := range indices {
		scopes[i] = scopeList[j]
		i = i + 1
	}
	return scopes
}
func PresentAuthScreen(w http.ResponseWriter, r *http.Request, flow int) {
	authScreenStruct := struct {
		ScopeList []string
		Flow      int
	}{
		ScopeList: getRandomUniqueScopes(3),
		Flow:      flow,
	}
	tmpl, err := template.ParseFiles(
		"public/templates/authScreen.html",
		"public/templates/nav.html",
		"public/templates/footer.html",
	)
	if err != nil {
		log.Fatal(err)
	}
	err = tmpl.ExecuteTemplate(w, "auth", authScreenStruct)
	if err != nil {
		log.Fatal(err)
	}
}
func ShowError(w http.ResponseWriter, r *http.Request, status int, title string, desc string) {
	tmpl, err := template.ParseFiles(
		"public/templates/error.html",
		"public/templates/nav.html",
		"public/templates/footer.html",
	)
	if err != nil {
		log.Fatal(err)
	}
	w.WriteHeader(status)
	err = tmpl.ExecuteTemplate(w, "error", struct {
		Title string
		Desc  string
	}{Title: title, Desc: desc})
	if err != nil {
		log.Fatal(err)
	}
}
type RequestError struct {
	Error string `json:"error"`
	Desc  string `json:"error_description"`
}
func ShowJSONError(w http.ResponseWriter, r *http.Request, status int, data interface{}) {
	body, err := json.Marshal(data)
	if err != nil {
		w.WriteHeader(500)
		fmt.Fprintln(w, "An error occurred while processing your request.")
		return
	}
	w.Header().Set("Content-Type", "application/json;charset=UTF-8")
	w.WriteHeader(status)
	fmt.Fprintf(w, string(body))
}
func RenderTemplate(w http.ResponseWriter, r *http.Request, templateName string, status int, data interface{}) {
	template, err := template.ParseFiles(fmt.Sprintf("public/templates/%s.html", templateName))
	if err != nil {
		panic(err)
	}
	w.WriteHeader(status)
	template.Execute(w, data)
}
func ParseParams(str string) (map[string]string, error) {
	str, err := url.QueryUnescape(str)
	if err != nil {
		return nil, err
	}
	if strings.Contains(str, "?") {
		str = strings.Split(str, "?")[1]
	}
	if !strings.Contains(str, "=") {
		return nil, fmt.Errorf("\"%s\" contains no key-value pairs", str)
	}
	pairs := make(map[string]string)
	for _, pair := range strings.Split(string(str), "&") {
		items := strings.Split(pair, "=")
		pairs[items[0]] = items[1]
	}
	return pairs, nil
}
func ParseBasicAuthHeader(header string) (string, string) {
	header = strings.TrimSpace(header)
	if strings.HasPrefix(header, "Basic ") {
		header = strings.Split(header, " ")[1]
	}
	bytes, err := base64.StdEncoding.DecodeString(header)
	if err != nil {
		log.Println(err)
		return "", ""
	}
	str := string(bytes)
	pair := strings.Split(str, ":")
	if len(pair) != 2 {
		return "", ""
	}
	return pair[0], pair[1]
}
func Clearln() {
	fmt.Print("\r \r")
}