// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package container

import (
	"errors"
	"fmt"
)

// 新会话(XORM会话)代理创建器
// @Implement LAPP_LF_MOM_BACKEND/container/CallerBrokerBuilder
type NewSessionBrokerBuilder struct {
}

// 创建新会话代理创建器
// 返回值:
// 1.新会话代理创建器
func NewNewSessionBrokerBuilder() *NewSessionBrokerBuilder {
	return &NewSessionBrokerBuilder{}
}

// @Reference LAPP_LF_MOM_BACKEND/container/CallerBrokerBuilder.Check
func (builder *NewSessionBrokerBuilder) Check(methodType Method) error {
	if methodType == nil {
		return errors.New(fmt.Sprintf("方法类型不能为空!"))
	}
	// 从接口类型获取方法类型,Receiver不算作入参
	if methodType.NumIn() < 1 {
		return errors.New(fmt.Sprintf("参数太少!实际数量:%d", methodType.NumIn()))
	}
	if methodType.In(0) != RequestContextType {
		return errors.New(fmt.Sprintf("第一个参数必须是*RequestContext"))
	}
	for i := 1; i < methodType.NumIn(); i++ {
		if methodType.In(i) == RequestContextType {
			return errors.New(fmt.Sprintf("第%d个参数不能是*RequestContext", i+1))
		}
	}
	return nil
}

// @Reference LAPP_LF_MOM_BACKEND/container/CallerBrokerBuilder.Build
func (builder *NewSessionBrokerBuilder) Build(sessionContext *SessionContext, caller Caller) (Caller, error) {
	if sessionContext == nil {
		return nil, errors.New(fmt.Sprintf("会话上下文不能为空!"))
	}
	if caller == nil {
		return nil, errors.New(fmt.Sprintf("调用器不能为空!"))
	}
	return (&NewSessionBroker{caller, sessionContext}).Call, nil
}
