package action

import (
	"errors"
	"pbx-api-gin/api/model"
	"pbx-api-gin/pkg/lfshook"
	"pbx-api-gin/pkg/utils"
)

// CoreShowChannels 获取通话通道
func CoreShowChannels() (result []model.CoreShowChannelResVO, err error) {
	// 通过 src 查询对应通道
	_, events, err := AminInstance.Send(map[string]string{
		"Action": "CoreShowChannels",
	})
	if err != nil {
		lfshook.NewLogger().Errorf("core show channels error %+v", err)
		return nil, err
	}

	lfshook.NewLogger().Infof("events %+v", events)
	result = make([]model.CoreShowChannelResVO, 0)
	for _, event := range events {
		if event.Data["Event"] == "CoreShowChannel" {
			channel := model.CoreShowChannelResVO{
				CallerIDName:      event.Data["CallerIDName"],
				CallerIDNum:       event.Data["CallerIDNum"],
				ConnectedLineName: event.Data["ConnectedLineName"],
				ConnectedLineNum:  event.Data["ConnectedLineNum"],
				Channel:           event.Data["Channel"],
				Duration:          event.Data["Duration"],
				DurationSecond:    utils.TimeStringToSecond(event.Data["Duration"]),
				ChannelStateDesc:  event.Data["ChannelStateDesc"],
			}
			result = append(result, channel)
		}
	}
	lfshook.NewLogger().Infof("channels %+v", result)
	return result, nil
}

// GetChannelByExten  通过 exten 查询对应通道
func GetChannelByExten(exten string) (channel string, err error) {
	lfshook.NewLogger().Infof("GetChannelByExten %s", exten)
	_, events, err := AminInstance.Send(map[string]string{
		"Action": "CoreShowChannels",
	})
	if err != nil {
		lfshook.NewLogger().Errorf("core show channels error %+v", err)
		return "", err
	}

	for _, event := range events {
		lfshook.NewLogger().Infof("CoreShowChannels event Data %+v", event.Data)
		if event.Data["Event"] == "CoreShowChannel" && event.Data["ConnectedLineNum"] == exten && event.Data["CallerIDNum"] != exten {
			channel = event.Data["Channel"]
			lfshook.NewLogger().Infof("GetChannelByExten get channel %s", channel)
			break
		}
	}
	if channel == "" {
		lfshook.NewLogger().Errorf("not found channel %s", exten)
		return "", errors.New("not found channel")
	}

	return channel, nil
}

// GetChannelByExten  通过 exten 查询对应通道
func GetChannelByExtenNotBridged(exten string) (channel string, err error) {
	lfshook.NewLogger().Infof("GetChannelByExten %s", exten)
	_, events, err := AminInstance.Send(map[string]string{
		"Action": "CoreShowChannels",
	})

	if err != nil {
		lfshook.NewLogger().Errorf("core show channels error %+v", err)
		return "", err
	}

	for _, event := range events {
		//lfshook.NewLogger().Infof("CoreShowChannels event Data %+v", event.Data)
		if event.Data["Event"] == "CoreShowChannel" && event.Data["CallerIDNum"] == exten {
			channel = event.Data["Channel"]
			lfshook.NewLogger().Infof("GetChannelByExten get channel %s", channel)
			break
		}
	}
	if channel == "" {
		lfshook.NewLogger().Errorf("not found channel %s", exten)
		return "", errors.New("not found channel")
	}

	return channel, nil
}

// GetBridgedChan  通过 exten 查询对应通道
func GetExtenChan(exten string) (channel string, err error) {
	lfshook.NewLogger().Infof("GetExtenChan %s", exten)
	_, events, err := AminInstance.Send(map[string]string{
		"Action": "CoreShowChannels",
	})
	if err != nil {
		lfshook.NewLogger().Errorf("core show channels error %+v", err)
		return "", err
	}

	for _, event := range events {
		//	lfshook.NewLogger().Infof("CoreShowChannels event Data %+v", event.Data)
		if event.Data["Event"] == "CoreShowChannel" && event.Data["ConnectedLineNum"] != exten && event.Data["CallerIDNum"] == exten {
			channel = event.Data["Channel"]
			lfshook.NewLogger().Infof("GetChannelByExten get channel %s", channel)
			break
		}
	}
	if channel == "" {
		lfshook.NewLogger().Errorf("not found channel %s", exten)
		return "", errors.New("not found channel")
	}

	return channel, nil
}
