package network

import (
	"encoding/json"
	"floares/config"
	"floares/lib/model"
	"log"
	"net/http"
	"time"
)

func ListJsons() []model.NetWorkJson {
	req, _ := http.NewRequest("GET", config.Entrypoint+"/networks", nil)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Println("request for network list error:", err)
		return []model.NetWorkJson{}
	}
	var result []model.NetWorkJson
	err = json.NewDecoder(res.Body).Decode(&result)
	return result
}
func ListNetwork() []model.NetWork {
	var result []model.NetWork
	for _, v := range ListJsons() {
		conf := v.IPAM.Config
		var ipam []model.Config
		for _, val := range conf {
			ipam = append(ipam, model.Config{
				Subnet: val.Subnet, Gateway: val.Gateway,
			})
		}
		result = append(result, model.NetWork{
			Name:    v.Name,
			Id:      v.Id,
			Created: v.Created.Format(time.DateTime),
			Driver:  v.Driver,
			IPAM:    ipam,
		})
	}
	return result
}
