package icsnet

import (
	"fmt"
	"net"

	"gitlab.com/ics_cinnamon/voiceStatistics/icserror"
)

var MAX_MTU int = 1500

type IcsNeter interface {
	Listen() *icserror.IcsError

	Connect(localAddr, remoteAddr *IcsNetAddr) *icserror.IcsError
	Close() *icserror.IcsError

	Write(b []byte) (int, *icserror.IcsError)
	WriteSIP(b []byte) (int, *icserror.IcsError)
	WriteRTP(b []byte) (int, *icserror.IcsError)

	Read(size int) ([]byte, int, *icserror.IcsError)
	ReadSIP() ([]byte, *net.UDPAddr, int, *icserror.IcsError)
	ReadRTP() ([]byte, *net.UDPAddr, int, *icserror.IcsError)

	LocalAddr() net.Addr
	RemoteAddr() net.Addr
	SetRemoteAddr(raddr *IcsNetAddr)
}

type IcsConn struct {
	ICSUDPConn *net.UDPConn
	ICSTCPConn *net.TCPConn
}

func GetMyIP() ([]string, *icserror.IcsError) {
	var retIP []string
	ifaces, err := net.Interfaces()
	if err != nil {
		icserror.ICSERRNETNotFoundIF.SetError(err)
		return nil, icserror.ICSERRNETNotFoundIF
	}

	for iter, i := range ifaces {
		addrs, err := i.Addrs()
		// handle err
		if err != nil {
			icserror.ICSERRNETGetAddr.SetError(err)
			return nil, icserror.ICSERRNETGetAddr
		}
		for _, addr := range addrs {
			var ip net.IP
			switch v := addr.(type) {
			case *net.IPNet:
				ip = v.IP
			case *net.IPAddr:
				ip = v.IP
			}
			retIP[iter] = fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
		}
	}

	return retIP, nil
}
