package types

import (
	"git.loafle.net/overflow/overflow_probe/discovery/discovery/types/timestamp"
	"strconv"
)

type DiscoveryPort struct {
	Port_ string `json:"-"`

	ID         int                   `json:"id,omitempty"`
	CreateDate timestamp.Timestamp   `json:"createDate,omitempty"`
	UpdateDate timestamp.Timestamp   `json:"updateDate,omitempty"`
	Host       *DiscoveryHost        `json:"-"`
	Services   []*DiscoveryService   `json:"services"`
	Histories  []*ServiceScanHistory `json:"-"`

	PortType string `json:"portType"`
	Number   uint16 `json:"portNumber"`

	Target bool `json:"target,omitempty"`
}

type PortScanHistory struct {
	ID            int                 `json:"id,omitempty"`
	CreateDate    timestamp.Timestamp `json:"createDate,omitempty"`
	Host          *DiscoveryHost      `json:"-"`
	PortNumber    uint16              `json:"portNumber"`
	PortType      string              `json:"portType"`
	DirectionType string              `json:"directionType"`
	Description   string              `json:"description"`
	StartDate     timestamp.Timestamp `json:"startDate"`
	EndDate       timestamp.Timestamp `json:"endDate"`
}

type PortMgr struct {
	DiscoveryMgr
	FromHost  chan *DiscoveryHost
	ToService chan *DiscoveryPort
}

func NewPort(port string, host *DiscoveryHost, portType string) *DiscoveryPort {
	rPort := &DiscoveryPort{Port_: port, Host: host, PortType: portType}
	i, _ := strconv.ParseUint(port, 10, 16)
	rPort.Number = uint16(i)
	return rPort
}

func NewPortScanHistory(host *DiscoveryHost, port uint16, portType string, directionType string, description string) *PortScanHistory {
	rPortSH := &PortScanHistory{
		CreateDate:    timestamp.Now(),
		Host:          host,
		PortNumber:    port,
		PortType:      portType,
		DirectionType: directionType,
		Description:   description,
	}
	host.AddHistory(rPortSH)
	return rPortSH
}

func (p *DiscoveryPort) SetHistory(h []*ServiceScanHistory) {
	p.Histories = h
}
