package gemini_test

import (
	"bytes"
	"context"
	"crypto/tls"
	"fmt"
	"io"
	"net/url"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	sr "tildegit.org/tjp/sliderule"
	"tildegit.org/tjp/sliderule/gemini"
)

func TestRoundTrip(t *testing.T) {
	tlsConf, err := gemini.FileTLS("./testdata/server.crt", "./testdata/server.key")
	require.Nil(t, err)

	handler := sr.HandlerFunc(func(ctx context.Context, req *sr.Request) *sr.Response {
		return gemini.Success("text/gemini", bytes.NewBufferString("you've found my page"))
	})

	server, err := gemini.NewServer(context.Background(), "localhost", "tcp", "127.0.0.1:0", handler, nil, tlsConf)
	require.Nil(t, err)

	go func() {
		_ = server.Serve()
	}()
	defer server.Close()

	u, err := url.Parse(fmt.Sprintf("gemini://%s/test", server.Address()))
	require.Nil(t, err)

	cli := gemini.NewClient(testClientTLS())
	response, err := cli.RoundTrip(&sr.Request{URL: u})
	require.Nil(t, err)

	assert.Equal(t, gemini.StatusSuccess, response.Status)
	assert.Equal(t, "text/gemini", response.Meta)

	require.NotNil(t, response.Body)
	body, err := io.ReadAll(response.Body)
	require.Nil(t, err)

	assert.Equal(t, "you've found my page", string(body))
}

func TestTitanRequest(t *testing.T) {
	tlsConf, err := gemini.FileTLS("./testdata/server.crt", "./testdata/server.key")
	require.Nil(t, err)

	invoked := false
	handler := sr.HandlerFunc(func(ctx context.Context, request *sr.Request) *sr.Response {
		invoked = true

		body := gemini.GetTitanRequestBody(request)
		if !assert.NotNil(t, body) {
			return gemini.Success("", nil)
		}

		bodyBytes, err := io.ReadAll(body)
		require.Nil(t, err)

		assert.Equal(t, "the request body\n", string(bodyBytes))
		return gemini.Success("", nil)
	})

	server, err := gemini.NewServer(context.Background(), "localhost", "tcp", "127.0.0.1:0", handler, nil, tlsConf)
	require.Nil(t, err)

	go func() {
		_ = server.Serve()
	}()
	defer server.Close()

	conn, err := tls.Dial(server.Network(), server.Address(), testClientTLS())
	require.Nil(t, err)

	_, err = fmt.Fprintf(
		conn,
		"titan://%s/foobar;size=17;mime=text/plain\r\nthe request body\n",
		server.Address(),
	)
	require.Nil(t, err)

	_, err = io.ReadAll(conn)
	require.Nil(t, err)

	assert.True(t, invoked)
}

func testClientTLS() *tls.Config {
	return &tls.Config{InsecureSkipVerify: true}
}