package networking

import (
	"crypto/tls"
	"crypto/x509"
	"encoding/json"
	"io"
	"log"
)

type Connection[T any] struct {
	Conn    *tls.Conn
	encoder *json.Encoder
	decoder *json.Decoder
}

func NewConnection[T any](netConn *tls.Conn) Connection[T] {
	return Connection[T]{
		Conn:    netConn,
		encoder: json.NewEncoder(netConn),
		decoder: json.NewDecoder(netConn),
	}
}

func (c Connection[T]) Send(obj T) error {
    if err := c.encoder.Encode(&obj); err!=nil {
		if err == io.EOF {
			log.Println("Connection closed by peer")
		} 
        return err
    }
    //Return true as connection active
    return nil
}

func (c Connection[T]) Receive() (*T, error) {
	var obj T
	if err := c.decoder.Decode(&obj); err != nil {
		if err == io.EOF {
			log.Println("Connection closed by peer")
		} 	
        return nil,err
    }
    //Return true as connection active
	return &obj, nil
}

func (c Connection[T]) GetPeerCertificate() *x509.Certificate {
	state := c.Conn.ConnectionState()
	return state.PeerCertificates[0]
}