package hub

import (
	"net/url"
	"vppn/hub/api"

	"git.crumpington.com/lib/go/webutil"
)

func (app *App) formGetNetwork(form url.Values) (*api.Network, error) {
	var id int64
	if err := webutil.NewFormScanner(form).Scan("NetworkID", &id).Error(); err != nil {
		return nil, err
	}

	return app.api.Network_Get(id)
}

func (app *App) formGetNetworkPeers(form url.Values) (*api.Network, []*api.Peer, error) {
	n, err := app.formGetNetwork(form)
	if err != nil {
		return nil, nil, err
	}

	peers, err := app.api.Peer_List(n.NetworkID)
	return n, peers, err
}

func (app *App) formGetPeer(form url.Values) (*api.Network, *api.Peer, error) {
	net, err := app.formGetNetwork(form)
	if err != nil {
		return nil, nil, err
	}

	var ip byte
	if err := webutil.NewFormScanner(form).Scan("PeerIP", &ip).Error(); err != nil {
		return nil, nil, err
	}

	peer, err := app.api.Peer_Get(net.NetworkID, ip)
	return net, peer, err
}
