package utils

import (
	"fmt"
	"sync"
	"testing"

	"github.com/badu/bus"
)

func Test_event(b *testing.T) {

	topic := bus.NewTopic[*Task]() //直接注册到用户
	for i := 0; i < 1; i++ {
		isdone := false
		t := topic.Sub(func(v *Task) { //用户初始化时注册里程碑
			if v.done { //如果任务完成
				isdone = true
				fmt.Println(v.id, "任务完成")
				return
			}

			if v.cfunc != nil {
				v.cont = v.cfunc(v.cont) //增加计数
			}

			if v.cont > 5 { //如果计数达到,标记任务完成
				v.done = true
			}
			fmt.Println(v.id, "当前任务计数", v.cont, "是否完成", v.done)

		})
		if isdone { //如果任务完成,取消注册
			t.Cancel()
		}

	}
	task1 := &Task{id: 1, cfunc: func(t uint32) uint32 { //满足任务1后推定

		//fmt.Println(1, "当前任务计数", t)
		//实现自定义逻辑
		//增加用户计数
		//增加服务器总计数
		return t + 1

	}}
	topic.Pub(task1)
	topic.Pub(task1)
	topic.Pub(&Task{id: 2, done: true})

	topic.Pub(task1) //发送事件
	task1.Complete()

	fmt.Println("当前任务状态", task1.IsDone())
}

type TaskTree struct {
	AllTasks []*Task //任务集合
	mu       sync.Mutex
}

// Various event types
const EventA = 0x01

type Task struct {
	id    uint32
	cfunc func(uint32) uint32
	cont  uint32
	done  bool
}

func (e *Task) IsDone() bool {
	return e.done
}
func (e *Task) Complete() {
	e.done = true
}
func (e *Task) EventID() string {
	return "YourInterestingEventName"
}

// Event type for testing purposes
type Event struct {
	Data  string
	type1 uint32
}

// Type returns the event type
func (ev Event) Type() uint32 {
	return ev.type1
}
