package steps

import (
	"context"
	"sync"
)

type contextKey string

const (
	// ctxKey specifies the context of the testcase.
	ctxKey = contextKey("TESTCASE_CONTEXT")
)

type Context struct {
	mutex sync.RWMutex

	// TODO: We need to design a good event system to construct more complex testcases through communication.
	// Ev EventBus
	board *board

	clusterName string
}

// New create a context instance.
func New(ctx context.Context) context.Context {
	return context.WithValue(ctx, ctxKey, &Context{
		mutex: sync.RWMutex{},
		board: NewBoard(),
	})
}

// GetInner return *Context from context.Context's value.
func GetInner(ctx context.Context) *Context {
	return ctx.Value(ctxKey).(*Context)
}

// GetClusterName returns the current cluster name
func (ctx *Context) GetClusterName() string {
	ctx.mutex.RLock()
	clusterName := ctx.clusterName
	ctx.mutex.RUnlock()

	return clusterName
}

func (ctx *Context) Board() *board {
	return ctx.board
}
