/**
= Creative Commons Lizenzvertrag =
Diese Software ist von der archium GmbH, Gera ist lizenziert unter einer Creative Commons Namensnennung - Nicht kommerziell - Keine Bearbeitungen 4.0 International Lizenz. (http://creativecommons.org/licenses/by-nc-nd/4.0/deed.de)
Individuelle über diese Lizenz hinausgehende Berechtigungen können Sie unter https://archium.org erhalten.

= Creative Commons License =
Software by archium GmbH, Gera is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. (http://creativecommons.org/licenses/by-nc-nd/4.0/)
Individual permissions beyond the scope of this license may be available at https://archium.org.
**/

package user

import (
	"errors"
	"fmt"
	"strings"

	// "log"
	// "reflect"

	"Toolbox/goDataverse/connect"
	tol "Toolbox/goDataverse/tools"

	gjs "github.com/tidwall/gjson"
	//	"log"
)

func ListUsers(_serverurl, _apitoken string, _sorted bool) (string, error) {
	response, err := connect.GetRequest(_serverurl+"/api/admin/list-users"+
		func(s bool) string {
			if s {
				return "?sortKey=createdtime"
			} else {
				return ""
			}
		}(_sorted), tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	return (fmt.Sprintf("%s", response)), err
}

func ListUser(_serverurl, _apitoken, _id string) (string, error) {
	response, err := connect.GetRequest(_serverurl+"/api/admin/authenticatedUsers/"+_id,
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	return (fmt.Sprintf("%s", response)), err
}

func IsValidUser(_serverurl, _apitoken string) (bool, bool, string, error) {
	var ident string = ""
	var super bool = false
	var valid bool = false
	//
	response, err := connect.GetRequest(_serverurl+"/api/users/:me",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	message := tol.GetObjectFromStr("")
	me := fmt.Sprintf("%v", string(response))
	status := gjs.Get(me, "status")
	if status.String() == "OK" {
		data := gjs.Get(me, "data")
		if data.IsObject() {
			ident = strings.ReplaceAll(gjs.Get(data.String(), "identifier").String(), "@", "")
			super = gjs.Get(data.String(), "superuser").Bool()
			valid = true
		}
	} else {
		if status.String() == "ERROR" {
			message = gjs.Get(me, "message")
			err = errors.New(message.String())
		}
	}
	// log.Println("IsValidUser:", message.String(), valid, super, ident, me)
	//
	return valid, super, ident, err
}

func IsSuperUser(_serverurl, _apitoken string) (bool, error) {
	var super bool = false
	//
	response, err := connect.GetRequest(_serverurl+"/api/users/:me",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	me := fmt.Sprintf("%s", response)
	status := gjs.Get(me, "status")
	if status.String() == "OK" {
		data := gjs.Get(me, "data")
		if data.IsObject() {
			super = gjs.Get("superuser", data.String()).Bool()
		}
	} else {
		if status.String() == "ERROR" {
			message := gjs.Get(me, "message")
			err = errors.New(message.String())
		}
	}
	//
	return super, err
}

func ToggleSuperUser(_serverurl, _apitoken, _id string) (string, error) {
	response, err := connect.PostRequest(_serverurl+"/api/admin/superuser/"+_id+"",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	return (fmt.Sprintf("%s", response)), err
}

func SearchData(_serverurl, _apitoken string, _sorted bool) (string, error) {
	response, err := connect.GetRequest(_serverurl+"/api/search?q=*"+
		func(s bool) string {
			if s {
				return "?sortKey=createdtime"
			} else {
				return ""
			}
		}(_sorted), tol.ColMap{}, tol.ColMap{"X-Dataverse-key": _apitoken})
	//
	return (fmt.Sprintf("%s", response)), err
	//
	// curl -H "X-Dataverse-key:$API_TOKEN" $SERVER_URL/api/admin/list-users
	// # sort it by createdtime (the creation time of the account)
	// # curl -H "X-Dataverse-key:$API_TOKEN" "$SERVER_URL/api/admin/list-users?sortKey=createdtime"
}

/*
func CreateAuthenticatedUser(server_url, api_token, params string) string {
	response, err := connect.PostRequestB(server_url+"/api/admin/authenticatedUsers", []byte(params), tol.ColMap{"X-Dataverse-key": api_token})

	if       err != nil {
		log.Fatal(err)
	}

	return (fmt.Sprintf("%s", response))
}
*/
