package object

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"testing"
	"time"

	"github.com/antgroup/hugescm/modules/plumbing"
	"github.com/antgroup/hugescm/modules/streamio"
	"github.com/emirpasic/gods/trees/binaryheap"
)

func TestCommitCompress(t *testing.T) {
	s := Signature{
		Name:  "Linus Dev",
		Email: "linux@dev.io",
		When:  time.Now(),
	}
	cc := &Commit{
		Author:    s,
		Committer: s,
		Tree:      plumbing.NewHash("04ca0feb68cb19158e0078227a989798600a0701b8d729f2edb09b5dcdbc79ac"),
		Message: `To list information about all objects in a bucket, you must have the oss:ListObjects permission.
The user metadata of objects is not returned for ListObjectsV2 (GetBucketV2) requests.
If you have enabled Logging and Real-time log query, the operation field in the access logs generated by calling the ListObjects (GetBucket) operation is GetBucket.
You are charged based on the number of PUT requests when you call the ListObjectsV2 (GetBucketV2) operation. For more information, see PUT requests.`,
	}
	var b bytes.Buffer
	_ = cc.Encode(&b)
	var zb bytes.Buffer
	zw := streamio.GetZstdWriter(&zb)
	defer streamio.PutZstdWriter(zw)
	n, _ := io.Copy(zw, bytes.NewReader(b.Bytes()))
	_ = zw.Close()
	fmt.Fprintf(os.Stderr, "%d --> %d | %d\n", b.Len(), zb.Len(), n)
	zr, err := streamio.GetZstdReader(bytes.NewReader(zb.Bytes()))
	if err != nil {
		return
	}
	defer streamio.PutZstdReader(zr)
	var zbb bytes.Buffer
	K, err := io.Copy(&zbb, zr)
	if err != nil {
		return
	}
	fmt.Fprintf(os.Stderr, "%d, %d\n", zbb.Len(), K)
}

func TestCommitDecode(t *testing.T) {
	s := Signature{
		Name:  "Linus Dev",
		Email: "linux@dev.io",
		When:  time.Now(),
	}
	cc := &Commit{
		Author:    s,
		Committer: s,
		Tree:      plumbing.NewHash("04ca0feb68cb19158e0078227a989798600a0701b8d729f2edb09b5dcdbc79ac"),
		Message: `To list information about all objects in a bucket, you must have the oss:ListObjects permission.
The user metadata of objects is not returned for ListObjectsV2 (GetBucketV2) requests.
If you have enabled Logging and Real-time log query, the operation field in the access logs generated by calling the ListObjects (GetBucket) operation is GetBucket.
You are charged based on the number of PUT requests when you call the ListObjectsV2 (GetBucketV2) operation. For more information, see PUT requests.`,
	}
	var b bytes.Buffer
	h := plumbing.NewHasher()
	_ = cc.Encode(io.MultiWriter(&b, h))
	oid := h.Sum()
	var zb bytes.Buffer
	zw := streamio.GetZstdWriter(&zb)
	defer streamio.PutZstdWriter(zw)
	_, _ = io.Copy(zw, bytes.NewReader(b.Bytes()))
	_ = zw.Close()
	a, err := Decode(bytes.NewReader(zb.Bytes()), oid, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %v\n", err)
		return
	}
	fmt.Fprintf(os.Stderr, "%v\n", a)
}

func TestSignature(t *testing.T) {
	s := "ZETA <zeta@alipay.com> 1706262944 +0800"
	var signature Signature
	signature.Decode([]byte(s))
	fmt.Fprintf(os.Stderr, "%v\n", signature)
}

func TestBinaryHeap(t *testing.T) {
	numbers := []int{1, 2, 3, 5, 6, 128, 8, 4, 7}
	p := binaryheap.NewWithIntComparator()
	for _, i := range numbers {
		p.Push(i)
	}
	for {
		v, ok := p.Pop()
		if !ok {
			break
		}
		fmt.Fprintf(os.Stderr, "%v\n", v)
	}
}
