package executor

import (
	log "github.com/33cn/chain33/common/log/log15"
	drivers "github.com/33cn/chain33/system/dapp"
	"github.com/33cn/chain33/types"
	brokertypes "github.com/33cn/plugin/plugin/dapp/broker/types"
)

/*
 * 执行器相关定义
 * 重载基类相关接口
 */

var (
	//日志
	elog = log.New("module", "broker.executor")
)

var driverName = brokertypes.BrokerX

// Init register dapp
func Init(name string, cfg *types.Chain33Config, sub []byte) {
	drivers.Register(cfg, GetName(), newBroker, cfg.GetDappFork(driverName, "Enable"))
	//TODO  初始化时，需要把应用链的ID加载进去
	InitExecType()
}

// InitExecType Init Exec Type
func InitExecType() {
	ety := types.LoadExecutorType(driverName)
	ety.InitFuncList(types.ListMethod(&broker{}))
}

type broker struct {
	drivers.DriverBase
}

func newBroker() drivers.Driver {
	t := &broker{}
	t.SetChild(t)
	t.SetExecutorType(types.LoadExecutorType(driverName))
	return t
}

// GetName get driver name
func GetName() string {
	return newBroker().GetName()
}

func (b *broker) GetDriverName() string {
	return driverName
}

// CheckTx 实现自定义检验交易接口，供框架调用
func (b *broker) CheckTx(tx *types.Transaction, index int) error {
	// implement code
	return nil
}
