package network

import (
	"floares/lib/http/containers"
	"floares/lib/http/network"
	"floares/lib/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

func ListConnect(c *gin.Context) {
	ns := c.Param("ns")
	if ns == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": http.StatusBadRequest,
			"msg":  "namespace is required",
		})
	}
	inspect := network.GetInspect(ns)
	var res model.NetworkInspect
	res.Name = inspect.Name
	res.Id = inspect.Id
	res.Ipam = model.Config{
		Subnet:  inspect.IPAM.Config[0].Subnet,
		Gateway: inspect.IPAM.Config[0].Gateway,
	}
	res.Containers = make([]model.NetConnect, 0)
	res.Driver = inspect.Driver
	res.Created = inspect.Created.Format(time.DateTime)
	for k, v := range inspect.Containers {
		container := containers.InspectContainer(k)
		res.Containers = append(res.Containers, model.NetConnect{
			Name:    v.Name,
			Id:      k,
			Gateway: container.NetworkSettings.Networks[ns].Gateway,
			Alias:   container.NetworkSettings.Networks[ns].Aliases,
			Ip:      v.IPv4Address,
			Mac:     v.MacAddress,
		})
	}
	c.JSON(http.StatusOK, gin.H{
		"data": res,
		"code": http.StatusOK,
		"err":  "",
	})

}
