package action

import (
	"crm-api/internal/app/ami"
	"crm-api/pkg/lfshook"
	"crm-api/pkg/utils"
	"fmt"
)

// Hangup 挂断指定分机或通道
func Hangup(channel string) {
	lfshook.NewLogger().Infof("hangup extensions/channel %s", channel)
	if !utils.IsChannel(channel) {
		channel = fmt.Sprintf(`/^(PJ)?SIP\/%s-.*$/`, channel)
	}

	action := map[string]string{
		"Action":  "hangup",
		"Channel": channel,
	}
	lfshook.NewLogger().Infof("hangup action %+v", action)
	if _, _, err := ami.AminInstance.Send(action); err != nil {
		lfshook.NewLogger().Errorf("Hangup %+v", err)
	}
}

// Dial 拨打号码
func Dial(src, dst, dialrule, callerID, callerName string) {
	chanel := fmt.Sprintf("Local/%s", src)
	ChanVar := fmt.Sprintf("DIAL_DEST=%s", dst)

	action := map[string]string{
		"Action":   "Originate",
		"Channel":  chanel,
		"Exten":    dst,
		"Context":  dialrule,
		"CallerID": fmt.Sprintf("%s<%s>", callerName, callerID),
		"Priority": "1",
		"async":    "true",
		"Variable": ChanVar,
	}
	lfshook.NewLogger().Infof("dial action %+v", action)
	res, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Errorf("%+v", err)
	}
	lfshook.NewLogger().Info(res)
}

/* *****************************************************************************************
// ChanSpy
func ChanSpy(src, dst string, whisper, bargein bool) {
	lfshook.NewLogger().Infof("chan spy src:%s dst:%s", src, dst)

	channel := fmt.Sprintf("%s/%s", utils.DialPrefix, dst)
	data := fmt.Sprintf("%s/%s,q,E", utils.DialPrefix, src)

	if whisper {
		data = fmt.Sprintf("%s,w", data)
	}

	if bargein {
		data = fmt.Sprintf("%s,B", data)
	}

	action := map[string]string{
		"Action":      "Originate",
		"Channel":     channel, // 不存在的通话
		"Application": "ChanSpy",
		"Data":        data, // 存在的通话
		"CallerID":    dst,
		"Async":       "true",
	}
	lfshook.NewLogger().Infof("ChanSpy action %+v", action)
	_, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Errorf("%+v", err)
	}
}

// Page
func Page(src string, extensions []string, duplex bool) {
	channel := fmt.Sprintf("%s/%s", utils.DialPrefix, src)
	data := make([]string, 0)
	for _, exten := range extensions {
		data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
	}
	appData := strings.Join(data, "&")
	if duplex {
		appData = fmt.Sprintf("%s,d", appData)
	}
	//timeout
	appData = fmt.Sprintf("%s,30", appData)
	action := map[string]string{
		"Action":      "Originate",
		"Channel":     channel,
		"Application": "page",
		"Data":        appData,
		"CallerID":    src,
		"CallerSrc":   src,
		"async":       "true",
		"Variable":    "CDR(group_type)=rg",
	}
	lfshook.NewLogger().Infof("page action %+v", action)
	res, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Errorf("%+v", err)
	}
	lfshook.NewLogger().Infof("%+v", res)
}

// Play 转 AGI 播放
func Play(name, UUID string, extensions []string, hangupAll bool) {
	if hangupAll {
		lfshook.NewLogger().Infof("hangup all before play to %+v", extensions)
		for _, extension := range extensions {
			Hangup(extension)
		}
		time.Sleep(3 * time.Second)
	}

	channel := "Local/broadcast@broadcast"
	data := make([]string, 0)
	for _, exten := range extensions {
		data = append(data, fmt.Sprintf("%s/%s", utils.DialPrefix, exten))
	}
	appData := strings.Join(data, "&")
	//timeout
	appData = fmt.Sprintf("%s,,%d", appData, 3000)
	variable := fmt.Sprintf("task_UUID=%s", UUID)
	action := map[string]string{
		"Action":      "Originate",
		"Channel":     channel,
		"Application": "page",
		"Data":        appData,
		"Variable":    variable,
		//"CallerID":    fmt.Sprintf("%s<%s>", configs.ConfigGlobal.AsteriskBroadcastName, configs.ConfigGlobal.AsteriskBroadcastID),
		"CallerID": fmt.Sprintf("%s<%s>", name, name),
		"CallSrc":  name,
		"async":    "true",
	}
	lfshook.NewLogger().Infof("play action %+v", action)
	_, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Errorf("%+v", err)
	}
}

// Redirect 转接
func Redirect(channel, dst, dialrule string) (err error) {
	callerID := "redirect"
	lfshook.NewLogger().Infof("redirect src %s to dst %s", channel, dst)
	if !utils.IsChannel(channel) {
		callerID = channel
		if channel, err = GetChannelByExten(channel); err != nil {
			return err
		}
	}

	action := map[string]string{
		"Action":    "Redirect",
		"Channel":   channel,
		"Extension": dst,
		"Context":   dialrule,
		"CallerID":  callerID,
		"Priority":  "1",
		"async":     "true",
	}
	lfshook.NewLogger().Infof("redirect %+v", action)
	res, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Error(err)
	}
	lfshook.NewLogger().Info(res)
	return err
}

// Atxfer
func Atxfer(channel, dst, dialrule string) (err error) {
	// 获取通道
	if !utils.IsChannel(channel) {
		if channel, err = GetChannelByExten(channel); err != nil {
			return err
		}
	}

	action := map[string]string{
		"Action":    "Atxfer",
		"Channel":   channel,
		"Extension": dst,
		"Context":   dialrule,
	}
	lfshook.NewLogger().Infof("atxfer action %+v", action)
	res, _, err := ami.AminInstance.Send(action)
	if err != nil {
		lfshook.NewLogger().Errorf("%+v", err)
		return err
	}
	lfshook.NewLogger().Debug("atxfer res %+v", res)
	return err
}

// ***************************************************************************************** */
