package network

import (
	"errors"
	"floares/config"
	"fmt"
	"io"
	"log"
	"net/http"
	"strings"
)

func DisConnect(ns string, id string) (err error) {
	reader := strings.NewReader(fmt.Sprintf(`{
		"container":"%s"
	}`, id))
	req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/networks/"+ns+"/disconnect", reader)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Println("request for network list error:", err)
		return
	}
	if res.StatusCode != 200 {
		str, _ := io.ReadAll(res.Body)
		log.Println(string(str))
		err = errors.New(res.Status)

	}
	return
}

func Connect(ns string, id string) (err error) {
	reader := strings.NewReader(fmt.Sprintf(`{
		"container":"%s"
	}`, id))
	req, _ := http.NewRequest(http.MethodPost, config.Entrypoint+"/v4.0.0/libpod/networks/"+ns+"/connect", reader)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Println("request for network list error:", err)
		return
	}
	if res.StatusCode != 200 {
		str, _ := io.ReadAll(res.Body)
		log.Println(string(str))
		err = errors.New(res.Status)

	}
	return
}
