package api

import (
	"net/http"
	"net/url"
	"testing"
	"time"

	. "github.com/pingcap/check"
	"github.com/pingcap/schrodinger/pkg/types"
	"github.com/pingcap/schrodinger/util"
)

var (
	r *http.Request
)

func TestClient(t *testing.T) {
	r = &http.Request{
		URL: &url.URL{
			RawQuery: "size=2",
		},
	}
	TestingT(t)
}

var _ = Suite(&testUtilSuite{})

type testUtilSuite struct{}

func (t *testUtilSuite) TestFilterExperimentByTime(c *C) {
	now := time.Now()
	experiment1 := &types.ExperimentProcess{StartTime: now.Add(-24 * time.Hour).Format(util.TimeFormat)}
	experiment2 := &types.ExperimentProcess{}
	experiment3 := &types.ExperimentProcess{StartTime: now.Add(-48 * time.Hour).Format(util.TimeFormat), EndTime: now.Add(-24 * time.Hour).Format(util.TimeFormat)}
	experiment4 := &types.ExperimentProcess{StartTime: now.Add(-72 * time.Hour).Format(util.TimeFormat), EndTime: now.Add(-48 * time.Hour).Format(util.TimeFormat)}

	experiments := types.ExperimentProcesses{experiment1, experiment2, experiment3, experiment4}
	filterExperiments1, err := filterExperimentByTime(experiments, now.Add(-36*time.Hour).Format(util.TimeFormat), now.Format(util.TimeFormat))
	c.Assert(err, IsNil)
	c.Assert(filterExperiments1, DeepEquals, types.ExperimentProcesses{experiment1, experiment3})

	filterExperiments2, err := filterExperimentByTime(experiments, now.Add(-52*time.Hour).Format(util.TimeFormat), now.Add(-12*time.Hour).Format(util.TimeFormat))
	c.Assert(err, IsNil)
	c.Assert(filterExperiments2, DeepEquals, types.ExperimentProcesses{experiment1, experiment3, experiment4})

	filterExperiments3, err := filterExperimentByTime(experiments, now.Add(-12*time.Hour).Format(util.TimeFormat), now.Add(10*time.Hour).Format(util.TimeFormat))
	c.Assert(err, IsNil)
	c.Assert(filterExperiments3, DeepEquals, types.ExperimentProcesses{experiment1})
}

func (t *testUtilSuite) TestFilterTemplate(c *C) {
	template1 := &types.TestTemplate{ID: 1, Status: types.TestTemplateDone, Type: types.GeneralTest}
	template2 := &types.TestTemplate{ID: 2, Status: types.TestTemplateDone, Type: types.PrepareTest}
	template3 := &types.TestTemplate{ID: 3, Status: types.TestTemplateDone, Type: types.PrepareTest, Tags: []string{"ddl"}}
	template4 := &types.TestTemplate{ID: 4, Name: "bank", Status: types.TestTemplateDone, Type: types.PrepareTest, Tags: []string{"ddl"}}
	template5 := &types.TestTemplate{ID: 5, Status: types.TestTemplateBuilding, Type: types.GeneralTest}

	templates := types.TestTemplates{template1, template2, template3, template4, template5}
	filterTemplates1 := filterTestTemplate(templates, []string{}, []string{}, []string{}, []string{})
	c.Assert(filterTemplates1, DeepEquals, templates)

	filterTemplates2 := filterTestTemplate(templates, []string{string(types.TestTemplateDone)}, []string{}, []string{}, []string{})
	c.Assert(filterTemplates2, DeepEquals, types.TestTemplates{template1, template2, template3, template4})

	filterTemplates3 := filterTestTemplate(templates, []string{string(types.TestTemplateDone)}, []string{}, []string{"ddl"}, []string{})
	c.Assert(filterTemplates3, DeepEquals, types.TestTemplates{template3, template4})

	filterTemplates4 := filterTestTemplate(templates, []string{string(types.TestTemplateDone)}, []string{}, []string{"ddl"}, []string{"ba"})
	c.Assert(filterTemplates4, DeepEquals, types.TestTemplates{template4})
}

func (t *testUtilSuite) TestGetSize(c *C) {
	r.URL.RawQuery = "size=2"
	size := getSize(r)
	c.Assert(size, Equals, int64(2))

	offset := getOffset(r)
	c.Assert(offset, Equals, int64(0))

	r.URL.RawQuery = "size=2&offset=3"
	offset = getOffset(r)
	c.Assert(offset, Equals, int64(3))
}

func (t *testUtilSuite) TestGetPagingIndexes(c *C) {
	start, end := getPagingIndexes(5, 2, 7)
	c.Assert(start, Equals, int64(2))
	c.Assert(end, Equals, int64(5))

	start, end = getPagingIndexes(5, 2, 1)
	c.Assert(start, Equals, int64(2))
	c.Assert(end, Equals, int64(3))
}

func (t *testUtilSuite) TestGetLabelFilter(c *C) {
	r.URL.RawQuery = "filter=usage&usage=debug"
	f := getLabelFilter(r)
	c.Assert(f, Equals, "usage=debug")

	r.URL.RawQuery = "filter=usage&usae=debug"
	f = getLabelFilter(r)
	c.Assert(f, Equals, "")

	r.URL.RawQuery = "filter=usge&usae=debug"
	f = getLabelFilter(r)
	c.Assert(f, Equals, "")
}
