package dtask

import (
	"bytes"
	"encoding/gob"
	"testing"
	"time"

	_ "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/defsch"
	"git.rosy.net.cn/jx-callback/business/model"
	_ "git.rosy.net.cn/jx-callback/business/partner/purchase/jd"
	"git.rosy.net.cn/jx-callback/globals/testinit"
)

func init() {
	testinit.Init()
}

func TestScope(t *testing.T) {
}

func TestIt(t *testing.T) {
	Init(myObjCreator, []*model.Store{})
	var err error
	// taskID, err := CurMan.AddTask("testtask", "xxx")
	// if err != nil {
	// 	t.Fatal(err)
	// }
	// tt := []*model.Store{
	// 	&model.Store{
	// 		Name: "storename",
	// 	},
	// }
	// pp := "asdfsafs"
	// err = CurMan.AddItem(taskID, "0", "DurableTaskTestFunc", 1, "str", map[string]interface{}{
	// 	"key1": "value1",
	// 	"key2": 100,
	// 	"key3": &pp,
	// }, []interface{}{1, "hello"}, tt)
	// err = CurMan.AddItem(taskID, "0", "DurableTaskTestFunc", 1, "str", map[string]interface{}{
	// 	"key1": "value1",
	// 	"key2": 100,
	// 	"key3": &pp,
	// }, []interface{}{1, "hello"}, tt)

	// if err != nil {
	// 	t.Fatal(err)
	// }
	// err = CurMan.StartTask(taskID)
	err = CurMan.LoadPendingTask()
	if err != nil {
		t.Fatal(err)
	}
	err = CurMan.Start()
	if err != nil {
		t.Fatal(err)
	}
	// CurMan.LoadPendingTask()
	// CurMan.Start()
	time.Sleep(3 * time.Second)
}

func TestEncode(t *testing.T) {
	data := []interface{}{
		3,
		"abc",
		float32(3.4),
	}
	var buf bytes.Buffer
	enc := gob.NewEncoder(&buf)
	dec := gob.NewDecoder(&buf)
	enc.Encode(data)
	// t.Log(string(buf.Bytes()))
	var kk []interface{}
	dec.Decode(&kk)
	t.Log(kk)
}

type tTest struct {
}

func (p *tTest) DurableTaskTestFunc(intParam int, strParam string, mapParam map[string]interface{}, sliceParam []interface{}, tt []*model.Store) error {
	time.Sleep(1 * time.Second)
	return nil // errors.New("hello")
}

func myObjCreator(objHint string) interface{} {
	return new(tTest)
}
