package test

import (
	"context"
	"encoding/json"
	"fmt"
	"testing"

	"github.com/cockroachdb/errors"

	"github.com/larksuite/oapi-sdk-go/api"
	"github.com/larksuite/oapi-sdk-go/api/core/response"
	"github.com/larksuite/oapi-sdk-go/core/tools"

	"github.com/larksuite/oapi-sdk-go/api/core/request"

	"github.com/larksuite/oapi-sdk-go/core"
)

func TestLarkNaiveApi(t *testing.T) {
	helperInit()

	coreCtx := core.WrapContext(context.Background())
	params := map[string]interface{}{
		"emails": "chenjianlin@pingcap.com",
	}
	ret := make(map[string]interface{})
	req := request.NewRequestWithNative("/open-apis/user/v1/batch_get_id", "GET",
		request.AccessTokenTypeTenant, nil, &ret, request.SetQueryParams(params))

	err := api.Send(coreCtx, getLarkConfig(), req)
	fmt.Println(coreCtx.GetRequestID())
	fmt.Println(coreCtx.GetHTTPStatusCode())
	if err != nil {
		fmt.Println(tools.Prettify(err))
		e := &response.Error{}
		if errors.As(err, e) {
			fmt.Println(e.Code)
			fmt.Println(e.Msg)
		}
		return
	}
	fmt.Println(tools.Prettify(ret))
}

func TestLarkSendMessage(t *testing.T) {
	helperInit()
	coreCtx := core.WrapContext(context.Background())

	cardContent := "{\"config\":{\"wide_screen_mode\":false},\"card_link\":{\"url\":\"http://34.219.103.43:8080/api/redirect?url=https://internal.pingcap.net/jira/browse/ONCALL-3394\"},\"header\":{\"title\":{\"tag\":\"plain_text\",\"content\":\"OnCall Issue ID: 3394**\"},\"template\":\"orange\"},\"i18n_elements\":{\"zh_cn\":[{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"This issue has not been updated for 10 hours. Is there any update?\"}},{\"tag\":\"hr\"},{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"**Issue details**\"}},{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"\"},\"fields\":[{\"is_short\":false,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Summary:**\\n贝壳金服-v3.0.16 升级失败--pd 节点时间不一致导致无法切换leader\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Status:**\\nPROBLEM LOCATED\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Priority:**\\nP1\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Component:**\\nPD\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Affect Version:**\\nv3.0.16\"}}]},{\"tag\":\"hr\"},{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"**People & Dates**\"}},{\"tag\":\"div\",\"text\":{\"tag\":\"lark_md\",\"content\":\"\"},\"fields\":[{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Reporter:**\\nLiu Peiliang\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Assignee:**\\nHuangMenglong\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Created:**\\n2021-07-12 23:32:23\"}},{\"is_short\":true,\"text\":{\"tag\":\"lark_md\",\"content\":\"**Updated:**\\n2021-07-22 23:32:23\"}}]}]}}"
	card := map[string]interface{}{}
	err := json.Unmarshal([]byte(cardContent), &card)
	if err != nil {
		panic(err)
	}
	body := map[string]interface{}{
		"open_id":  "ou_cb5e242cf06886e8a7dd5b67b7780ede",
		"msg_type": "interactive",
		"card":     card,
	}
	ret := make(map[string]interface{})
	req := request.NewRequestWithNative("/open-apis/message/v4/send", "POST",
		request.AccessTokenTypeTenant, body, &ret,
	)
	err = api.Send(coreCtx, getLarkConfig(), req)
	fmt.Println(coreCtx.GetRequestID())
	fmt.Println(coreCtx.GetHTTPStatusCode())
	if err != nil {
		fmt.Println(tools.Prettify(err))
		e := &response.Error{}
		if errors.As(err, e) {
			fmt.Println(e.Code)
			fmt.Println(e.Msg)
		}
		return
	}
	fmt.Println(tools.Prettify(ret))
}
