package action

import (
	"errors"
	"fmt"
	alstatus "pbx-api-gin/internal/app/stc/sendstatus"
	"pbx-api-gin/pkg/lfshook"
	"strings"
)

func PlaybackPacu(filename string, count int, delay int, PaType string) (err error) {

	Para := fmt.Sprintf("count=%d,filename=%s,delay=%d,priority=%s", count, strings.Replace(strings.Replace(filename, ".mp3", "", -1), ".wav", "", -1), delay, PaType)
	Chan := ""

	switch PaType {
	case "STN":
		Chan = "Local/0503" //spa-rule
	case "SPC":
		Chan = "Local/0505" //spc-rule
	case "DCS":
		Chan = "Local/0504" //dc-rule
	case "CHK":
		Chan = "Local/0510" //paging pacu
	case "EMG":
		Chan = "Local/0502" //emg-rule
	case "VOL":
		Chan = "Local/0513" //emg-rule
	}

	action := map[string]string{
		"Action":   "Originate",
		"Channel":  Chan,
		"Exten":    "000",
		"CallerID": PaType,
		"Context":  "broadcast-playfile",
		"Priority": "1",
		"Variable": Para,
	}

	lfshook.NewLogger().Logger.Infof("PlaybackPacu action : %+v", action)

	res, _, err := AminInstance.Send(action)
	if err != nil {
		return err
	}
	if res["Response"] != "Success" {
		alstatus.PaStatus("", PaType, "fail")
		return errors.New(res["Message"])
	}
	return nil
}
