package arkid

import (
	"encoding/json"
	"errors"
	"fmt"

	"applet/app/cfg"
	"applet/app/utils"
	"applet/app/utils/cache"
	"applet/app/utils/logx"
)

func arkidLogin(args map[string]interface{}) ([]byte, error) {
	url := cfg.ArkID.Url + "/siteapi/v1/ucenter/login/"
	b, err := json.Marshal(args)
	if err != nil {
		return nil, logx.Error(err)
	}
	var d []byte
	d, err = utils.CurlPost(url, b, nil)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidLogout(token string) ([]byte, error) {
	// fmt.Println(cfg.ArkID.Url)
	url := cfg.ArkID.Url + "/siteapi/v1/revoke/token/"
	h := map[string]string{"authorization": fmt.Sprintf("token %s", token)}
	d, err := utils.CurlPost(url, "", h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidUserInfo(token string) ([]byte, error) {
	url := cfg.ArkID.Url + "/siteapi/v1/auth/token/"
	h := map[string]string{"authorization": fmt.Sprintf("token %s", token)}
	d, err := utils.CurlGet(url, h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidRegister(args map[string]interface{}) ([]byte, error) {
	url := cfg.ArkID.Url + "/siteapi/oneid/user/"
	b, err := json.Marshal(args)
	if err != nil {
		return nil, logx.Error(err)
	}
	admin, err := getArkIDAdmin()
	if err != nil {
		return nil, logx.Error(err)
	}
	h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
	var d []byte
	d, err = utils.CurlPost(url, b, h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidAppAccessWhiteList(args map[string]interface{}, permName string) ([]byte, error) {
	if permName == "" {
		return nil, errors.New("The perm_name arg must required")
	}
	path := fmt.Sprintf("/siteapi/oneid/perm/%s/owner/", permName)
	url := cfg.ArkID.Url + path
	b, err := json.Marshal(args)
	if err != nil {
		return nil, logx.Error(err)
	}
	admin, err := getArkIDAdmin()
	if err != nil {
		return nil, logx.Error(err)
	}
	// fmt.Println(admin.Token)
	h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
	var d []byte
	d, err = utils.CurlPatch(url, b, h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidUserDelete(username string) ([]byte, error) {
	if username == "" {
		return nil, errors.New("The username arg must required")
	}
	path := fmt.Sprintf("/siteapi/oneid/user/%s/", username)
	url := cfg.ArkID.Url + path
	admin, err := getArkIDAdmin()
	if err != nil {
		return nil, logx.Error(err)
	}
	// fmt.Println(admin.Token)
	h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
	var d []byte
	d, err = utils.CurlDelete(url, nil, h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func arkidUserUpdate(username string, args map[string]interface{}) ([]byte, error) {
	if username == "" {
		return nil, errors.New("The username arg must required")
	}
	b, err := json.Marshal(args)
	if err != nil {
		return nil, logx.Error(err)
	}
	path := fmt.Sprintf("/siteapi/oneid/user/%s/", username)
	url := cfg.ArkID.Url + path
	var admin *ArkIDUser
	admin, err = getArkIDAdmin()
	if err != nil {
		return nil, logx.Error(err)
	}
	h := map[string]string{"authorization": fmt.Sprintf("token %s", admin.Token)}
	d, err := utils.CurlPatch(url, b, h)
	if err != nil {
		return nil, logx.Error(err)
	}
	return d, nil
}

func getArkIDAdmin() (*ArkIDUser, error) {
	c, err := cache.Bytes(cache.Get(ARKID_ADMIN_TOKEN))
	if err != nil {
		logx.Error(err)
	}
	if c != nil && err == nil {
		admin := new(ArkIDUser)
		if err = json.Unmarshal(c, admin); err != nil {
			return admin, err
		}
		return admin, nil
	}
	return Init()
}
