package types

import (
	log "github.com/33cn/chain33/common/log/log15"
	"github.com/33cn/chain33/types"
)

/*
 * 交易相关类型定义
 * 交易action通常有对应的log结构，用于交易回执日志记录
 * 每一种action和log需要用id数值和name名称加以区分
 */

// action类型id和name，这些常量可以自定义修改
const (
	TyUnknowAction = iota + 100
	TyInitAction
	TyRegisterExectorAction
	TyAuditAction
	TyUpdateIndexAction
	TyEmitInterchainEventAction

	NameInitAction                = "Init"
	NameRegisterExectorAction     = "RegisterExector"
	NameAuditAction               = "Audit"
	NameUpdateIndexAction         = "UpdateIndex"
	NameEmitInterchainEventAction = "EmitInterchainEvent"
)

// 请求类型
const (
	Req_Type_Storage = iota
	Req_Type_Coins
	Req_Type_Token
)

//元数据类型
const (
	Meta_Inner = iota
	Meta_Outter
	Meta_RollBack
)

//元数据类型
const (
	Req_Inner = iota
	Req_Callback
	Req_DstRollback
)

// 跨链事件状态
const (
	Pending = iota
	Success
	Fail
)

//查询方向
const (
	ListDESC = int32(0)
	ListASC  = int32(1)
	ListSeek = int32(2)
)

const (
	//Count 单次list还回条数
	PageSize    = int32(10)
	MaxPageSize = 100
)

// log类型id值
const (
	TyUnknownLog = iota + 100
	TyInitLog
	TyRegisterExectorLog
	TyAuditLog
	TyUpdateIndexLog
	TyEmitInterchainEventLog
)

var (
	//BrokerX 执行器名称定义
	BrokerX = "broker"
	//定义actionMap
	actionMap = map[string]int32{
		NameInitAction:                TyInitAction,
		NameRegisterExectorAction:     TyRegisterExectorAction,
		NameAuditAction:               TyAuditAction,
		NameUpdateIndexAction:         TyUpdateIndexAction,
		NameEmitInterchainEventAction: TyEmitInterchainEventAction,
	}
	//定义log的id和具体log类型及名称，填入具体自定义log类型
	logMap = map[int64]*types.LogInfo{
		//LogID:	{Ty: reflect.TypeOf(LogStruct), Name: LogName},
	}
	tlog = log.New("module", "broker.types")
)

// init defines a register function
func init() {
	types.AllowUserExec = append(types.AllowUserExec, []byte(BrokerX))
	//注册合约启用高度
	types.RegFork(BrokerX, InitFork)
	types.RegExec(BrokerX, InitExecutor)
}

// InitFork defines register fork
func InitFork(cfg *types.Chain33Config) {
	cfg.RegisterDappFork(BrokerX, "Enable", 0)
}

// InitExecutor defines register executor
func InitExecutor(cfg *types.Chain33Config) {
	types.RegistorExecutor(BrokerX, NewType(cfg))
}

type brokerType struct {
	types.ExecTypeBase
}

func NewType(cfg *types.Chain33Config) *brokerType {
	c := &brokerType{}
	c.SetChild(c)
	c.SetConfig(cfg)
	return c
}

// GetPayload 获取合约action结构
func (b *brokerType) GetPayload() types.Message {
	return &BrokerAction{}
}

// GeTypeMap 获取合约action的id和name信息
func (b *brokerType) GetTypeMap() map[string]int32 {
	return actionMap
}

// GetLogMap 获取合约log相关信息
func (b *brokerType) GetLogMap() map[int64]*types.LogInfo {
	return logMap
}
