package service

import (
	"be.ems/src/framework/vo"
	packetTask "be.ems/src/modules/trace/packet_task"
)

// 实例化服务层 Packet 结构体
var NewPacket = &Packet{}

// 信令跟踪 服务层处理
type Packet struct{}

// NetworkDevices 获取网卡设备信息
func (s *Packet) NetworkDevices() []vo.TreeSelect {
	arr := make([]vo.TreeSelect, 0)
	devices, err := packetTask.NetworkDevices()
	if err != nil {
		return arr
	}

	for _, device := range devices {
		if len(device.Addresses) == 0 {
			continue
		}

		lable := device.Description
		if lable == "" {
			lable = device.Name
		}
		item := vo.TreeSelect{
			ID:       device.Name,
			Label:    lable,
			Children: []vo.TreeSelect{},
		}

		for _, address := range device.Addresses {
			if address.IP != nil {
				ip := address.IP.String()
				item.Children = append(item.Children, vo.TreeSelect{ID: ip, Label: ip})
			}
		}
		arr = append(arr, item)
	}

	return arr
}

// LiveStart 开始捕获数据
func (s *Packet) LiveStart(taskNo, deviceName, filterBPF string, outputPCAP bool) (string, error) {
	return packetTask.LiveStart(taskNo, deviceName, filterBPF, outputPCAP)
}

// LiveFilter 捕获过滤
func (s *Packet) LiveFilter(taskNo, expr string) error {
	return packetTask.LiveFilter(taskNo, expr)
}

// LiveTimeout 更新捕获失效时间
func (s *Packet) LiveTimeout(taskNo string, seconds int) error {
	return packetTask.LiveTimeout(taskNo, seconds)
}

// LiveStop 停止捕获数据
func (s *Packet) LiveStop(taskNo string) error {
	return packetTask.LiveStop(taskNo)
}
