package diag

import (
	"net"
)

type tcp4 struct {
	children []Node
	addr     string
}

func (d *tcp4) String() string {
	return "tcp4/" + d.addr
}

func (d *tcp4) Then(t Node) Node {
	d.children = append(d.children, t)
	return d
}

func (d *tcp4) Children() []Node {
	return d.children
}

func (d *tcp4) Evaluate() (string, error) {
	conn, err := net.Dial("tcp4", d.addr)
	if err != nil {
		return "", err
	}
	defer conn.Close()
	return "connection established", nil
}

// TCP4 returns a Node which succeeds when the specified address accepts a TCPv4
// connection.
func TCP4(addr string) Node {
	return &tcp4{addr: addr}
}

type tcp6 struct {
	children []Node
	addr     string
}

func (d *tcp6) String() string {
	return "tcp6/" + d.addr
}

func (d *tcp6) Then(t Node) Node {
	d.children = append(d.children, t)
	return d
}

func (d *tcp6) Children() []Node {
	return d.children
}

func (d *tcp6) Evaluate() (string, error) {
	conn, err := net.Dial("tcp6", d.addr)
	if err != nil {
		return "", err
	}
	defer conn.Close()
	return "connection established", nil
}

// TCP6 returns a Node which succeeds when the specified address accepts a TCPv6
// connection.
func TCP6(addr string) Node {
	return &tcp6{addr: addr}
}
