package api

import (
	"sync"
	"testing"

	"github.com/Ptt-official-app/go-pttbbs/bbs"
	"github.com/Ptt-official-app/go-pttbbs/ptt"
	"github.com/Ptt-official-app/go-pttbbs/testutil"
)

func TestEditArticle(t *testing.T) {
	setupTest(t.Name())
	defer teardownTest(t.Name())

	_ = ptt.SetupNewUser(testNewPostUserRaw1)

	class0 := []byte("test")
	title0 := []byte("this is a test")
	fullTitle0 := []byte("[test] this is a test")
	postContent0 := [][]byte{[]byte("test1"), []byte("test2")}
	ip0 := "127.0.0.1"

	gotSummary, _ := bbs.CreateArticle("A1", "10_WhoAmI", class0, title0, postContent0, ip0)

	gotContent0, _, oldSum0, _ := bbs.GetArticle("A1", "10_WhoAmI", gotSummary.ArticleID, 0, true)

	editContent0 := [][]byte{
		{
			0xa7, 0x40, 0xaa, 0xcc, 0x3a, 0x20, 'A', '1', ' ', // 作者: A1
			0x28, 0xaf, 0xab, //(神
			0x29, 0x20, 0xac, 0xdd, 0xaa, 0x4f, //) 看板
			0x3a, 0x20, 0x57, 0x68, 0x6f, 0x41, 0x6d, 0x49, //: WhoAmI
		},
		{
			0xbc, 0xd0, 0xc3, 0x44, 0x3a, 0x20, 0x5b, 0x74, 0x65, 0x73, // 標題: [tes
			0x74, 0x5d, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, // t] this is
			0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, // a test
		},
		{
			0xae, 0xc9, 0xb6, 0xa1, 0x3a, 0x20, // 時間:
		},
		{},
		{
			0x74, 0x65, 0x73, 0x74, 0x38, // test8
		},
		{
			0x74, 0x65, 0x73, 0x74, 0x39, // test9
		},
		{},
		{
			0x2d, 0x2d, //--
		},
		{
			0xa1, 0xb0, 0x20, 0xb5, 0x6f, 0xab, 0x48, 0xaf, 0xb8, 0x3a, //※ 發信站:
			0x20, 0xb7, 0x73, 0xa7, 0xe5, 0xbd, 0xf0, 0xbd, 0xf0, 0x28, // 新批踢踢(
			0x70, 0x74, 0x74, 0x32, 0x2e, 0x63, 0x63, 0x29, 0x2c, 0x20, // ptt2.cc),
			0xa8, 0xd3, 0xa6, 0xdb, 0x3a, 0x20, 0x31, 0x32, 0x37, 0x2e, // 來自: 127.
			0x30, 0x2e, 0x30, 0x2e, 0x31, // 0.0.1
		},
		{
			0xa1, 0xb0, 0x20, 0xa4, 0xe5, 0xb3, 0xb9, 0xba, 0xf4, //※ 文章網
			0xa7, 0x7d, 0x3a, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, // 址: http:/
			0x2f, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // /localhost
			0x2f, 0x62, 0x62, 0x73, 0x2f, 0x57, 0x68, 0x6f, 0x41, 0x6d, 0x49, 0x2f, 0x4d, 0x2e, // /bbs/WhoAmI/M.
		},
	}

	expectedContent0 := []byte{
		0xa7, 0x40, 0xaa, 0xcc, 0x3a, 0x20, 0x41, 0x31, 0x20, // 作者: A1
		0x28, 0xaf, 0xab, 0x29, 0x20, 0xac, 0xdd, 0xaa, 0x4f, 0x3a, 0x20, //(神) 看板:
		0x57, 0x68, 0x6f, 0x41, 0x6d, 0x49, 0x0a, // WhoAmI
		0xbc, 0xd0, 0xc3, 0x44, 0x3a, 0x20, 0x5b, 0x74, 0x65, 0x73, 0x74, 0x5d, 0x20, // 標題: [test]
		0x74, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x0a, // this is a test
		0xae, 0xc9, 0xb6, 0xa1, 0x3a, 0x0a, // 時間:
		0x0a,
		0x74, 0x65, 0x73, 0x74, 0x38, 0x0a, // test8
		0x74, 0x65, 0x73, 0x74, 0x39, 0x0a, // test9
		0x0a,
		0x2d, 0x2d, 0x0a, //--
		0xa1, 0xb0, 0x20, 0xb5, 0x6f, 0xab, 0x48, 0xaf, 0xb8, 0x3a, 0x20, // ※ 發信站:
		0xb7, 0x73, 0xa7, 0xe5, 0xbd, 0xf0, 0xbd, 0xf0, // 新批踢踢
		0x28, 0x70, 0x74, 0x74, 0x32, 0x2e, 0x63, 0x63, 0x29, 0x2c, 0x20, // (ptt2.cc),
		0xa8, 0xd3, 0xa6, 0xdb, 0x3a, 0x20, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x0a, // 來自: 127.0.0.1
		0xa1, 0xb0, 0x20, 0xa4, 0xe5, 0xb3, 0xb9, 0xba, 0xf4, 0xa7, 0x7d, 0x3a, 0x20, // ※ 文章網址:
		0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x68, 0x6f, 0x73, 0x74, // http://localhost
		0x2f, 0x62, 0x62, 0x73, 0x2f, 0x57, 0x68, 0x6f, 0x41, 0x6d, 0x49, 0x2f, 0x4d, 0x2e, 0x0a, // /bbs/WhoAmI/M.
		0xa1, 0xb0, 0x20, 0xbd, 0x73, 0xbf, 0xe8, 0x3a, 0x20, 0x41, 0x31, 0x20, // ※ 編輯: A1
		0x28, 0x31, 0x32, 0x37, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x31, 0x29, 0x2c, 0x20, // (127.0.0.1)
		0x00, 0x00, 0x2f, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x0a, // 00/00/0000 00:00:00
	}

	removeIdxes0 := []int{
		193, 194, 196, 197, 199, 200, 201, 202, 204, 205, 207, 208, 210, 211, // 時間
	}

	params0 := &EditArticleParams{
		Content: editContent0,
		OldSZ:   len(gotContent0),
		OldSum:  oldSum0,
	}

	path0 := &EditArticlePath{
		BBoardID:  "10_WhoAmI",
		ArticleID: gotSummary.ArticleID,
	}

	expected := &EditArticleResult{
		Content:   expectedContent0,
		Class:     class0,
		RealTitle: title0,
		FullTitle: fullTitle0,
	}

	type args struct {
		remoteAddr string
		uuserID    bbs.UUserID
		params     interface{}
		path       interface{}
	}
	tests := []struct {
		name           string
		args           args
		expectedResult *EditArticleResult
		wantErr        bool
	}{
		// TODO: Add test cases.
		{
			args:           args{remoteAddr: "127.0.0.1", uuserID: "A1", params: params0, path: path0},
			expectedResult: expected,
		},
	}
	var wg sync.WaitGroup
	for _, tt := range tests {
		wg.Add(1)
		t.Run(tt.name, func(t *testing.T) {
			defer wg.Done()
			gotResult, err := EditArticle(tt.args.remoteAddr, tt.args.uuserID, tt.args.params, tt.args.path, nil)
			if (err != nil) != tt.wantErr {
				t.Errorf("EditArticle() error = %v, wantErr %v", err, tt.wantErr)
				return
			}

			result, _ := gotResult.(*EditArticleResult)
			for _, each := range removeIdxes0 {
				result.Content[each] = 0x00
			}
			result.MTime = 0
			testutil.TDeepEqual(t, "got", result, tt.expectedResult)
		})
		wg.Wait()
	}
}
