package ruleEngine

type RuleChainService interface {
	// FindRuleChainById 根据id查询规则链数据
	FindRuleChainById(tenantId, ruleChainId string) (*RuleChain, error)
	// FindRuleNodeById 根据id查询规则节点数据
	FindRuleNodeById(tenantId, ruleNodeId string) (*RuleNode, error)
	// GetRuleChainNodes 查询规则链的节点列表
	GetRuleChainNodes(tenantId, ruleChainId string) ([]*RuleNode, error)
	// FindRuleChains 查询租户的全部规则链
	FindRuleChains(tenantId string) ([]*RuleChain, error)
	// GetRuleNodeRelations 查询节点的连接关系列表
	GetRuleNodeRelations(tenantId, nodeId string) ([]*Relation, error)
}

type TestRuleChainService struct {
}

func (t *TestRuleChainService) GetRuleNodeRelations(tenantId, nodeId string) ([]*Relation, error) {
	if nodeId == "node1" {
		return []*Relation{
			{
				From:              "node1",
				To:                "node2",
				Type:              "True",
				RelationTypeGroup: COMMON,
			},
		}, nil
	}
	if nodeId == "node2" {
		return []*Relation {
			{
				From:              "node2",
				To:                "node3",
				Type:              "True",
				RelationTypeGroup: COMMON,
			},
		}, nil
	}
	return nil, nil
}

func (t *TestRuleChainService) FindRuleChainById(tenantId, ruleChainId string) (*RuleChain, error) {
	return &RuleChain{
		TenantId:    "4jnh0r0hrl0c5a8mmecuoew200o32b8g",
		Name:        "test rule chain 1",
		FirstNodeId: "node1",
		IsDebug:     false,
		IsRoot:      true,
		Config:      "",
		ChainId:     "chain id 1",
	}, nil
}

func (t *TestRuleChainService) FindRuleNodeById(tenantId, ruleNodeId string) (*RuleNode, error) {
	switch ruleNodeId {
	case "node1":
		return &RuleNode{
			RuleChainId: "chain id 1",
			Type:        "MsgTypeFilterNode",
			Name:        "filternode1",
			IsDebug:     false,
			Config:      "",
			RuleNodeId:  "node1",
		}, nil
	case "node2":
		return &RuleNode{
			RuleChainId: "chain id 2",
			Type:        "MsgTypeFilterNode",
			Name:        "filternode2",
			IsDebug:     false,
			Config:      "",
			RuleNodeId:  "node2",
		}, nil
	case "node3":
		return &RuleNode{
			RuleChainId: "chain id 2",
			Type:        "RestApiRequestNode",
			Name:        "rest api",
			IsDebug:     false,
			Config:      "",
			RuleNodeId:  "node3",
		}, nil
	}
	return nil, nil
}

func (t *TestRuleChainService) GetRuleChainNodes(tenantId, ruleChainId string) ([]*RuleNode, error) {
	return []*RuleNode{
		{
			RuleChainId: "chain id 1",
			Type:        "MsgTypeFilterNode",
			Name:        "filternode",
			IsDebug:     false,
			Config:      "",
			RuleNodeId:  "node1",
		},
		{
			RuleChainId: "chain id 2",
			Type:        "MsgTypeFilterNode",
			Name:        "filternode",
			IsDebug:     false,
			Config:      "",
			RuleNodeId:  "node2",
		},
		{
			RuleChainId: "chain id 2",
			Type:        "RestApiRequestNode",
			Name:        "rest api",
			IsDebug:     false,
			Config:      "{\"url\":\"http://localhost/api/test\",\"headers\":{},\"retry\":3,\"method\":\"post\",\"time_out\":5,\"retry_wait\":1}",
			RuleNodeId:  "node3",
		},
	}, nil
}

func (t *TestRuleChainService) FindRuleChains(tenantId string) ([]*RuleChain, error) {
	return []*RuleChain{
		{
			TenantId:    "4jnh0r0hrl0c5a8mmecuoew200o32b8g",
			Name:        "test rule chain 1",
			FirstNodeId: "node1",
			IsDebug:     false,
			IsRoot:      true,
			Config:      "",
			ChainId:     "chain id 1",
		},
	}, nil
}
