package steps

import (
	"github.com/pingcap/errors"
	"github.com/pingcap/test-infra/caselib/pkg/host"
	"github.com/pingcap/test-infra/sdk/resource"
)

type board struct {
	tc            resource.TiDBCluster
	sink          resource.CDCSink
	sink2         resource.CDCSink
	workload      resource.WorkloadNode
	tiup          resource.WorkloadNode
	LightningHost *host.LightningHost
	kafka         resource.KafkaCluster

	value        interface{}
	workloadDone chan struct{}
}

func NewBoard() *board {
	return &board{
		workloadDone: make(chan struct{}, 1),
	}
}

func (b *board) TiDBCluster() (resource.TiDBCluster, error) {
	if b.tc == nil {
		return nil, errors.New("tidb cluster does not exists in board")
	}

	return b.tc, nil
}

func (b *board) Sink() (resource.CDCSink, error) {
	if b.sink == nil {
		return nil, errors.New("sink does not exists in board")
	}

	return b.sink, nil
}

func (b *board) Kafka() (resource.KafkaCluster, error) {
	if b.kafka == nil {
		return nil, errors.New("kafka sink does not exists in board, make sure run `initWorkload` step before")
	}

	return b.kafka, nil
}

func (b *board) SetTiDBCluster(tc resource.TiDBCluster) error {
	if b.tc != nil {
		return errors.New("tidb cluster already exists in board")
	}

	b.tc = tc
	return nil
}

func (b *board) RemoveTiDBCluster(){
	b.tc = nil
	return
}

func (b *board) SetSink(sink resource.CDCSink) error {
	if b.sink != nil {
		return errors.New("sink already exists in board")
	}

	b.sink = sink
	return nil
}

func (b *board) RemoveSink(){
	b.sink = nil
	return
}

func (b *board) Workload() (resource.WorkloadNode, error) {
	if b.workload == nil {
		return nil, errors.New("workload does not exist, make sure run `initWorkload` step before")
	}

	return b.workload, nil
}

func (b *board) SetWorkload(node resource.WorkloadNode) error {
	if b.workload != nil {
		return errors.New("workload already exists in board")
	}

	b.workload = node
	return nil
}

func (b *board) Tiup() (resource.WorkloadNode, error) {
	if b.tiup == nil {
		return nil, errors.New("tiup does not exist, make sure run `initTiup` step before")
	}

	return b.tiup, nil
}

func (b *board) SetTiup(node resource.WorkloadNode) error {
	if b.tiup != nil {
		return errors.New("tiup already exists in board")
	}

	b.tiup = node
	return nil
}

func (b *board) SetKafka(kafka resource.KafkaCluster) error {
	if b.kafka != nil {
		return errors.New("kafka already exists in board")
	}

	b.kafka = kafka
	return nil
}
