package lib

import (
	"bytes"
	"errors"
	"fmt"
	"strings"
	"time"

	"github.com/sirupsen/logrus"
)

type ParamMgr struct {
	Timeout  time.Duration
	Lps      uint32
	Duration time.Duration
	Caller   Handler          //调用器
	ResultCh chan *CallResult //调用结果
}

func (param *ParamMgr) Check() error {
	var errMsgs []string

	if param.Caller == nil {
		errMsgs = append(errMsgs, "无效的调用器")
	}

	if param.Timeout == 0 {
		errMsgs = append(errMsgs, "参数Timeout无效")
	}

	if param.Duration == 0 {
		errMsgs = append(errMsgs, "参数Duration无效")
	}

	if param.Lps == 0 {
		errMsgs = append(errMsgs, "参数lps无效")
	}

	if param.ResultCh == nil {
		errMsgs = append(errMsgs, "结果通过未实例化")
	}

	var buf bytes.Buffer
	buf.WriteString("校验参数")
	if errMsgs != nil {
		errMsg := strings.Join(errMsgs, " ")
		buf.WriteString(fmt.Sprintf("没有通过 %s", errMsg))
		logrus.Infoln(buf.String())
		return errors.New(errMsg)
	}

	buf.WriteString(fmt.Sprintf("通过. (timeoutMS=%s, lps=%d, durationS=%s)",
		param.Timeout, param.Lps, param.Duration))
	logrus.Debugln(buf.String())
	return nil
}
