package svc

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"testing"

	"golib/v2/features/mo"
	"golib/v2/gnet"
)

func TestHttpHandler_ServeHTTP(t *testing.T) {
	mux := http.NewServeMux()
	mux.Handle("/svc/", &HttpHandler{Items: svc.Items, User: testUser})
	err := http.ListenAndServe("127.0.0.1:7000", mux)
	if err != nil {
		t.Error(err)
		return
	}
}

const (
	testGlobalItemName = "test.user"
)

func TestHttpHandlerCmdInsertOne(t *testing.T) {
	body := httpHandleBody{
		Data: map[string]interface{}{"name": "TestHttpHandlerCmdInsertOne", "age": 20, "gender": "Male", "phone": "11111111111", "company": mo.A{111, 222}},
	}
	resp, err := testHTTPHandle(cmdInsertOne, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdInsertMany(t *testing.T) {
	body := httpHandleBody{
		Data: []interface{}{
			map[string]interface{}{"name": "TestHttpHandlerCmdInsertMany", "age": 21, "gender": "Male", "phone": "11111111111", "company": mo.A{111, 222}},
			map[string]interface{}{"name": "TestHttpHandlerCmdInsertMany", "age": 21, "gender": "Male", "phone": "11111111111", "company": mo.A{111, 222}},
		},
	}
	resp, err := testHTTPHandle(cmdInsertMany, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdUpdateOne(t *testing.T) {
	body := httpHandleBody{
		Data:    map[string]interface{}{"name": "TestHttpHandlerCmdInsertOne"},
		ExtData: map[string]interface{}{"phone": "updated One"},
	}
	resp, err := testHTTPHandle(cmdUpdateOne, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdUpdateByID(t *testing.T) {
	body := httpHandleBody{
		Data:    "649e1f9ed159267e8f2ffdf5",
		ExtData: map[string]interface{}{"phone": "updated by id"},
	}
	resp, err := testHTTPHandle(cmdUpdateById, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdUpdateMany(t *testing.T) {
	body := httpHandleBody{
		Data:    map[string]interface{}{"name": "TestHttpHandlerCmdInsertMany"},
		ExtData: map[string]interface{}{"phone": "updated Many"},
	}
	resp, err := testHTTPHandle(cmdUpdateMany, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdFindOne(t *testing.T) {
	body := httpHandleBody{
		Data: map[string]interface{}{"name": map[string]interface{}{"$regex": "Test"}, "age": 20},
	}
	resp, err := testHTTPHandle(cmdFindOne, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdFind(t *testing.T) {
	body := httpHandleBody{
		Data: map[string]interface{}{"age": map[string]interface{}{"$gte": 20}},
	}
	resp, err := testHTTPHandle(cmdFind, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	for i, row := range resp.Data.([]interface{}) {
		t.Logf("data[%d]: %v\n", i, row)
	}
}

func TestHttpHandlerCmdDeleteOne(t *testing.T) {
	body := httpHandleBody{
		Data: map[string]interface{}{"name": "TestHttpHandlerCmdInsertOne"},
	}
	resp, err := testHTTPHandle(cmdDeleteOne, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func TestHttpHandlerCmdDeleteMany(t *testing.T) {
	body := httpHandleBody{
		Data: map[string]interface{}{"name": map[string]interface{}{"$regex": "Test"}},
	}
	resp, err := testHTTPHandle(cmdDeleteMany, body)
	if err != nil {
		t.Error(err)
		return
	}
	t.Log("cmd:", resp.CMD)
	t.Log("itemName:", resp.ItemName)
	t.Log("data:", resp.Data)
}

func testHTTPHandle(cmd string, body httpHandleBody) (*httpHandleBody, error) {
	b, err := json.Marshal(body)
	if err != nil {
		return nil, fmt.Errorf("json.Marshal: %s", err)
	}
	uri := fmt.Sprintf("http://127.0.0.1:7000/svc/%s/%s", cmd, testGlobalItemName)
	resp, err := http.Post(uri, gnet.HTTPContentTypeJson, bytes.NewReader(b))
	if err != nil {
		return nil, fmt.Errorf("http.Post: %s", err)
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("http.Post: StatusCode: %d", resp.StatusCode)
	}
	p, err := gnet.HTTP.ReadResponseBody(resp, 0)
	if err != nil {
		return nil, fmt.Errorf("ReadResponseBody: %s", err)
	}
	var respBody httpHandleBody
	if err = json.Unmarshal(p, &respBody); err != nil {
		return nil, fmt.Errorf("json.Unmarshal: %s", err)
	}
	return &respBody, nil
}
